Module: Interest::Definition

Defined in:
lib/interest/definition.rb

Class Method Summary collapse

Class Method Details

.class_methods_for(source, target) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/interest/definition.rb', line 28

def class_methods_for(source, target)
  Module.new do
    define_method :"#{source}_association_method_name_for" do |record|
      :"#{target}_#{Interest::Utils.symbolic_name_of record}"
    end
  end
end

.collection_methods_for(target) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/interest/definition.rb', line 36

def collection_methods_for(target)
  Module.new do
    define_method :of do |*args|
      where :"#{target}_type" => args.map(&Interest::Utils.method(:source_type_of))
    end
  end
end

.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])

      __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