active_pstore

This library has Active Record like interface. Use pstore to store data.

MIT License

Downloads
44.2K
Stars
15
Committers
2

Active PStore Build Status Code Climate Test Coverage Gem Version

Active Recordに似たインターフェイスを持ったライブラリです。 データの保存にはpstoreを使います。

SYNOPSIS

データの保存先のファイルパス指定

require 'active_pstore'

ActivePStore::Base.establish_connection(database: '/path/to/file')

クラス定義

class Artist < ActivePStore::Base
  attr_accessor :name
end

インスタンスの生成

randy_rhoads = Artist.new(name: 'Randy Rhoads')

randy_rhoads = Artist.new {|a|
  a.name = 'Randy Rhoads'
}

ActivePStore::Base.new メソッドと別名として ActivePStore::Base.build を使うこともできます。

インスタンスの保存

randy_rhoads.save

saveメソッドの対象オブジェクトのクラス名がPStoreのキーになります。

Artist.table_name # => 'Artist'

例えばPStoreをもちいて保存されたartistオブジェクトを取得してみます。

database = PStore.new('/path/to/file')
database.transaction {|db| artist = db['Artist'] } # fetch instances of Artist class.

ID

保存時にSecureRandom.hexの値を使ったActivePStore::Base#idが付与されます。

randy_rhoads = Artist.new(name: 'Randy Rhoads')
randy_rhoads.id # => nil

randy_rhoads.save
randy_rhoads.id # => "0b84ece5d5be3bce3ee2101c1c4f6fda"

インスタンスの生成時に保存

randy_rhoads = Artist.create(name: 'Randy Rhoads')

randy_rhoads = Artist.create {|a|
  a.name = 'Randy Rhoads'
}

検索系

Artist.all
Artist.first
Artist.last
Artist.find('388980778246cbcbfcbb7a8292f28c37')
Artist.where(name: 'Randy Rhoads')

範囲指定

Artist.where(birth_date: Date.new(1948, 12, 3)..Date.new(1956, 12, 6))

続きはテストコードをご参照ください。

Integration with Rails

Active PStore では以下のジェネレータを用意しています。

Generate config file

以下の行をRailsアプリケーションで実行してください。

bundle exec rails g active_pstore:config

config/active_pstore.yml が生成されます。

config/database.yml をもちいて Active PStore が pstore を使った保存先の情報を指定します。

development:
  database: db/active_pstore_development.yml

Generate model file

以下の行をRailsアプリケーションで実行してください。

bundle exec rails g active_pstore:model artist name associated_act instrument birth_date

app/models/artist.rb が生成されます。

class Artist < ActivePStore::Base
  attr_accessor :name
  attr_accessor :associated_act
  attr_accessor :instrument
  attr_accessor :birth_date
end

REQUIREMENTS

INSTALL

Add these lines to your application's Gemfile:

gem 'active_pstore'

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_pstore

And require it as:

require 'active_pstore'

問題点

  • トランザクションをサポートしてません (PStoreサポートレベルまで少しなんとかしたい)
  • データマイグレーションをサポートしてません
  • パフォーマンスへはかなりアマい実装になっています (いずれもう少しなんとかしたい)
  • これらのことからエンタープライズ要件には向かないです

プレゼンテーション文書

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Active PStore is released under the MIT License.

Package Rankings
Top 18.83% on Rubygems.org
Badges
Extracted from project README
Build Status Code Climate Test Coverage Gem Version