Class: PatternMatch::ValuePattern

Inherits:
PatternElement show all
Defined in:
lib/egison/core.rb

Defined Under Namespace

Classes: BindingModule

Instance Attribute Summary

Attributes inherited from Pattern

#quantified

Instance Method Summary collapse

Methods inherited from Pattern

#to_a

Constructor Details

#initialize(ctx, expr) ⇒ ValuePattern



153
154
155
156
157
# File 'lib/egison/core.rb', line 153

def initialize(ctx, expr)
  super()
  @ctx = ctx
  @expr = expr
end

Instance Method Details

#binding_module(obj) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/egison/core.rb', line 194

def binding_module(obj)
  m = obj.singleton_class.ancestors.find {|i| i.kind_of?(BindingModule) }
  unless m
    m = BindingModule.new
    obj.singleton_class.class_eval do
      if respond_to?(:prepend, true)
        prepend m
      else
        include m
      end
    end
  end
  m
end

#match(tgt, bindings) ⇒ Object



159
160
161
162
163
164
165
166
# File 'lib/egison/core.rb', line 159

def match(tgt, bindings)
  val = with_bindings(@ctx, bindings, {:expr => @expr}) { eval expr }
  if val.__send__(:===, tgt) then
    [[[], []]]
  else
    []
  end
end

#with_bindings(obj, bindings, ext_bindings, &block) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/egison/core.rb', line 171

def with_bindings(obj, bindings, ext_bindings, &block)
  binding_module(obj).module_eval do
    begin
      bindings.each do |name, val|
        define_method(name) { val }
        private name
      end
      ext_bindings.each do |name, val|
        define_method(name) { val }
        private name
      end
      obj.instance_eval(&block)
    ensure
      bindings.each do |name, _|
        remove_method(name)
      end
      ext_bindings.each do |name, _|
        remove_method(name)
      end
    end
  end
end