Class: Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/roundtrip_xml/extractor.rb

Instance Method Summary collapse

Constructor Details

#initialize(roxml_objs, runtime, root_class, definitions = nil, &block) ⇒ Extractor

Returns a new instance of Extractor.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/roundtrip_xml/extractor.rb', line 13

def initialize(roxml_objs, runtime, root_class, definitions = nil, &block)
  @roxml_objs = roxml_objs
  @runtime = runtime
  @root_class = root_class
  @definitions = {}
  if block_given?
    eval_definitions root_class, &block
  else
    definitions.each {|defin| eval_definitions root_class, defin } if definitions
  end
end

Instance Method Details

#add_old_metadata(obj, old_metadata, current_metadata) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/roundtrip_xml/extractor.rb', line 123

def (obj, , )
  # only keep the keys that don't exist in the template's metadata (current)
  # that metadata will be availabled when evaluate the roxml object
  # obj._metadata = old_metadata.merge current_metadata
  diff_keys = .keys.inject([]) do |out, key|
    out << key unless .key? key
    out
  end
   = .delete_if {|k, _| !diff_keys.include? k}
  obj. = 
end

#convert_roxml_obj(obj) ⇒ Object



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
# File 'lib/roundtrip_xml/extractor.rb', line 89

def convert_roxml_obj (obj)
  name = obj.class.class_name
   = obj.
  defs = @definitions[name]

  defs.each do |defin|
    diffs = diff_definition(defin, obj)
    if diffs.all? {|diff| diff.operation == '~' && (diff.template_val.is_a?(Utils::UndefinedParam) || diff.template_val == nil || interpolated_diff(diff)) }
      new_obj = defin.class.new
      param_values = diffs.inject({}) do |values, diff|
        name = diff.template_val ? diff.template_val.name : diff.key
        values[name] = interpolated_diff(diff) ? extract_interpolated_diff(diff) : diff.obj_val
        values
      end
      set_attributes new_obj, param_values
      # return convert_roxml_obj new_obj
       new_obj, , defin.
      obj = new_obj
    end
  end if defs

  obj.class.roxml_attrs.each do |a|
    if a.array?
      elements = a.sought_type.class == Class ?
        obj.send(a.accessor).map {|el| convert_roxml_obj el} : obj.send(a.accessor)
      obj.send a.setter.to_sym, elements
    elsif a.sought_type.class == Class
      current_value = obj.send(a.accessor)
      obj.send a.setter.to_sym, convert_roxml_obj(current_value) if current_value
    end
  end
  obj
end

#convert_roxml_objsObject



82
83
84
85
86
87
# File 'lib/roundtrip_xml/extractor.rb', line 82

def convert_roxml_objs
  def_names = @definitions.keys
  @roxml_objs.map do |obj|
    convert_roxml_obj obj
  end
end

#diff_definition(defin, obj) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/roundtrip_xml/extractor.rb', line 56

