Module: Yaoc::MappingBase::ClassMethods

Includes:
AbstractType
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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/yaoc/mapping_base.rb', line 37

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



32
33
34
# File 'lib/yaoc/mapping_base.rb', line 32

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

#map(to, from, block = nil) ⇒ Object



25
26
27
28
29
30
# File 'lib/yaoc/mapping_base.rb', line 25

def map(to, from, block=nil)
  class_private_module(:Mapping).tap do |mod|
    mod.send :define_method, "map_#{"%04d" %[converter_methods.count]}_#{from}_to_#{to}".to_sym, (block || converter_proc(to, from))
    include mod
  end
end