Class: AccountInfoList::Block

Inherits:
Treetop::Runtime::SyntaxNode
  • Object
show all
Defined in:
lib/aq_banking/node_extensions.rb

Constant Summary collapse

DEFAULT_PATH_SEPARATOR =
' '

Instance Method Summary collapse

Instance Method Details

#as_datetimeTime

Returns:

  • (Time)


100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/aq_banking/node_extensions.rb', line 100

def as_datetime
  raise 'date must be in utc' unless get_field('inUtc').value == '1'

  Time.utc(
      get_field(%i(date year)).value.to_i,
      get_field(%i(date month)).value.to_i,
      get_field(%i(date day)).value.to_i,
      get_field(%i(time hour)).value.to_i,
      get_field(%i(time min)).value.to_i,
      get_field(%i(time sec)).value.to_i
  )
end

#blocksArray<AccountInfoList::Block>

Returns:



36
37
38
# File 'lib/aq_banking/node_extensions.rb', line 36

def blocks
  content_elements.select{|e| e.is_a? Block}
end

#fieldsArray<AccountInfoList::Block>

Returns:



31
32
33
# File 'lib/aq_banking/node_extensions.rb', line 31

def fields
  content_elements.select{|e| e.is_a? Field }
end

#get_blocks(path, separator: DEFAULT_PATH_SEPARATOR) ⇒ Object

Raises:

See Also:

  • AccountInfoList::Block.{{#query}


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/aq_banking/node_extensions.rb', line 71

def get_blocks(path, separator: DEFAULT_PATH_SEPARATOR)
  path = parse_path(path, separator: separator)

  # process first part of path
  current_path_part = path.shift
  match = current_path_part.match /^(?<name>\w+)(\[(?<index>\d+)\])?$/
  name = match[:name] or raise ArgumentError('could not read name from ' + path.first)
  index = match[:index].try(:to_i)

  # find blocks with that name
  found_blocks = blocks.select { |b| b.name.to_sym == name.to_sym }

  # put only the block at index in found_blocks if index is set
  found_blocks = [*(found_blocks[index])] if index.present?

  # return found_blocks if path is at the end or we didn't find anything
  return found_blocks if path.empty? or found_blocks.empty?

  raise AmbiguousPathError.new(name) if found_blocks.length > 1

  # recursion
  begin
    found_blocks.first.get_blocks path, separator: separator
  rescue AmbiguousPathError => e
    raise AmbiguousPathError.new([current_path_part, e.path].join(separator))
  end
end

#get_field(path, separator: DEFAULT_PATH_SEPARATOR) ⇒ Object

Raises:

See Also:

  • AccountInfoList::Block.{{#query}


56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/aq_banking/node_extensions.rb', line 56

def get_field(path, separator: DEFAULT_PATH_SEPARATOR)
  path = parse_path(path, separator: separator)
  field_name = path.pop

  final_blocks = path.length == 0 ? [self] : get_blocks(path, separator: separator)

  raise AmbiguousPathError.new(path.join(separator)) if final_blocks.length > 1

  return [] if final_blocks.empty?

  # return first matched field of final block
  final_blocks.first.fields.select { |f| f.name.to_sym == field_name.to_sym }.first
end

#inspectObject



21
22
23
# File 'lib/aq_banking/node_extensions.rb', line 21

def inspect
  "#{self.class.name}(name: #{name})"
end

#nameString

Returns:

  • (String)


26
27
28
# File 'lib/aq_banking/node_extensions.rb', line 26

def name
  elements[1].text_value
end

#query(path, separator: DEFAULT_PATH_SEPARATOR) ⇒ Array<AccountInfoList::Block>

traverses the tree to return the block or fields at the given path

e.g query “path to blocks” e.g query “path to blocks otherblock” e.g query “path/to/blocks/otherblock”, separator: ‘/’

Parameters:

  • path (String, Symbol, Array<String>)
  • separator (String) (defaults to: DEFAULT_PATH_SEPARATOR)

    split by this if given path is a String

Returns:

Raises:

  • AmbiguousPathError

  • ArgumentError if path is empty



51
52
53
# File 'lib/aq_banking/node_extensions.rb', line 51

def query(path, separator: DEFAULT_PATH_SEPARATOR)
  [*(get_field(path, separator: separator))] + get_blocks(path, separator: separator)
end