def diff_definition(defin, obj)
  def_hash = defin.to_hash
  obj_hash = obj.to_hash
  [def_hash, obj_hash].each do |h|
    h.delete '__class'
  end
  diffs = HashDiff.diff(obj_hash, def_hash).map do |diff|
    accessor_name = if diff[3].is_a?(String) && match = diff[3].match(/#{Utils::UNDEFINED_PARAM}:(\w*)/)
                      match[1].to_sym
                    end
    if accessor_name && defin.class.plain_accessors.include?(accessor_name)
      matcher = defin.class.plain_accessors(true)[accessor_name]
    end
    template_val = diff[3].is_a?(String) && accessor_name ? Utils::UndefinedParam.new(accessor_name, diff[3]) : diff[3]
    ROXMLDiff.new diff[0], diff[1], from_hash(diff[2]), template_val, matcher
  end

  diffs
end

#eval_definitions(root_class, str = nil, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/roundtrip_xml/extractor.rb', line 25

def eval_definitions(root_class, str = nil, &block)
  if block_given?
    @runtime.evaluate_raw '', root_class, &block
  else
    @runtime.evaluate_raw str, root_class
  end
  def_names = @runtime.instance_variable_get(:@classes).inject([]) do |out, (name, clazz)|
    out << name if clazz.subclass?
    out
  end
  @definitions = def_names.inject(@definitions) do |out, name|
    clazz = @runtime.fetch(name)
    parent = get_root_class clazz
    obj = clazz.new
    cleanroom = @runtime.create_cleanroom clazz, true
    cleanroom.get_el.process.each { |p| cleanroom.evaluate &p }
    out[parent] ||= []
    out[parent] << cleanroom.get_el
    out
  end
end

#extract_interpolated_diff(diff) ⇒ Object



139
140
141
142
143
# File 'lib/roundtrip_xml/extractor.rb', line 139

def extract_interpolated_diff(diff)
  s_diff = Differ.diff_by_word(diff.obj_val, diff.template_val.original).to_s
  value = s_diff.match(/(\{"\s*#{Utils::UNDEFINED_PARAM}:([\S]*)\s*" >> "(.*)"})/)[3]
  value.strip.match(diff.matcher)[1]
end

#from_hash(hash) ⇒ Object



76
77
78
79
80
# File 'lib/roundtrip_xml/extractor.rb', line 76

def from_hash(hash)
  return hash unless hash.is_a? Hash
  @runtime.fetch(hash['__class']).from_hash @runtime, hash

end

#get_root_class(clazz) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/roundtrip_xml/extractor.rb', line 47

def get_root_class(clazz)
  super_clazz = @runtime.fetch clazz.superclass.class_name
  if super_clazz.subclass?
    get_root_class super_clazz
  else
    super_clazz.class_name
  end
end

#interpolated_diff(diff) ⇒ Object



135
136
137
# File 'lib/roundtrip_xml/extractor.rb', line 135

def interpolated_diff(diff)
  diff.obj_val.is_a?(String) && diff.template_val.is_a?(Utils::UndefinedParam) && diff.template_val.original && diff.template_val.original.match(/#{Utils::UNDEFINED_PARAM}:/)
end

#set_attributes(obj, params) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/roundtrip_xml/extractor.rb', line 145

def set_attributes(obj, params)
  params.each do |param, val|
    methods = param.to_s.split '.'
    methods << val
    set_deep_attributes obj, methods
    # obj.send "#{param}=", val
  end

end

#set_deep_attributes(obj, methods) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/roundtrip_xml/extractor.rb', line 155

def set_deep_attributes(obj, methods)
  index = methods[0].match(/\[(\d+)\]/)
  child = index ? nil : obj.send(methods[0])
  method = methods[0]
  method.gsub!(/\[\d+\]/, '')
  if child
    set_deep_attributes child, methods[1..methods.size]
  else
    if index
      arr = obj.send(method) || []
      arr[Integer(index[1])] = methods[1..1][0] # hacky way to get second element
      obj.send("#{method}=", arr)
    else
      obj.send(methods[0] + '=', set_deep_attributes_helper(obj, methods))
    end
  end
end

#set_deep_attributes_helper(obj, methods) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/roundtrip_xml/extractor.rb', line 173

def set_deep_attributes_helper(obj, methods)
  method = methods.shift
  index = method.match(/\[(\d+)\]/)
  return obj.send(method + '=', methods.first) if !index && methods.size == 1

  method.gsub!(/\[\d+\]/, '')
  child = obj.send(method)
  unless child || methods.size == 1
    clazz_name = method.dup
    clazz_name[0] = clazz_name[0].upcase
    child = @runtime.fetch(clazz_name.to_sym).new
    obj.send("#{method}=", child)

  end
  if index
    arr = obj.send(method) || []
    arr[Integer(index[1])] = methods.first
    obj.send("#{method}=", arr)
  else
    obj.send(method + '=', set_deep_attributes_helper(child, methods))
  end

  child.is_a?(Array) ? obj : child || obj
end