lib/wildcard_matchers/matchers.rb in wildcard_matchers-0.1.1 vs lib/wildcard_matchers/matchers.rb in wildcard_matchers-0.1.2
- old
+ new
@@ -1,29 +1,56 @@
+require "facets/string/camelcase"
+
module WildcardMatchers
module Matchers
- def is_a(klass)
+ class << self
+ def define_wildcard_matcher(name, &block)
+ if block_given?
+ define_method(name, &block)
+ else
+ define_method(name) do |*args|
+ ::WildcardMatchers::Matchers.const_get(name.to_s.camelcase(:upper)).new(args)
+ end
+ end
+ end
+
+ def define_wildcard_matcher_with_proc(name, &block)
+ raise "no block defined" unless block_given?
+
+ define_method(name) do
+ block
+ end
+ end
+ end
+
+ # is_a(klass)
+ define_wildcard_matcher(:is_a) do |klass|
klass
end
+ # is_a_string
+ # is_a_integer
+ # is_a_float
+ # is_a_hash
+ # is_a_array
[ String, Integer, Float, Hash, Array ].each do |klass|
- module_eval %{
- def is_a_#{klass.to_s.downcase}
- #{klass.inspect}
- end
- }
+ define_wildcard_matcher("is_a_#{klass.to_s.downcase}") do
+ klass
+ end
end
- def is_bool
- lambda {|bool| TrueClass === bool or FalseClass === bool }
+ # is_bool
+ define_wildcard_matcher_with_proc(:is_bool) do |bool|
+ TrueClass === bool or FalseClass === bool
end
- def is_time
- lambda do |time|
- require "time"
- time.is_a?(Time) or (Time.parse(time) rescue false)
- end
+ # is_time
+ define_wildcard_matcher_with_proc(:is_time) do |time|
+ require "time"
+ time.is_a?(Time) or (Time.parse(time) rescue false)
end
+ # TODO: DSL
def is_a_member_of(*args)
lambda do |item|
args.flatten.any? {|expected| expected === item }
end
end