Class: ESBify::Parser

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

Overview

The Parser is the top level class. It’s responsibility it is to do most of the generic processing of the language and then delegate to the Strategy, Behavior and Expectation classes.

Usage:

parser = ESBify::Parser.new
parser.parse_file! "path/to/file.esb"
puts parser.expectations #=> "<?xml ..."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = {}) ⇒ Parser

Returns a new instance of Parser.



19
20
21
22
23
24
25
# File 'lib/ESBify/parser.rb', line 19

def initialize(context = {})
  @expectation = ::ESBify::Expectation.new
  @behavior = ::ESBify::Behavior.new
  @strategy = ::ESBify::Strategy.new
  
  @context = context
end

Instance Attribute Details

#expectationObject (readonly)

Returns the value of attribute expectation.



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

def expectation
  @expectation
end

Instance Method Details

#behaviorsObject

After parsing contains the xml representation of all behaviors.



33
34
35
# File 'lib/ESBify/parser.rb', line 33

def behaviors
  @behavior.to_xml
end

#expectationsObject

After parsing contains the xml representation of all expectations.



28
29
30
# File 'lib/ESBify/parser.rb', line 28

def expectations
  @expectation.to_xml
end

#parse!(str) ⇒ Object

Parse a string of esb code.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ESBify/parser.rb', line 49

def parse!(str)
  # Remove comments
  str = str.split("\n").map{|l| l.split('#').first }.join("\n")
  # Evaluate ERB code
  str = Erubis::Eruby.new(str).evaluate(@context)
  # Split by sections
  str.split(/\!(?=(?:expectation|behaviou?r|strateg(?:y|ie))s?\n)/).each do |section|
    next if section =~ /\A\s*\z/m
    section.strip!
    m = section.match /\A!?(expectation|behaviou?r|strateg(?:y|ie))s?/
    txt = str.split("\n")
    txt.shift
    txt.join("\n")
    case m[1]
    when /expectations?/
      @expectation.parse! section
    when /behaviou?rs?/
      @behavior.parse! section
    when /strateg(?:y|ie)s?/
      @strategy.parse! section
    else
      raise ArgumentError
    end
  end
  
end

#parse_file!(path) ⇒ Object

Pass a file path. File will be read and parsed.



44
45
46
# File 'lib/ESBify/parser.rb', line 44

def parse_file!(path)
  parse! File.read(path)
end

#strategiesObject Also known as: strategy

After parsing contains the xml representation of the strategy.



38
39
40
# File 'lib/ESBify/parser.rb', line 38

def strategies
  @strategy.to_xml
end