Class: Outcome

Inherits:
Object
  • Object
show all
Defined in:
lib/Olib/pattern_matching/outcome.rb

Constant Summary collapse

HANDLEBARS =
%r[{{(?<var>.*?)}}]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ Outcome

Returns a new instance of Outcome.



27
28
29
# File 'lib/Olib/pattern_matching/outcome.rb', line 27

def initialize(template)
  @template = template
end

Instance Attribute Details

#templateObject (readonly)

Returns the value of attribute template.



25
26
27
# File 'lib/Olib/pattern_matching/outcome.rb', line 25

def template
  @template
end

Class Method Details

.prepare(template, vars) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/Olib/pattern_matching/outcome.rb', line 10

def self.prepare(template, vars)
  template.gsub(HANDLEBARS) do |name|
    name = name.match(HANDLEBARS).to_struct.var.to_sym
    if vars.respond_to?(name)  
      vars.send(name) 
    elsif vars.respond_to?(:fetch) 
      vars.fetch(name)
    elsif vars.respond_to?(:[])
      vars[name]
    else
      raise Exception.new "could not serialize var: #{name} of #{vars.class.name} in Outcome"
    end
  end
end

.union(outcomes, **vars) ⇒ Object



6
7
8
# File 'lib/Olib/pattern_matching/outcome.rb', line 6

def self.union(outcomes, **vars)
  Regexp.union(outcomes.map do |outcome| outcome.prepare(**vars) end)
end

Instance Method Details

#prepare(vars) ⇒ Object



31
32
33
34
# File 'lib/Olib/pattern_matching/outcome.rb', line 31

def prepare(vars)
  return @template if @template.is_a?(Regexp)
  %r[#{Outcome.prepare(@template, vars)}]
end