Class: Mappum::DSL::Map

Inherits:
Object show all
Defined in:
lib/mappum/dsl.rb

Direct Known Subclasses

FieldMap, RootMap

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMap

Returns a new instance of Map.



29
30
31
# File 'lib/mappum/dsl.rb', line 29

def initialize
  @def = Mappum::Map.new
end

Instance Attribute Details

#defObject

Returns the value of attribute def.



28
29
30
# File 'lib/mappum/dsl.rb', line 28

def def
  @def
end

Instance Method Details

#`(str) ⇒ Object

Add comment to mapping.



94
95
96
97
# File 'lib/mappum/dsl.rb', line 94

def `(str) #for bad colorizers add:```
  @comment ||= ""
  @comment += str
end

#const(cst) ⇒ Object



129
130
131
# File 'lib/mappum/dsl.rb', line 129

def const(cst)
  Mappum::DSL::Constant.new(cst)        
end

#contextObject



135
136
137
138
# File 'lib/mappum/dsl.rb', line 135

def context
  fld = Mappum::DSL::Context.new
  return fld
end

#funcObject



126
127
128
# File 'lib/mappum/dsl.rb', line 126

def func
  Mappum::DSL::Function.new        
end

#map(*attr, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mappum/dsl.rb', line 32

def map(*attr, &block)
  mapa  = FieldMap.new(attr)
  mapa.def.source = @def.source
  mapa.def.src_ref = DSL.get_src_ref
  mapa.def.desc = @comment
  @comment = nil
  
  if (not mapa.def.normalized?) && block_given?
    eval_right = mapa.mpun_right.clone
    eval_right.mpun_definition.is_root = true
    eval_left = mapa.mpun_left.clone
    eval_left.mpun_definition.is_root = true
    mapa.instance_exec(eval_left, eval_right, &block)
  elsif block_given?
    mapa.def.func = block
    mapa.def.func_on_nil = true if mapa.mpun_left.kind_of?(Function)
  end 
  @def.maps += mapa.def.normalize
  @def.bidi_maps << mapa.def
  return mapa.def
end

#map_when(label, &condition) ⇒ Object

Define condition evaluated before this mapping is applyed. First argument is a label :> - left to right map :< - right to left map Second argument is a block and from elemnt is pased to the block.



60
61
62
63
64
65
66
67
# File 'lib/mappum/dsl.rb', line 60

def map_when(label,&condition)
  case label
    when :>, '>'
      @def.when_l2r = condition
    when :<, '<'
      @def.when_r2l = condition
   end 
end

#name_map(map, name) ⇒ Object

Give name to a map where map is: :<=> - bidirectional map name :> - left to right map name :< - right to left map name :prefix - add prefix to auto generated names



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mappum/dsl.rb', line 110

def name_map(map, name)
    case map
    when :<=>, '<=>'
      @def.name = name.to_s
    when :>, '>'
       @def.l2r_name = name.to_s
    when :<, '<'
       @def.r2l_name = name.to_s
    when :prefix, 'prefix'
       if name.to_s[-1..-1]=='_'
         @def.name_prefix = name.to_s 
       else
         @def.name_prefix = "#{name.to_s}_" 
       end
    end 
end

#name_map_prefix(name) ⇒ Object

Add prefix to auto map names



100
101
102
# File 'lib/mappum/dsl.rb', line 100

def name_map_prefix(name)
  name_map(:prefix, name)
end

#to_array_take(label, the_way) ⇒ Object

Define how "to" array is passed to function. First argument is a label :> - left to right map :< - right to left map :<=> - both maps Second argument is the_way and can be: :new - will create new element :first - will map to the first element :all - will map to all elements



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mappum/dsl.rb', line 79

def to_array_take(label, the_way)
  raise "the_way should be one of :new, :first, :all" unless [:new, :first, :all].include? the_way.to_sym 
  case label
    when :<=>
      @def.to_array_take_l2r = the_way.to_sym
      @def.to_array_take_r2l = the_way.to_sym
    when :>, '>'
      @def.to_array_take_l2r = the_way.to_sym
    when :<, '<'
      @def.to_array_take_r2l = the_way.to_sym
   end 
end

#tree(clazz) ⇒ Object



132
133
134
# File 'lib/mappum/dsl.rb', line 132

def tree(clazz)
  return Mappum::DSL::Field.new(nil, nil, clazz)
end