This library has Active Record like interface. Use pstore to store data.
MIT License
Active Recordに似たインターフェイスを持ったライブラリです。 データの保存にはpstoreを使います。
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.
保存時に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))
続きはテストコードをご参照ください。
Active PStore では以下のジェネレータを用意しています。
以下の行を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
以下の行を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
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'
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)Active PStore is released under the MIT License.