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

#before_parse?(node, parser, key = nil, &block) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
# File 'lib/xml_dsl/block_method.rb', line 37

def before_parse?(node, parser, key = nil, &block)
  if block_given?
    parser.instance_exec node, &block
  else
    raise ArgumentError, 'Key to check is nil' if key.nil?
    !node.search(convert_source(key)).empty?
  end
end

#call(a, b = nil, c = nil) ⇒ Object

order of params to be called can differ, so naming is ambiguous



11
12
13
14
15
16
17
# File 'lib/xml_dsl/block_method.rb', line 11

def call(a, b = nil, c = nil)
  if block
    self.send method, *[a, b, c].compact + args, &block
  else
    self.send method, *[a, b, c].compact + args
  end
end

#error_handle(exception, node, parser, &block) ⇒ Object



33
34
35
# File 'lib/xml_dsl/block_method.rb', line 33

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)


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/xml_dsl/block_method.rb', line 19

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
  wrapped_instance = XmlDsl::InstanceWrapper.new instance
  if block_given?
    wrapped_instance.set target, parser.instance_exec(instance, node, &block)
  else
    raise ArgumentError, 'No source specified' if source.nil?
    wrapped_instance.set target,  node.search(convert_source(source)).send(getter).send(matcher)
  end
  if null
    raise XmlDsl::ParseError, "#{target} is empty. Node: #{node}" if instance[target].nil? || instance[target] == ""
  end
end