Method: Interest::Definition.instance_methods_for

Defined in:
lib/interest/definition.rb

.instance_methods_for(source, target) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/interest/definition.rb', line 8

def instance_methods_for(source, target)
  Module.new do
    define_method :"#{source}_collection_for" do |record|
      __send__ self.class.__send__(:"#{source}_association_method_name_for", record)
    end

    define_method :method_missing do |name, *args, &block|
      return super(name, *args, &block) unless matches = /\A#{target}_(?<type>.+)\Z/.match(name.to_s)

      self.class.__send__ :"define_#{source}_association_method", Interest::Utils.source_type_of(matches[:type], self.class.model_name.to_s)

      __send__ name, *args, &block
    end

    define_method :respond_to_missing? do |name, include_private = false|
      !! (super(name, include_private) or /\A#{target}_.+\Z/ =~ name.to_s)
    end
  end
end