class Ephemeral::Collection

Attributes

klass[RW]
objects[RW]

Public Class Methods

new(klass, objects=[]) click to toggle source
# File lib/ephemeral/collection.rb, line 15
def initialize(klass, objects=[])
  self.klass = klass
  attach_scopes
  self.objects = self.materialize(objects)
  self
end
respond_to?(method_sym, include_private = false) click to toggle source
# 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

Public Instance Methods

<<(objekts) click to toggle source
# File lib/ephemeral/collection.rb, line 54
def << (objekts)
  self.objects << objekts
  self.objects.flatten!
end
attach_scopes() click to toggle source
# 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) click to toggle source
# File lib/ephemeral/collection.rb, line 22
def each(&block)
  self.objects && self.objects.each(&block)
end
empty?() click to toggle source
# File lib/ephemeral/collection.rb, line 26
def empty?
  self.objects.empty?
end
execute_scope(method=nil) click to toggle source
# 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={}) click to toggle source
# File lib/ephemeral/collection.rb, line 35
def find(args={})
  where(args).first
end
last() click to toggle source
# File lib/ephemeral/collection.rb, line 39
def last
  self.objects.last
end
marshal_dump() click to toggle source
# File lib/ephemeral/collection.rb, line 59
def marshal_dump
  [@klass, @objects]
end
marshal_load(array=[]) click to toggle source
# File lib/ephemeral/collection.rb, line 63
def marshal_load(array=[])
  @klass, @objects = array
  attach_scopes
end
materialize(objects_array=[]) click to toggle source
# 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
method_missing(method_name, *arguments, &block) click to toggle source
Calls superclass method
# 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
where(args={}) click to toggle source
# 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