Class: ActivePStore::Base

Inherits:
Object
  • Object
show all
Extended by:
ConnectionHandling, Delegation, DynamicMatchers, FinderMethods, Inheritance, ModelSchema, QueryMethods, Querying
Includes:
ActiveModel::Model, AttributeMethods, Core, Integration, Persistence
Defined in:
lib/active_pstore/base.rb

Instance Attribute Summary

Attributes included from Persistence

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConnectionHandling

connection_config, establish_connection, use_connection

Methods included from DynamicMatchers

respond_to?

Methods included from FinderMethods

find, find_by, find_by!, first, last, take

Methods included from Inheritance

new

Methods included from ModelSchema

pstore_key

Methods included from QueryMethods

where

Methods included from Persistence

#destroy, #new_record?, #persisted?, #save, #update, #update_attribute

Methods included from Integration

#to_param

Methods included from Core

#==, #hash

Methods included from AttributeMethods

#[], #[]=

Constructor Details

#initialize(attributes = nil) {|_self| ... } ⇒ Base

Returns a new instance of Base.

Yields:

  • (_self)

Yield Parameters:



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_pstore/base.rb', line 19

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

.allObject



36
37
38
39
40
# File 'lib/active_pstore/base.rb', line 36

def all
  use_connection {|connection|
    ActivePStore::Collection.new(connection[self.pstore_key] || [])
  }
end

.create(attributes = nil, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/active_pstore/base.rb', line 42

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



56
57
58
# File 'lib/active_pstore/base.rb', line 56

def find_or_create_by(attributes, &block)
  find_by(attributes) || create(attributes, &block)
end

.find_or_initialize_by(attributes, &block) ⇒ Object



60
61
62
# File 'lib/active_pstore/base.rb', line 60

def find_or_initialize_by(attributes, &block)
  find_by(attributes) || new(attributes, &block)
end

.first_or_create(attributes = nil, &block) ⇒ Object



52
53
54
# File 'lib/active_pstore/base.rb', line 52

def first_or_create(attributes = nil, &block)
  first || create(attributes, &block)
end