Sha256: 564305c7d52c2879891c37e6955b17e2a2330851ccc9c229072f8b97b5f86ced

Contents?: true

Size: 1.46 KB

Versions: 6

Compression:

Stored size: 1.46 KB

Contents

require "facets/string/camelcase"

module WildcardMatchers
  module Matchers
    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|
      define_wildcard_matcher("is_a_#{klass.to_s.downcase}") do
        klass
      end
    end

    # is_bool
    define_wildcard_matcher_with_proc(:is_bool) do |bool|
      TrueClass === bool or FalseClass === bool
    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
  end
end

require "wildcard_matchers/matchers/hash_includes"
require "wildcard_matchers/matchers/is_uri"
require "wildcard_matchers/matchers/with_uri_template"

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
wildcard_matchers-0.2.0 lib/wildcard_matchers/matchers.rb
wildcard_matchers-0.1.8 lib/wildcard_matchers/matchers.rb
wildcard_matchers-0.1.7 lib/wildcard_matchers/matchers.rb
wildcard_matchers-0.1.6 lib/wildcard_matchers/matchers.rb
wildcard_matchers-0.1.5 lib/wildcard_matchers/matchers.rb
wildcard_matchers-0.1.4 lib/wildcard_matchers/matchers.rb