Class: ESBify::Parser
- Inherits:
-
Object
- Object
- ESBify::Parser
- Defined in:
- lib/ESBify/parser.rb
Overview
require “./strategy”
Instance Attribute Summary collapse
-
#expectation ⇒ Object
readonly
Returns the value of attribute expectation.
Instance Method Summary collapse
- #behaviors ⇒ Object
- #expectations ⇒ Object
-
#initialize(context = {}) ⇒ Parser
constructor
A new instance of Parser.
- #parse!(str) ⇒ Object
- #parse_file!(path) ⇒ Object
- #parse_section(str) ⇒ Object
- #strategies ⇒ Object
Constructor Details
Instance Attribute Details
#expectation ⇒ Object (readonly)
Returns the value of attribute expectation.
9 10 11 |
# File 'lib/ESBify/parser.rb', line 9 def expectation @expectation end |
Instance Method Details
#behaviors ⇒ Object
24 25 26 |
# File 'lib/ESBify/parser.rb', line 24 def behaviors @behavior.to_xml end |
#expectations ⇒ Object
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 |
#strategies ⇒ Object
28 29 30 |
# File 'lib/ESBify/parser.rb', line 28 def strategies @strategy.to_xml end |