Class: ActivePStore::Base
- Inherits:
-
Object
- Object
- ActivePStore::Base
- Extended by:
- ConnectionHandling, Delegation, DynamicMatchers, FinderMethods, Inheritance, ModelSchema, QueryMethods, Querying
- Includes:
- ActiveModel::Model, AttributeMethods, Core, Persistence
- Defined in:
- lib/active_pstore/base.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes included from Persistence
Class Method Summary collapse
- .all ⇒ Object
- .create(attributes = nil, &block) ⇒ Object
- .find_or_create_by(attributes, &block) ⇒ Object
- .first_or_create(attributes = nil, &block) ⇒ Object
Instance Method Summary collapse
-
#initialize(attributes = nil) {|_self| ... } ⇒ Base
constructor
A new instance of Base.
Methods included from ConnectionHandling
connection_config, establish_connection, use_connection
Methods included from DynamicMatchers
Methods included from FinderMethods
find, find_by, find_by!, first, last, take
Methods included from Inheritance
Methods included from ModelSchema
Methods included from QueryMethods
Methods included from Persistence
#destroy, #new_record?, #save, #update, #update_attribute
Methods included from Core
Methods included from AttributeMethods
Constructor Details
#initialize(attributes = nil) {|_self| ... } ⇒ Base
Returns a new instance of Base.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/active_pstore/base.rb', line 18 def initialize(attributes = nil) if attributes.present? attributes.each do |attr, val| if respond_to? "#{attr}=".to_sym self.__send__("#{attr}=", val) else raise "undefined method `#{attr}='" end end end yield self if block_given? end |
Class Method Details
.all ⇒ Object
35 36 37 38 39 |
# File 'lib/active_pstore/base.rb', line 35 def all use_connection {|connection| ActivePStore::Collection.new(connection[self.pstore_key] || []) } end |
.create(attributes = nil, &block) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/active_pstore/base.rb', line 41 def create(attributes = nil, &block) if attributes.is_a?(Array) attributes.map {|attr| create(attr, &block) } else build(attributes, &block).tap do |obj| obj.save end end end |
.find_or_create_by(attributes, &block) ⇒ Object
55 56 57 |
# File 'lib/active_pstore/base.rb', line 55 def find_or_create_by(attributes, &block) find_by(attributes) || create(attributes, &block) end |
.first_or_create(attributes = nil, &block) ⇒ Object
51 52 53 |
# File 'lib/active_pstore/base.rb', line 51 def first_or_create(attributes = nil, &block) first || create(attributes, &block) end |