lib/decontaminate/decontaminator.rb in decontaminate-0.1.2 vs lib/decontaminate/decontaminator.rb in decontaminate-0.2.0
- old
+ new
@@ -2,10 +2,11 @@
require_relative 'decoder/array'
require_relative 'decoder/child_node_proxy'
require_relative 'decoder/hash'
require_relative 'decoder/scalar'
+require_relative 'decoder/tuple'
module Decontaminate
# 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.
@@ -19,20 +20,26 @@
@root = '.'
@decoders = {}
end
end
- def scalar(xpath, type: :string, key: infer_key(xpath))
- add_decoder key, Decontaminate::Decoder::Scalar.new(xpath, type)
+ def scalar(xpath, type: :string, key: infer_key(xpath), &block)
+ add_decoder key, Decontaminate::Decoder::Scalar.new(xpath, type, block)
end
- def scalars(xpath = nil, path: nil, type: :string, key: nil)
+ def scalars(xpath = nil, path: nil, type: :string, key: nil, &block)
resolved_path = path || infer_plural_path(xpath)
key ||= infer_key(path || xpath)
- singular = Decontaminate::Decoder::Scalar.new('.', type)
+ singular = Decontaminate::Decoder::Scalar.new('.', type, block)
decoder = Decontaminate::Decoder::Array.new(resolved_path, singular)
+ add_decoder key, decoder
+ end
+
+ def tuple(paths, key:, type: :string, &block)
+ scalar = Decontaminate::Decoder::Scalar.new('.', type, nil)
+ decoder = Decontaminate::Decoder::Tuple.new(paths, scalar, block)
add_decoder key, decoder
end
def hash(xpath = '.', key: infer_key(xpath), &body)
decontaminator = Class.new(Decontaminate::Decontaminator, &body)