Class: Outcome
- Inherits:
-
Object
- Object
- Outcome
- Defined in:
- lib/Olib/pattern_matching/outcome.rb
Constant Summary collapse
- HANDLEBARS =
%r[{{(?<var>.*?)}}]
Instance Attribute Summary collapse
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(template) ⇒ Outcome
constructor
A new instance of Outcome.
- #prepare(vars) ⇒ Object
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
#template ⇒ Object (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 |