Class: ActivePStore::Base

Inherits:
Object
  • Object
show all
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

Generators::ModelGenerator

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?, #save, #update, #update_attribute

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:



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

.allObject



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