Module: Hamstar

Defined in:
lib/hamstar.rb,
lib/hamstar/version.rb

Constant Summary collapse

KLEENE_STAR =
'*'
VERSION =
"0.0.8"

Class Method Summary collapse

Class Method Details

.match(matcher, c, *match_path, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/hamstar.rb', line 31

def match(matcher, c, *match_path, &block)
  mp_rest = Hamster.from(match_path)[1..-1] # drop first expr
  c.each_pair do |key,value|
    if matcher.call key, value
      mp = mp_rest.unshift key # put key where expr was
      c = update_having c, *mp, &block
    end
  end
  c
end

.update_having(c, *match_path, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hamstar.rb', line 11

def update_having(c, *match_path, &block)
  if match_path.empty?
    raise ArgumentError, "must have at least one matcher in path"
  end
  matcher = match_path[0]
  case matcher
  when KLEENE_STAR; match ->(k,v){true}, c, *match_path, &block
  when Array, Hamster::Vector; match ->(k,v){key,value=matcher; value === v[key]}, c, *match_path, &block
  when Proc; match matcher, c, *match_path, &block
  else
    if match_path.size == 1
      new_value = block.call c.fetch(matcher,nil)
    else
      value = c.fetch matcher, Hamster::EmptyHash
      new_value = update_having value, *match_path[1..-1], &block
    end
    c.put matcher, new_value
  end
end