lib/matchete.rb in matchete-0.4.0 vs lib/matchete.rb in matchete-0.5.0

- old
+ new

@@ -31,20 +31,28 @@ @default_methods[method_name] = instance_method(method_name) convert_to_matcher method_name end # Matches something like sum types: - # either(Integer, Array) + # 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 property results + # e.g. having('#to_s': '[]') will match [] + def having(**properties) + -> arg do + properties.all? { |prop, result| arg.respond_to?(prop[1..-1]) && arg.send(prop[1..-1]) == result } + end end # Matches each guard # full_match(Integer, '#value') # matches only instances of Integer which respond to '#value'