Class: AttrJson::Associations::Definer

Inherits:
Object
  • Object
show all
Defined in:
lib/attr_json/associations/definer.rb

Instance Method Summary collapse

Constructor Details

#initialize(type, klass, args, options) ⇒ Definer

Returns a new instance of Definer.



12
13
14
15
16
# File 'lib/attr_json/associations/definer.rb', line 12

def initialize(type, klass, args, options)
  @type = type
  @klass = klass
  @assoc = Association.const_get(@type.to_s.classify).new(klass, args, options)
end

Instance Method Details

#callObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/attr_json/associations/definer.rb', line 18

def call
  @klass.attr_json_associations = @klass.attr_json_associations.merge(@assoc.name => @assoc)
  @klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{@assoc.name}
      assoc = self.class.attr_json_associations[:#{@assoc.name}]
      @attr_json_associations_cache[:#{@assoc.name}] ||=
        AttrJson::Associations::Loader::#{@type.to_s.classify}.new(assoc, self).call
    end
  RUBY
end