lib/matchete.rb in matchete-0.2.0 vs lib/matchete.rb in matchete-0.4.0
- old
+ new
@@ -30,14 +30,27 @@
def default(method_name)
@default_methods[method_name] = instance_method(method_name)
convert_to_matcher method_name
end
+ # Matches something like sum types:
+ # either(Integer, Array)
+ # matches both [2] and 2
def either(*guards)
-> arg { guards.any? { |g| match_guard(g, arg) } }
end
+ # Matches an exact value
+ # useful if you want to match a string starting with '#' or the value of a class
+ # exact(Integer) matches Integer, not 2
+ def exact(value)
+ -> arg { arg == value }
+ end
+
+ # Matches each guard
+ # full_match(Integer, '#value')
+ # matches only instances of Integer which respond to '#value'
def full_match(*guards)
-> arg { guards.all? { |g| match_guard(g, arg) } }
end
def supporting(*method_names)
@@ -99,15 +112,18 @@
match_guard guard, kwargs[label]
end
end
def match_guard(guard, arg)
- p
case guard
when Module
arg.is_a? guard
when Symbol
- send guard, arg
+ if guard.to_s[-1] == '?'
+ send guard, arg
+ else
+ guard == arg
+ end
when Proc
instance_exec arg, &guard
when Regexp
arg.is_a? String and guard.match arg
when Array