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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/yaoc/mapping_base.rb', line 64

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



59
60
61
# File 'lib/yaoc/mapping_base.rb', line 59

def converter_methods
  @converter_methods ||= []
end

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



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/yaoc/mapping_base.rb', line 45

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

    method_name = "map_#{"%04d" % [converter_methods.count]}_#{from}_to_#{to}".to_sym
    converter_methods << method_name

    converter_methods.sort!.uniq!

    mod.send :define_method, method_name, method_implementation
    include mod
  end
end

#mapping_strategyObject



41
42
43
# File 'lib/yaoc/mapping_base.rb', line 41

def mapping_strategy
  @mapping_strategy
end

#mapping_strategy=(new_strat) ⇒ Object



37
38
39
# File 'lib/yaoc/mapping_base.rb', line 37

def mapping_strategy=(new_strat)
  @mapping_strategy = new_strat
end