Class: Ephemeral::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ephemeral/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, objects = []) ⇒ Collection

Returns a new instance of Collection.



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

def initialize(klass, objects=[])
  self.klass = klass
  attach_scopes
  self.objects = self.materialize(objects)
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



78
79
80
81
82
83
84
85
86
# File 'lib/ephemeral/collection.rb', line 78

def method_missing(method_name, *arguments, &block)
  scope = eval(self.klass).scopes[method_name]
  super if scope.nil?
  if scope.is_a?(Proc)
    scope.call(arguments)
  else
    execute_scope(method_name)
  end
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



7
8
9
# File 'lib/ephemeral/collection.rb', line 7

def klass
  @klass
end

#objectsObject

Returns the value of attribute objects.



7
8
9
# File 'lib/ephemeral/collection.rb', line 7

def objects
  @objects
end

Class Method Details

.respond_to?(method_sym, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.respond_to?(method_sym, include_private = false)
  if Collection.new(method_sym).match? || Collection.new.eval(klass).scopes[method_name]
    true
  end
end

Instance Method Details

#<<(objekts) ⇒ Object



54
55
56
57
# File 'lib/ephemeral/collection.rb', line 54

def << (objekts)
  self.objects << objekts
  self.objects.flatten!
end

#attach_scopesObject



68
69
70
71
72
73
74
75
76
# File 'lib/ephemeral/collection.rb', line 68

def attach_scopes
  eval(self.klass).scopes.each do |k, v|
    if v.is_a?(Proc)
      define_singleton_method(k, v)
    else
      define_singleton_method k, lambda { self.execute_scope(k)}
    end
  end
end

#each(&block) ⇒ Object



22
23
24
# File 'lib/ephemeral/collection.rb', line 22

def each(&block)
  self.objects && self.objects.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/ephemeral/collection.rb', line 26

def empty?
  self.objects.empty?
end

#execute_scope(method = nil) ⇒ Object



49
50
51
52
# File 'lib/ephemeral/collection.rb', line 49

def execute_scope(method=nil)
  results = eval(self.klass).scopes[method].inject([]) {|a, (k, v)| a << self.objects.select {|o| o.send(k) == v } }.flatten
  Ephemeral::Collection.new(self.klass, results)
end

#find(args = {}) ⇒ Object



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

def find(args={})
  where(args).first
end

#lastObject



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

def last
  self.objects.last
end

#marshal_dumpObject



59
60
61
# File 'lib/ephemeral/collection.rb', line 59

def marshal_dump
  [@klass, @objects]
end

#marshal_load(array = []) ⇒ Object



63
64
65
66
# File 'lib/ephemeral/collection.rb', line 63

def marshal_load(array=[])
  @klass, @objects = array
  attach_scopes
end

#materialize(objects_array = []) ⇒ Object



43
44
45
46
47
# File 'lib/ephemeral/collection.rb', line 43

def materialize(objects_array=[])
  return [] unless objects_array
  return objects_array if objects_array && objects_array.first.class.name == self.klass
  objects_array.map{|t| eval(self.klass).new(t) }
end

#where(args = {}) ⇒ Object



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

def where(args={})
  results = args.inject([]) {|a, (k, v)| a << self.objects.select {|o| o.send(k) == v} }.flatten
  Ephemeral::Collection.new(self.klass, results)
end