module Ephemeral::Base::ClassMethods

Public Instance Methods

attach_scopes() click to toggle source
# File lib/ephemeral/base.rb, line 91
def attach_scopes
  scopes.each do |k, v|
    if v.is_a?(Proc)
      define_singleton_method(k, v)
    else
      define_singleton_method k, lambda { v }
    end
  end
end
collections() click to toggle source
# File lib/ephemeral/base.rb, line 78
def collections
  @@collections ||= {}
end
collects(name=nil, args={}) click to toggle source
# File lib/ephemeral/base.rb, line 29
def collects(name=nil, args={})
  return @@collections unless name
  class_name = args[:class_name] || name.to_s.classify
  @@collections ||= {}
  @@collections[name] = Ephemeral::Collection.new(class_name)

  self.send :define_method, name do
    self.collections[class_name] ||= Ephemeral::Collection.new(class_name)
    self.collections[class_name]#.objects
  end

  self.send :define_method, "#{name}=" do |objects|
    self.collections[class_name] = Ephemeral::Collection.new(class_name, objects)
  end

end
delete_all() click to toggle source
# File lib/ephemeral/base.rb, line 46
def delete_all
  class_variable_set(:@@objects, [])
end
execute_scope(method=nil) click to toggle source
# File lib/ephemeral/base.rb, line 111
def execute_scope(method=nil)
  results = scopes[method].inject([]) {|a, (k, v)| a << self.objects.select {|o| o.send(k) == v } }.flatten
  Ephemeral::Collection.new(self.name, results)
end
has_one(name, args={}) click to toggle source
# File lib/ephemeral/base.rb, line 50
def has_one(name, args={})
  class_name = args[:class_name] || name.to_s.classify
  self.send :define_method, name do
    self.relations ||= {}
    self.relations[class_name] ||= Ephemeral::Relation.new(class_name).materialize
  end
  self.send :define_method, "#{name}=" do |object|
    self.relations ||= {}
    self.relations[class_name] = Ephemeral::Relation.new(class_name).materialize(object)
  end
end
method_missing(method_name, *arguments, &block) click to toggle source
Calls superclass method
# File lib/ephemeral/base.rb, line 101
def method_missing(method_name, *arguments, &block)
  scope = scopes[method_name]
  super if scope.nil?
  if scope.is_a?(Proc)
    scope.call(arguments)
  else
    execute_scope(method_name)
  end
end
new(*args, &block) click to toggle source
# File lib/ephemeral/base.rb, line 21
def new(*args, &block)
  object = allocate
  object.send(:initialize, *args, &block)
  objects = class_variable_get("@@objects")
  class_variable_set("@@objects", [objects, object].flatten)
  object
end
objects() click to toggle source
# File lib/ephemeral/base.rb, line 82
def objects
  class_variable_get("@@objects")
end
scope(name, conditions) click to toggle source
# File lib/ephemeral/base.rb, line 62
def scope(name, conditions)
  self.scopes ||= {}
  self.scopes[name] = conditions
end
scopes() click to toggle source
# File lib/ephemeral/base.rb, line 67
def scopes
  begin
    return @@scopes if @@scopes
  rescue
    @@scopes = {}
    attach_scopes
  ensure
    return @@scopes
  end
end
where(args={}) click to toggle source
# File lib/ephemeral/base.rb, line 86
def where(args={})
  results = args.inject([]) {|a, (k, v)| a << objects.select {|o| o.send(k) == v[0] || o.send(k) == v} }.flatten
  Ephemeral::Collection.new(name, results)
end