Class: Decontaminate::Decontaminator
- Inherits:
-
Object
- Object
- Decontaminate::Decontaminator
- Defined in:
- lib/decontaminate/decontaminator.rb
Overview
Decontaminate::Decontaminator is the base class for creating XML extraction parsers. A DSL is exposed via class methods to allow specifying how the XML should be parsed.
Class Attribute Summary collapse
-
.decoders ⇒ Object
readonly
Returns the value of attribute decoders.
-
.root ⇒ Object
Returns the value of attribute root.
Instance Attribute Summary collapse
-
#xml_node ⇒ Object
readonly
Returns the value of attribute xml_node.
Class Method Summary collapse
- .add_decoder(key, decoder) ⇒ Object
- .hash(xpath = '.', key: infer_key(xpath), &body) ⇒ Object
- .hashes(xpath = nil, path: nil, key: nil, &body) ⇒ Object
- .infer_key(xpath) ⇒ Object
- .infer_plural_path(xpath) ⇒ Object
- .inherited(subclass) ⇒ Object
- .scalar(xpath, type: :string, key: infer_key(xpath), transformer: nil, &block) ⇒ Object
- .scalars(xpath = nil, path: nil, type: :string, key: nil, transformer: nil, &block) ⇒ Object
- .tuple(paths, key:, type: :string, transformer: nil, &block) ⇒ Object
- .with(xpath, &body) ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(xml_node, instance: nil) ⇒ Decontaminator
constructor
A new instance of Decontaminator.
Constructor Details
#initialize(xml_node, instance: nil) ⇒ Decontaminator
Returns a new instance of Decontaminator.
115 116 117 118 |
# File 'lib/decontaminate/decontaminator.rb', line 115 def initialize(xml_node, instance: nil) @xml_node = xml_node @instance = instance end |
Class Attribute Details
.decoders ⇒ Object (readonly)
Returns the value of attribute decoders.
16 17 18 |
# File 'lib/decontaminate/decontaminator.rb', line 16 def decoders @decoders end |
.root ⇒ Object
Returns the value of attribute root.
15 16 17 |
# File 'lib/decontaminate/decontaminator.rb', line 15 def root @root end |
Instance Attribute Details
#xml_node ⇒ Object (readonly)
Returns the value of attribute xml_node.
113 114 115 |
# File 'lib/decontaminate/decontaminator.rb', line 113 def xml_node @xml_node end |
Class Method Details
.add_decoder(key, decoder) ⇒ Object
93 94 95 96 |
# File 'lib/decontaminate/decontaminator.rb', line 93 def add_decoder(key, decoder) fail "Decoder already registered for key #{key}" if decoders.key? key decoders[key] = decoder end |
.hash(xpath = '.', key: infer_key(xpath), &body) ⇒ Object
63 64 65 66 |
# File 'lib/decontaminate/decontaminator.rb', line 63 def hash(xpath = '.', key: infer_key(xpath), &body) decontaminator = Class.new(Decontaminate::Decontaminator, &body) add_decoder key, Decontaminate::Decoder::Hash.new(xpath, decontaminator) end |
.hashes(xpath = nil, path: nil, key: nil, &body) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/decontaminate/decontaminator.rb', line 68 def hashes(xpath = nil, path: nil, key: nil, &body) resolved_path = path || infer_plural_path(xpath) key ||= infer_key(path || xpath) decontaminator = Class.new(Decontaminate::Decontaminator, &body) singular = Decontaminate::Decoder::Hash.new('.', decontaminator) decoder = Decontaminate::Decoder::Array.new(resolved_path, singular) add_decoder key, decoder end |
.infer_key(xpath) ⇒ Object
98 99 100 |
# File 'lib/decontaminate/decontaminator.rb', line 98 def infer_key(xpath) xpath.delete('@').underscore end |
.infer_plural_path(xpath) ⇒ Object
102 103 104 |
# File 'lib/decontaminate/decontaminator.rb', line 102 def infer_plural_path(xpath) xpath + '/' + xpath.split('/').last.singularize end |
.inherited(subclass) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/decontaminate/decontaminator.rb', line 18 def inherited(subclass) subclass.instance_eval do @root = '.' @decoders = {} end end |
.scalar(xpath, type: :string, key: infer_key(xpath), transformer: nil, &block) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/decontaminate/decontaminator.rb', line 25 def scalar(xpath, type: :string, key: infer_key(xpath), transformer: nil, &block) block ||= self_proc_for_method transformer add_decoder key, Decontaminate::Decoder::Scalar.new(xpath, type, block) end |
.scalars(xpath = nil, path: nil, type: :string, key: nil, transformer: nil, &block) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/decontaminate/decontaminator.rb', line 34 def scalars(xpath = nil, path: nil, type: :string, key: nil, transformer: nil, &block) resolved_path = path || infer_plural_path(xpath) key ||= infer_key(path || xpath) block ||= self_proc_for_method transformer singular = Decontaminate::Decoder::Scalar.new('.', type, block) decoder = Decontaminate::Decoder::Array.new(resolved_path, singular) add_decoder key, decoder end |
.tuple(paths, key:, type: :string, transformer: nil, &block) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/decontaminate/decontaminator.rb', line 50 def tuple(paths, key:, type: :string, transformer: nil, &block) block ||= self_proc_for_method transformer scalar = Decontaminate::Decoder::Scalar.new('.', type, nil) decoder = Decontaminate::Decoder::Tuple.new(paths, scalar, block) add_decoder key, decoder end |
.with(xpath, &body) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/decontaminate/decontaminator.rb', line 79 def with(xpath, &body) this = self decontaminator = Class.new(Decontaminate::Decontaminator) decontaminator.instance_eval do define_singleton_method :add_decoder do |key, decoder| proxy = Decontaminate::Decoder::ChildNodeProxy.new(xpath, decoder) this.add_decoder key, proxy end end decontaminator.class_eval(&body) end |
Instance Method Details
#as_json ⇒ Object
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/decontaminate/decontaminator.rb', line 120 def as_json acc = {} root_node = xml_node && xml_node.at_xpath(root) decoders.each do |key, decoder| acc[key] = decoder.decode instance, root_node end acc end |