Class: Braai::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/braai/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, matchers, attributes = {}) ⇒ Context

Returns a new instance of Context.



7
8
9
10
11
# File 'lib/braai/context.rb', line 7

def initialize(template, matchers, attributes = {})
  self.attributes = HashWithIndifferentAccess.new(attributes)
  self.template = template.dup
  self.matchers = matchers
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



3
4
5
# File 'lib/braai/context.rb', line 3

def attributes
  @attributes
end

#matchersObject

Returns the value of attribute matchers.



5
6
7
# File 'lib/braai/context.rb', line 5

def matchers
  @matchers
end

#templateObject

Returns the value of attribute template.



4
5
6
# File 'lib/braai/context.rb', line 4

def template
  @template
end

Instance Method Details

#renderObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/braai/context.rb', line 13

def render
  begin
    self.matchers.each do |regex, matcher|
      regex = Regexp.new(regex)
      matches = self.template.scan(regex)
      matches.each do |set|
        set = [set].flatten.map {|m| m.strip unless m.nil? }
        val = matcher.call(self, set[0], set)
        self.template.gsub!(set[0], val.to_s) if val
      end
    end
    return self.template
  rescue Exception => e
    puts e.inspect
    Braai.logger.error(e)
    raise e unless Braai.config.swallow_matcher_errors
  end
end