Class: Ataulfo::PatternMatching

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

Defined Under Namespace

Classes: Body, Matcher, Var

Instance Method Summary collapse

Constructor Details

#initialize(object, other_self) ⇒ PatternMatching

Returns a new instance of PatternMatching.



41
42
43
44
45
# File 'lib/ataulfo/pattern_matching.rb', line 41

def initialize(object, other_self)
  @object       = object
  @other_self   = other_self
  @context_vars = { }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_) ⇒ Object



57
58
59
60
61
62
# File 'lib/ataulfo/pattern_matching.rb', line 57

def method_missing(method_name, *_)
  # The line below also returns the "Var" to the
  # pattern. That makes it a collector parameter
  # already distributed where it belongs to.
  @context_vars[method_name] = Var.new
end

Instance Method Details

#like(pattern, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/ataulfo/pattern_matching.rb', line 47

def like(pattern, &block)
  matcher = Matcher.new(@object, pattern)
  return unless matcher.matches?

  matcher.fill_vars

  Body.new(@context_vars, @other_self).instance_eval(&block)
  @context_vars = { }
end