Sha256: 45e115823959bb91ce776edd69717eceaad6a641770841c8c7206a26bbc1e749

Contents?: true

Size: 1.07 KB

Versions: 33

Compression:

Stored size: 1.07 KB

Contents

# frozen-string-literal: true

#
class Roda
  module RodaPlugins
    # The hash_matcher plugin adds the hash_matcher class method, which
    # allows for easily defining hash matchers:
    #
    #   class App < Roda
    #     hash_matcher(:foo) do |v|
    #       self['foo'] == v
    #     end
    #
    #     route do
    #       r.on foo: 'bar' do
    #         # matches when param foo has value bar
    #       end
    #     end
    #   end
    module HashMatcher
      module ClassMethods
        # Create a match_#{key} method in the request class using the given
        # block, so that using a hash key in a request match method will
        # call the block.  The block should return nil or false to not
        # match, and anything else to match.  See the HashMatcher module
        # documentation for an example.
        def hash_matcher(key, &block)
          meth = :"match_#{key}"
          self::RodaRequest.send(:define_method, meth, &block)
          self::RodaRequest.send(:private, meth)
        end
      end
    end

    register_plugin(:hash_matcher, HashMatcher)
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
roda-3.31.0 lib/roda/plugins/hash_matcher.rb
roda-3.30.0 lib/roda/plugins/hash_matcher.rb
roda-3.29.0 lib/roda/plugins/hash_matcher.rb
roda-3.28.0 lib/roda/plugins/hash_matcher.rb
roda-3.27.0 lib/roda/plugins/hash_matcher.rb
roda-3.26.0 lib/roda/plugins/hash_matcher.rb
roda-3.25.0 lib/roda/plugins/hash_matcher.rb
roda-3.24.0 lib/roda/plugins/hash_matcher.rb
roda-3.23.0 lib/roda/plugins/hash_matcher.rb
roda-3.22.0 lib/roda/plugins/hash_matcher.rb
roda-3.21.0 lib/roda/plugins/hash_matcher.rb
roda-3.20.0 lib/roda/plugins/hash_matcher.rb
roda-3.19.0 lib/roda/plugins/hash_matcher.rb
roda-3.18.0 lib/roda/plugins/hash_matcher.rb
roda-3.17.0 lib/roda/plugins/hash_matcher.rb
roda-3.16.0 lib/roda/plugins/hash_matcher.rb
roda-3.15.0 lib/roda/plugins/hash_matcher.rb
roda-3.14.1 lib/roda/plugins/hash_matcher.rb
roda-3.14.0 lib/roda/plugins/hash_matcher.rb
roda-3.13.0 lib/roda/plugins/hash_matcher.rb