Module: Mycrm::Facade::InstanceMethods
- Defined in:
- lib/mycrm/facade.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
48
49
50
51
52
|
# File 'lib/mycrm/facade.rb', line 48
def method_missing(name, *args, &block)
return delegates[name] if delegates.key?(name)
delegate = delegates.values.detect { |d| d.respond_to?(name) }
delegate ? delegate.send(name, *args) : super
end
|
Class Method Details
.included(descendant) ⇒ Object
10
11
12
13
14
|
# File 'lib/mycrm/facade.rb', line 10
def self.included(descendant)
descendant.class_eval do
attr_accessor :delegates
end
end
|
Instance Method Details
#initialize(attributes = {}) ⇒ Object
16
17
18
19
20
21
|
# File 'lib/mycrm/facade.rb', line 16
def initialize(attributes = {})
self.delegates = {}
self.class.delegate_classes.each do |name, delegate|
delegates[name] = delegate[:class].new attributes[name]
end
end
|
#refresh_relation(name) ⇒ Object
38
39
40
41
42
|
# File 'lib/mycrm/facade.rb', line 38
def refresh_relation(name)
self.class.relations(name).to_a.each do |pair|
delegates[name].send("#{pair.first}=", relative_value(*pair.last.split('.'))) rescue nil
end
end
|
#relative_value(delegate_name, field) ⇒ Object
44
45
46
|
# File 'lib/mycrm/facade.rb', line 44
def relative_value(delegate_name, field)
delegates[delegate_name.to_sym].send(field)
end
|
#respond_to_missing?(name, include_private = false) ⇒ Boolean
54
55
56
|
# File 'lib/mycrm/facade.rb', line 54
def respond_to_missing?(name, include_private = false)
delegates[name] || delegates.detect { |d| d.respond_to?(name) } || super
end
|
#run ⇒ Object
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/mycrm/facade.rb', line 27
def run
{}.tap do |response|
Mycrm::Connectable.session do
delegates.each do |name, instance|
refresh_relation(name)
response[name] = yield(instance)
end
end
end
end
|
#to_json ⇒ Object
23
24
25
|
# File 'lib/mycrm/facade.rb', line 23
def to_json
delegates.each_with_object({}) { |(name, instance), r| r[name] = instance.to_json }
end
|