Class: Mappum::RubyTransform

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

Direct Known Subclasses

RubyXmlTransform

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map_catalogue = nil, default_struct_class = nil) ⇒ RubyTransform

Returns a new instance of RubyTransform.



26
27
28
29
30
31
# File 'lib/mappum/ruby_transform.rb', line 26

def initialize(map_catalogue = nil, default_struct_class=nil)
  @map_catalogue = map_catalogue if map_catalogue.kind_of?(Mappum::Map)
  @map_catalogue ||= Mappum.catalogue(map_catalogue)
  @default_struct_class = default_struct_class
  @default_struct_class ||= Mappum::OpenStruct;
end

Instance Attribute Details

#map_catalogueObject

Returns the value of attribute map_catalogue.



24
25
26
# File 'lib/mappum/ruby_transform.rb', line 24

def map_catalogue
  @map_catalogue
end

Instance Method Details

#get(object, field) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mappum/ruby_transform.rb', line 32

def get(object, field)
  if field.nil?
    return object
  elsif not object.kind_of?(@default_struct_class) or 
    object.respond_to?(field)
    return object.send(field)
  else
    #for open structures field will be defined later
    return nil
  end
end

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mappum/ruby_transform.rb', line 43

def transform(from, map=nil, to=nil)

  map ||= @map_catalogue[from.class]
  
  raise MapMissingException.new(from) if map.nil?
  
  to ||= map.to.clazz.new unless map.to.clazz.nil? or map.to.clazz.kind_of?(Symbol)
  
  all_nils = true

  map.maps.each do |sm|
    from_value, to_value = nil, nil
    
    if sm.from.respond_to?(:name)
       from_value = get(from, sm.from.name) 
    else
       from_value = sm.from.value
    end
    
    if sm.maps.empty?
      to_value = from_value
    elsif not from_value.nil?
      if from_value.instance_of?(Array)
        sm_v = sm.clone
        sm_v.from.is_array = false
        sm_v.to.is_array = false
        to_value = from_value.collect{|v| transform(v, sm_v)}
      else
        to ||= @default_struct_class.new
        v_to = nil
        #array values are assigned after return
        v_to = get(to, sm.to.name) unless sm.to.is_array and not sm.from.is_array
        to_value = transform(from_value, sm, v_to)
      end

    end
    unless sm.func.nil? or (not sm.func_on_nil? and to_value.nil?)
      to_value = sm.func.call(to_value)
    end
    unless sm.from.func.nil? or to_value.nil?
      to_value = to_value.instance_eval(sm.from.func)
    end
    unless sm.dict.nil?
      to_value = sm.dict[to_value]
    end
    if sm.to.is_array and not sm.from.is_array
      to_array = get(to,sm.to.name)
      to_array ||= []
      to_array << to_value
      
      if to_array.empty? and sm.strip_empty?
        to_array = nil
      end
      
      all_nils = false unless to_array.nil?

      if sm.to.name.nil?
        to = to_array
      else
        to ||= @default_struct_class.new
        to.send("#{sm.to.name}=", to_array) unless to_array.nil?
      end
    else

      if to_value.respond_to?(:empty?) and to_value.empty? and sm.strip_empty?
        to_value = nil
      end
      
      all_nils = false unless to_value.nil?
                
      if sm.to.name.nil?
        to ||= to_value
      elsif
        to ||= @default_struct_class.new 
        
        to.send("#{sm.to.name}=", to_value) unless to_value.nil?
      end
    end
    
  end
  if all_nils and map.strip_empty?
    return nil
  end
  return to
end