Class: DMatch

Inherits:
Object show all
Defined in:
lib/destructure/env.rb,
lib/destructure/types.rb,
lib/destructure/dmatch.rb

Defined Under Namespace

Classes: Env, FilterSplat, Obj, Or, Pred, SelectSplat, Splat, Var, Wildcard

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ DMatch

Returns a new instance of DMatch.



13
14
15
# File 'lib/destructure/dmatch.rb', line 13

def initialize(env)
  @env = env
end

Class Method Details

._Object



9
10
11
# File 'lib/destructure/dmatch.rb', line 9

def self._
  Wildcard.instance
end

.match(pat, x) ⇒ Object



5
6
7
# File 'lib/destructure/dmatch.rb', line 5

def self.match(pat, x)
  DMatch.new(Env.new).match(pat, x)
end

Instance Method Details

#match(pat, x) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/destructure/dmatch.rb', line 17

def match(pat, x)
  case
    when pat.is_a?(Wildcard); @env
    when pat.is_a?(Pred) && pat.test(x, @env); @env
    when pat.is_a?(FilterSplat); match_filter_splat(pat, x)
    when pat.is_a?(SelectSplat); match_select_splat(pat, x)
    when pat.is_a?(Splat); match_splat(pat, x)
    when pat.is_a?(Var) && pat.test(x, @env); match_var(pat, x)
    when pat.is_a?(Obj) && pat.test(x, @env) && all_field_patterns_match(pat, x); @env
    when pat.is_a?(String) && pat == x; @env
    when pat.is_a?(Regexp); match_regexp(pat, x)
    when pat.is_a?(Or); match_or(pat, x)
    when hash(pat, x) && all_keys_match(pat, x); @env
    when enumerable(pat, x); match_enumerable(pat, x)
    when pat == x; @env
    else; nil
  end
end