Class: XmlDsl::BlockMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/xml_dsl/block_method.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/xml_dsl/block_method.rb', line 3

def args
  @args
end

#blockObject (readonly)

Returns the value of attribute block.



3
4
5
# File 'lib/xml_dsl/block_method.rb', line 3

def block
  @block
end

#methodObject (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

Raises:

  • (ArgumentError)


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