Module: Mycrm::Facade::ClassMethods

Defined in:
lib/mycrm/facade.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(descendant) ⇒ Object



60
61
62
63
64
# File 'lib/mycrm/facade.rb', line 60

def self.extended(descendant)
  descendant.singleton_class.class_eval do
    attr_accessor :delegate_classes
  end
end

Instance Method Details

#delegate(name, clazz, relations = {}) ⇒ Object



66
67
68
69
# File 'lib/mycrm/facade.rb', line 66

def delegate(name, clazz, relations = {})
  self.delegate_classes ||= {}
  delegate_classes[name] = { class: clazz, relations: relations }
end

#relations(delegate) ⇒ Object



71
72
73
# File 'lib/mycrm/facade.rb', line 71

def relations(delegate)
  delegate_classes[delegate][:relations]
end

#run(method = :find, *args) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/mycrm/facade.rb', line 81

def run(method = :find, *args)
  attributes = {}.tap do |response|
    Mycrm::Connectable.session do
      self.delegate_classes.each do |name, delegate|
        res = if block_given?
          yield(name, delegate, response)
        elsif delegate[:class].respond_to? :find
          delegate[:class].send(method, *args)
        end
        response[name] = res.is_a?(Array) ? res.map(&:to_json) : res.to_json
      end
    end
  end
  new(attributes)
end