Class: ESBify::Parser

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

Overview

require “./strategy”

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = {}) ⇒ Parser

Returns a new instance of Parser.



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

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.



9
10
11
# File 'lib/ESBify/parser.rb', line 9

def expectation
  @expectation
end

Instance Method Details

#behaviorsObject



24
25
26
# File 'lib/ESBify/parser.rb', line 24

def behaviors
  @behavior.to_xml
end

#expectationsObject



20
21
22
# File 'lib/ESBify/parser.rb', line 20

def expectations
  @expectation.to_xml
end

#parse!(str) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ESBify/parser.rb', line 36

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

#parse_file!(path) ⇒ Object



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

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

#parse_section(str) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/ESBify/parser.rb', line 62

def parse_section(str)
  segs = str.split(/\s*\n---+\s*\n\s*/m)
  segs.map do |seg|
    ms = seg.scan /(?:^|\n)([\w_]+)\s*\:\s*(.+?)(?=(?:\n[\w_]+\s*\:|\z))/m
    Hash[*ms.flatten.map(&:strip)]
  end
end

#strategiesObject



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

def strategies
  @strategy.to_xml
end