Module: Yaoc::MappingBase::ClassMethods

Defined in:
lib/yaoc/mapping_base.rb

Instance Method Summary collapse

Instance Method Details

#class_private_module(name = :Mapping) ⇒ Object

inspired by Avdi Grimm, rubytapas.com 028-macros-and-modules



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/yaoc/mapping_base.rb', line 89

def class_private_module(name=:Mapping)

  if const_defined?(name, false)
    const_get(name)
  else
    new_mod = Module.new do
      def self.to_s
        "Mapping (#{instance_methods(false).join(', ')})"
      end

      def self.inspect
        to_s
      end
    end
    const_set(name, new_mod)
  end
end

#converter_methodsObject



84
85
86
# File 'lib/yaoc/mapping_base.rb', line 84

def converter_methods
  class_private_module(:Mapping).instance_methods(false).sort
end

#converter_proc(to, from, deferred = false) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/yaoc/mapping_base.rb', line 57

def converter_proc(to, from, deferred=false)
  -> (to_convert, result){
    get_value_with = ->{
      to_convert.public_send(fetcher, from)
    }

    fill_result_from_proc(result, to, get_value_with, deferred)
  }
end

#map(to: nil, from: to, converter: nil, lazy_loading: false) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/yaoc/mapping_base.rb', line 75

def map(to: nil, from: to, converter: nil, lazy_loading: false)
  class_private_module(:Mapping).tap do |mod|
    method_implementation = converter || converter_proc(to, from, lazy_loading)

    mod.send :define_method, "map_#{"%04d" %[converter_methods.count]}_#{from}_to_#{to}".to_sym, method_implementation
    include mod
  end
end

#mapping_strategyObject



71
72
73
# File 'lib/yaoc/mapping_base.rb', line 71

def mapping_strategy
  @mapping_strategy
end

#mapping_strategy=(new_strat) ⇒ Object



67
68
69
# File 'lib/yaoc/mapping_base.rb', line 67

def mapping_strategy=(new_strat)
  @mapping_strategy = new_strat
end