Class: Extractor
- Inherits:
-
Object
- Object
- Extractor
- Defined in:
- lib/roundtrip_xml/extractor.rb
Instance Method Summary collapse
- #convert_roxml_obj(obj) ⇒ Object
- #convert_roxml_objs ⇒ Object
- #diff_definition(defin, obj) ⇒ Object
- #eval_definitions(root_class, str = nil, &block) ⇒ Object
- #from_hash(hash) ⇒ Object
- #get_root_class(clazz) ⇒ Object
-
#initialize(roxml_objs, runtime, root_class, definitions = nil, &block) ⇒ Extractor
constructor
A new instance of Extractor.
- #set_attributes(obj, params) ⇒ Object
Constructor Details
#initialize(roxml_objs, runtime, root_class, definitions = nil, &block) ⇒ Extractor
Returns a new instance of Extractor.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/roundtrip_xml/extractor.rb', line 12 def initialize(roxml_objs, runtime, root_class, definitions = nil, &block) @roxml_objs = roxml_objs @runtime = runtime @root_class = root_class if block_given? @definitions = eval_definitions root_class, &block else @definitions = eval_definitions root_class, definitions end end |
Instance Method Details
#convert_roxml_obj(obj) ⇒ Object
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 |
# File 'lib/roundtrip_xml/extractor.rb', line 79 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.template_val.is_a?(Utils::UndefinedParam) || diff.template_val == nil } new_obj = defin.class.new param_values = diffs.inject({}) do |values, diff| name = diff.template_val ? diff.template_val.name : diff.key values[name] = 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 = obj.send(a.accessor).map {|el| convert_roxml_obj el} 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_objs ⇒ Object
72 73 74 75 76 77 |
# File 'lib/roundtrip_xml/extractor.rb', line 72 def convert_roxml_objs def_names = @definitions.keys @roxml_objs.map do |obj| convert_roxml_obj obj end end |
#diff_definition(defin, obj) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/roundtrip_xml/extractor.rb', line 53 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| ROXMLDiff.new diff[1], from_hash(diff[2]), diff[3] end diffs end |
#eval_definitions(root_class, str = nil, &block) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/roundtrip_xml/extractor.rb', line 23 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 def_names.inject({}) 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 |
#from_hash(hash) ⇒ Object
66 67 68 69 70 |
# File 'lib/roundtrip_xml/extractor.rb', line 66 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
44 45 46 47 48 49 50 51 |
# File 'lib/roundtrip_xml/extractor.rb', line 44 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 |
#set_attributes(obj, params) ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/roundtrip_xml/extractor.rb', line 110 def set_attributes(obj, params) params.each do |param, val| methods = param.to_s.split '.' # set_deep_attribute obj, methods, val obj.send "#{param}=", val end end |