Module: Ephemeral::Base::ClassMethods
- Defined in:
- lib/ephemeral/base.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
101
102
103
104
105
106
107
108
109
|
# 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
|
Instance Method Details
#attach_scopes ⇒ Object
91
92
93
94
95
96
97
98
99
|
# 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 ⇒ Object
78
79
80
|
# File 'lib/ephemeral/base.rb', line 78
def collections
@@collections ||= {}
end
|
#collects(name = nil, args = {}) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# 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] end
self.send :define_method, "#{name}=" do |objects|
self.collections[class_name] = Ephemeral::Collection.new(class_name, objects)
end
end
|
#delete_all ⇒ Object
46
47
48
|
# File 'lib/ephemeral/base.rb', line 46
def delete_all
class_variable_set(:@@objects, [])
end
|
#execute_scope(method = nil) ⇒ Object
111
112
113
114
|
# 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 = {}) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
|
# 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
|
#new(*args, &block) ⇒ Object
21
22
23
24
25
26
27
|
# 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 ⇒ Object
82
83
84
|
# File 'lib/ephemeral/base.rb', line 82
def objects
class_variable_get("@@objects")
end
|
#scope(name, conditions) ⇒ Object
62
63
64
65
|
# File 'lib/ephemeral/base.rb', line 62
def scope(name, conditions)
self.scopes ||= {}
self.scopes[name] = conditions
end
|
#scopes ⇒ Object
67
68
69
70
71
72
73
74
75
76
|
# 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 = {}) ⇒ Object
86
87
88
89
|
# 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
|