Module: InsalesApi::Helpers::HasInsalesObject

Defined in:
lib/insales_api/helpers/has_insales_object.rb

Instance Method Summary collapse

Instance Method Details

#has_insales_object(type, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/insales_api/helpers/has_insales_object.rb', line 3

def has_insales_object(type, options = {})
  i_method_name = "insales_#{type}"
  class_method_name = "insales_#{type}_class"
  object_class = options[:class] || InsalesApi.const_get(type.to_s.camelcase)
  helpers = Module.new do
    extend ActiveSupport::Concern

    module_eval <<-RUBY, __FILE__, __LINE__ + 1
      included do                                                           # included do
        class_attribute :#{class_method_name}                               #   class_attribute :insales_order_class
        self.#{class_method_name} = #{object_class}                         #   self.insales_order_class = InsalesApi::Order
      end                                                                   # end
                                                                            #
      def #{i_method_name}(force = false)                                   # def insales_order(force = false)
        return @#{i_method_name} if @#{i_method_name} && !force             #   return @insales_order if @insales_order && !force
        @#{i_method_name} = init_api do                                     #   @insales_order = init_api do
          #{class_method_name}.find(insales_id)                             #     insales_order_class.find(insales_id)
        end                                                                 #   end
      end                                                                   # end
                                                                            #
      attr_writer :#{i_method_name}                                         # attr_writer :insales_order
                                                                            #
      def reload(*)                                                         # def reload(*)
        @#{i_method_name} = nil                                             #   @insales_order = nil
        super                                                               #   super
      end                                                                   # end
    RUBY
  end
  include helpers
end