Class: PassiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/passive_record/base.rb

Constant Summary collapse

@@instances =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.

Yields:

  • (_self)

Yield Parameters:



12
13
14
15
# File 'lib/passive_record/base.rb', line 12

def initialize(attributes = {})
  @attributes = attributes
  yield self if block_given?
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



8
9
10
# File 'lib/passive_record/base.rb', line 8

def attributes
  @attributes
end

#keyObject Also known as: id

Returns the value of attribute key.



8
9
10
# File 'lib/passive_record/base.rb', line 8

def key
  @key
end

Class Method Details

.countObject



51
52
53
# File 'lib/passive_record/base.rb', line 51

def count
  find_every.size
end

.create(*args) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/passive_record/base.rb', line 35

def create(*args)
  attributes = extract_options!(args)
  key = args.first || find_every.size+1
  instance = self.new(attributes)
  instance.key = key
  @@instances[base_key(key)] = instance
  return key
end

.find(*args) ⇒ Object



46
47
48
49
# File 'lib/passive_record/base.rb', line 46

def find(*args)
  options = extract_options!(args)
  args.first == :all ? find_every : find_from_keys(args)
end

Instance Method Details

#==(comparison_object) ⇒ Object



25
26
27
28
29
30
# File 'lib/passive_record/base.rb', line 25

def ==(comparison_object)
  comparison_object.equal?(self) ||
    (comparison_object.instance_of?(self.class) && 
      comparison_object.id == id && 
      !comparison_object.new_record?)
end

#[](attribute_name) ⇒ Object



17
18
19
# File 'lib/passive_record/base.rb', line 17

def [](attribute_name)
  self.attributes[attribute_name]
end

#[]=(attribute_name, value) ⇒ Object



21
22
23
# File 'lib/passive_record/base.rb', line 21

def []=(attribute_name, value)
  self.attributes[attribute_name] = value
end