Class: Extractor2
- Inherits:
-
Object
show all
- Includes:
- Utils
- Defined in:
- lib/roundtrip_xml/extractor2.rb
Overview
extracts duplicate attributes from ROXML objects
Constant Summary
Constants included
from Utils
Utils::VAR_SUFIX
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Utils
included, #name_to_sym, #new_roxml_class
Constructor Details
#initialize(roxml_objs, runtime) ⇒ Extractor2
Returns a new instance of Extractor2.
14
15
16
17
18
19
20
|
# File 'lib/roundtrip_xml/extractor2.rb', line 14
def initialize(roxml_objs, runtime)
@roxml_objs = roxml_objs
@hashes = @roxml_objs.map { |obj| obj.to_hash }
@runtime = runtime
@subclass_names = Multiset.new
@subclasses = []
end
|
Instance Attribute Details
#subclasses ⇒ Object
Returns the value of attribute subclasses.
13
14
15
|
# File 'lib/roundtrip_xml/extractor2.rb', line 13
def subclasses
@subclasses
end
|
Instance Method Details
#convert_roxml(obj, keys, class_name) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/roundtrip_xml/extractor2.rb', line 66
def convert_roxml(obj, keys, class_name)
parent = @runtime.fetch(class_name)
new = parent.new
parent.plain_accessors.each do |new_accessor|
old_accessor = new_accessor.to_s.gsub(VAR_SUFIX, '').to_sym
new.send "#{new_accessor}=", obj.send(old_accessor)
end
keys.each do |accessor|
new.send "#{accessor}=", obj.send(accessor)
end
new
end
|
#convert_roxml_objs ⇒ Object
80
81
82
83
84
85
86
87
|
# File 'lib/roundtrip_xml/extractor2.rb', line 80
def convert_roxml_objs
fields, diff_keys = similar_fields
parent_name = @roxml_objs[0].class.class_name
name = create_romxl_class fields, parent_name
@roxml_objs.map do |roxml|
convert_roxml roxml, diff_keys, name
end
end
|
#create_romxl_class(keys, parent) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/roundtrip_xml/extractor2.rb', line 54
def create_romxl_class(keys, parent)
@subclass_names << parent
name = "#{parent.to_s}#{@subclass_names.count(parent)}"
clazz = new_roxml_class name, @runtime.fetch(parent) do
include RoxmlSubclassHelpers
end
clazz.defaults = keys
@runtime.add_class name, clazz
@subclasses << clazz
name
end
|
#list_diffs ⇒ Object
21
22
23
24
25
26
27
|
# File 'lib/roundtrip_xml/extractor2.rb', line 21
def list_diffs
HashDiff.best_diff(@hashes[0], @hashes[6]).sort_by do |diff|
m = diff[1].match(/\./)
m ? m.size : 0
end.map {|diff| diff[1]}
end
|
#similar_fields ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/roundtrip_xml/extractor2.rb', line 29
def similar_fields
keys = Set.new @hashes[0].keys
diff_keys = Set.new
compared = {}
@hashes.each do |hash_1|
@hashes.each do |hash_2|
compared[hash_2] ||= Set.new
next if hash_1 == hash_2 || compared[hash_1].include?(hash_2)
compared[hash_2].add hash_1
diffs = HashDiff.diff(hash_1, hash_2).map do |diff|
diff[1].match(/(\w+)\.?/)[1]
end
diff_keys.merge diffs
end
end
keys = keys - diff_keys
key_hash = keys.inject({}) do |hash, key|
hash[key] = @hashes[0][key]
hash
end
[key_hash, diff_keys]
end
|