Class: XmlDsl::BlockMethod
- Inherits:
-
Object
- Object
- XmlDsl::BlockMethod
- Defined in:
- lib/xml_dsl/block_method.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
Instance Method Summary collapse
- #call(instance, node, parser) ⇒ Object
- #error_handle(exception, node, parser, &block) ⇒ Object
- #field(instance, node, parser, target, source = nil, getter: :text, matcher: :to_s, null: false, &block) ⇒ Object
-
#initialize(method, *args, &block) ⇒ BlockMethod
constructor
A new instance of BlockMethod.
Constructor Details
#initialize(method, *args, &block) ⇒ BlockMethod
Returns a new instance of BlockMethod.
4 5 6 7 8 |
# File 'lib/xml_dsl/block_method.rb', line 4 def initialize(method, *args, &block) @method = method @args = args @block = block if block_given? end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
3 4 5 |
# File 'lib/xml_dsl/block_method.rb', line 3 def args @args end |
#block ⇒ Object (readonly)
Returns the value of attribute block.
3 4 5 |
# File 'lib/xml_dsl/block_method.rb', line 3 def block @block end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
3 4 5 |
# File 'lib/xml_dsl/block_method.rb', line 3 def method @method end |
Instance Method Details
#call(instance, node, parser) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/xml_dsl/block_method.rb', line 10 def call(instance, node, parser) if block self.send method, *[instance, node, parser] + args, &block else self.send method, *[instance, node, parser] + args end end |
#error_handle(exception, node, parser, &block) ⇒ Object
39 40 41 |
# File 'lib/xml_dsl/block_method.rb', line 39 def error_handle(exception, node, parser, &block) parser.instance_exec exception, node, &block end |
#field(instance, node, parser, target, source = nil, getter: :text, matcher: :to_s, null: false, &block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/xml_dsl/block_method.rb', line 18 def field(instance, node, parser, target, source = nil, getter: :text, matcher: :to_s, null: false, &block) raise ArgumentError, 'Wrong target' unless target.is_a? Symbol if block_given? instance[target] = parser.instance_exec instance, node, &block else raise ArgumentError, 'No source specified' if source.nil? source = case [source.class] when [Symbol] source.to_s when [Array] source.join('/') else source.to_s end instance[target] = node.search(source).send(getter).send(matcher) end if null raise XmlDsl::ParseError, "#{target} is empty. Node: #{node}" if instance[target].nil? || instance[target] == "" end end |