Class: ActivePStore::Collection

Inherits:
Object
  • Object
show all
Includes:
Calculations
Defined in:
lib/active_pstore/collection.rb

Instance Method Summary collapse

Methods included from Calculations

#ids, #maximum, #minimum

Constructor Details

#initialize(objects) ⇒ Collection

Returns a new instance of Collection.



5
6
7
# File 'lib/active_pstore/collection.rb', line 5

def initialize(objects)
  @objects = objects
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



35
36
37
# File 'lib/active_pstore/collection.rb', line 35

def method_missing(method_name, *args, &block)
  @objects.__send__(method_name, *args, &block)
end

Instance Method Details

#==(comparison_object) ⇒ Object Also known as: eql?



39
40
41
# File 'lib/active_pstore/collection.rb', line 39

def ==(comparison_object)
  comparison_object.instance_of?(self.class) && self.map(&:id) <=> comparison_object.map(&:id)
end

#count(attr_name = nil) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/active_pstore/collection.rb', line 23

def count(attr_name = nil)
  if attr_name.nil? || attr_name == :all
    @objects.count
  else
    @objects.map(&attr_name.to_sym).compact.count
  end
end

#destroy_allObject



15
16
17
18
19
20
21
# File 'lib/active_pstore/collection.rb', line 15

def destroy_all
  @objects.inject(0) {|result, obj|
    obj.destroy

    result += 1
  }
end

#empty?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/active_pstore/collection.rb', line 31

def empty?
  @objects.empty?
end

#hashObject



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

def hash
  if id
    id.hash
  else
    super
  end
end

#update_all(updates) ⇒ Object



9
10
11
12
13
# File 'lib/active_pstore/collection.rb', line 9

def update_all(updates)
  @objects.each {|obj|
    obj.update_attributes(updates)
  }.count
end