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

#convert_roxml_obj(obj) ⇒ Object



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

def convert_roxml_obj (obj)
  name = obj.class.class_name
  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
      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



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

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

#diff_definition(defin, obj) ⇒ Object



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

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

def eval_definitions(root_class, str = nil, &block)
  old_names = @runtime.instance_variable_get(:@classes).keys
  if block_given?
    @runtime.evaluate_raw '', root_class, &block
  else
    @runtime.evaluate_raw str, root_class
  end
  new_names = @runtime.instance_variable_get(:@classes).keys
  def_names = new_names - old_names
  @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



124
125
126
127
128
# File 'lib/roundtrip_xml/extractor.rb', line 124

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



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

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



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

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



120
121
122
# File 'lib/roundtrip_xml/extractor.rb', line 120

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



130
131
132
133
134
135
136
137
138
# File 'lib/roundtrip_xml/extractor.rb', line 130

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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/roundtrip_xml/extractor.rb', line 140

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



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/roundtrip_xml/extractor.rb', line 158

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