Sha256: 8e5324240c87ca7bc238a7480f2e4323570f1e05f4beabf2dafba6101f26318c

Contents?: true

Size: 1.1 KB

Versions: 22

Compression:

Stored size: 1.1 KB

Contents

module BBK
  module App
    module Matchers
      class Base

        attr_reader :rule

        def hash
          (self.class.to_s + rule.to_s).hash
        end

        def ==(other)
          self.class == other.class && rule == other.rule
        end

        def eql?(other)
          self == other
        end

        def keys_deep(data)
          data.inject([]) do |res, p|
            k, _v = p
            res.push k
            res += keys_deep(data[k]) if data[k].is_a? Hash
            res
          end
        end

        def match_impl(rule, data)
          result = rule.each_with_object({}.with_indifferent_access) do |p, res|
            k, v = p

            if v == :any && data.key?(k.to_sym)
              res[k.to_sym] = data[k.to_sym]
            elsif v.is_a? Hash
              res[k.to_sym] = match_impl(v, data[k.to_sym] || {})
            elsif v == data[k.to_sym]
              res[k.to_sym] = data[k.to_sym]
            end
          end

          result.keys.size == rule.keys.size && keys_deep(result).count >= keys_deep(rule).count ? result : nil
        end


      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
bbk-app-1.1.1.273627 lib/bbk/app/matchers/base.rb
bbk-app-1.1.1.273338 lib/bbk/app/matchers/base.rb
bbk-app-1.1.1.273312 lib/bbk/app/matchers/base.rb
bbk-app-1.1.1.273294 lib/bbk/app/matchers/base.rb
bbk-app-1.1.0.219406 lib/bbk/app/matchers/base.rb
bbk-app-1.1.0.216998 lib/bbk/app/matchers/base.rb
bbk-app-1.1.0.204569 lib/bbk/app/matchers/base.rb
bbk-app-1.1.0.200751 lib/bbk/app/matchers/base.rb
bbk-app-1.1.0.200186 lib/bbk/app/matchers/base.rb
bbk-app-1.1.0.199675 lib/bbk/app/matchers/base.rb
bbk-app-1.1.0.199604 lib/bbk/app/matchers/base.rb
bbk-app-1.1.0.199389 lib/bbk/app/matchers/base.rb
bbk-app-1.1.0.199383 lib/bbk/app/matchers/base.rb
bbk-app-1.0.0.152254 lib/bbk/app/matchers/base.rb
bbk-app-1.0.0.141716 lib/bbk/app/matchers/base.rb
bbk-app-1.0.0.80957 lib/bbk/app/matchers/base.rb
bbk-app-1.0.0.79514 lib/bbk/app/matchers/base.rb
bbk-app-1.0.0.79241 lib/bbk/app/matchers/base.rb
bbk-app-1.0.0.78020 lib/bbk/app/matchers/base.rb
bbk-app-1.0.0.72920 lib/bbk/app/matchers/base.rb