Class: Welo::IdentifyingMatcher

Inherits:
Matcher
  • Object
show all
Defined in:
lib/welo/core/matcher.rb

Instance Attribute Summary

Attributes inherited from Matcher

#params, #prefix

Instance Method Summary collapse

Methods inherited from Matcher

#initialize, #missing_params?, #too_many_params?, #wrong_params_set?

Constructor Details

This class inherits a constructor from Welo::Matcher

Instance Method Details

#extra_params(resource, ident = :default) ⇒ Object



49
50
51
# File 'lib/welo/core/matcher.rb', line 49

def extra_params(resource, ident=:default)
  (params.keys - identifiers_for_params_matching(resource, ident))
end

#identifiers_for_params_matching(resource, ident = :default) ⇒ Object



41
42
43
# File 'lib/welo/core/matcher.rb', line 41

def identifiers_for_params_matching(resource, ident=:default)
  resource.identifiers(ident, prefix).map{|id| id.sub(/^:/,'')}
end

#match?(resource, ident = :default) ⇒ Boolean Also known as: =~

Returns true if the params hash qualifies for identifying this resource. prefixing is possible in case the hash’s keys has prefixes e.g., Foo = Struct.new(:a, :b) do

   include Resource
   identify :default, :a, :b
 end
foo = Foo.new(1,2)
params = { 'bla.a' => 1, 'bla.b' => 2 }
Matcher.new(params, 'bla.').match?(foo, :default) 
# => true

see Resource#identifiers and identifiers_for_params_matching for more on prefix.

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
# File 'lib/welo/core/matcher.rb', line 64

def match?(resource, ident=:default)
  return false if wrong_params_set?(resource, ident)
  pairs = [resource.identify(ident), identifiers_for_params_matching(resource, ident)].transpose
  pairs.inject(true) do |bool, pair|
    sym, param_name = *pair
    bool and resource.send(sym) == params[param_name]
  end
end

#missing_params(resource, ident = :default) ⇒ Object



45
46
47
# File 'lib/welo/core/matcher.rb', line 45

def missing_params(resource, ident=:default)
  (identifiers_for_params_matching(resource, ident) - params.keys)
end