Class: Baldr::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/baldr/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, params = {}) ⇒ Parser

Returns a new instance of Parser.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/baldr/parser.rb', line 5

def initialize(input, params = {})
  @input = input
  @grammar = params[:grammar]
  @separators = params[:separators]
  
  parse

  if params[:skip_validation] != true && successful?
    validate
  end
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



3
4
5
# File 'lib/baldr/parser.rb', line 3

def error
  @error
end

#grammarObject (readonly)

Returns the value of attribute grammar.



3
4
5
# File 'lib/baldr/parser.rb', line 3

def grammar
  @grammar
end

#inputObject (readonly)

Returns the value of attribute input.



3
4
5
# File 'lib/baldr/parser.rb', line 3

def input
  @input
end

#root_typeObject (readonly)

Returns the value of attribute root_type.



3
4
5
# File 'lib/baldr/parser.rb', line 3

def root_type
  @root_type
end

#separatorsObject (readonly)

Returns the value of attribute separators.



3
4
5
# File 'lib/baldr/parser.rb', line 3

def separators
  @separators
end

Instance Method Details

#envelopesObject



21
22
23
24
25
26
27
# File 'lib/baldr/parser.rb', line 21

def envelopes
  if @root_type == :envelope
    @roots
  else
    raise Baldr::Error, "interchange doesn't have envelopes"
  end
end

#successful?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/baldr/parser.rb', line 17

def successful?
  @error.nil?
end

#transactionsObject



29
30
31
32
33
34
35
# File 'lib/baldr/parser.rb', line 29

def transactions
  if @root_type == :transaction
    @roots
  else
    @roots.map(&:transactions).flatten
  end
end

#validateObject



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

def validate
  if @roots
    @roots.each { |e| Baldr::Validator.validate!(e, @grammar) }
    @roots.each { |e| Baldr::Types.convert_after_parse!(e, @grammar) }
  end
rescue Baldr::Error => e
  @error = e.message
end