Sha256: 5cdfbd1690fc24e093db2f1180dded016bd4687113b85282a4220f4d47450631

Contents?: true

Size: 1.41 KB

Versions: 12

Compression:

Stored size: 1.41 KB

Contents

# encoding: utf-8
module Infoboxer
  module Navigation
    module Lookup
      # Incapsulates storage of selectors, used in {Lookup::Node node lookup}.
      #
      # See {Lookup::Node Lookup::Node} for detailed explanation of available selectors.
      class Selector
        include ProcMe
        
        def initialize(*arg, &block)
          @arg = [arg, block].flatten.compact.map(&method(:sym_to_class))
          @arg.each do |a|
            a.reject!{|k, v| v.nil?} if a.is_a?(Hash)
          end
        end

        attr_reader :arg

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

        def inspect
          "#<Selector(#{@arg.map(&:to_s).join(', ')})>"
        end

        def matches?(node)
          @arg.all?{|a| arg_matches?(a, node)}
        end

        private

        def sym_to_class(a)
          if a.is_a?(Symbol) && a =~ /^[A-Z][a-zA-Z]+$/ && Tree.const_defined?(a)
            Tree.const_get(a)
          else
            a
          end
        end

        def arg_matches?(check, node)
          case check
          when Proc
            check.call(node)
          when Hash
            check.all?{|attr, value|
              node.respond_to?(attr) && value === node.send(attr)
            }
          when Symbol
            node.respond_to?(check) && node.send(check)
          else
            check === node
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
infoboxer-0.2.7 lib/infoboxer/navigation/selector.rb
infoboxer-0.2.6 lib/infoboxer/navigation/selector.rb
infoboxer-0.2.5 lib/infoboxer/navigation/selector.rb
infoboxer-0.2.4 lib/infoboxer/navigation/selector.rb
infoboxer-0.2.3 lib/infoboxer/navigation/selector.rb
infoboxer-0.2.2 lib/infoboxer/navigation/selector.rb
infoboxer-0.2.1 lib/infoboxer/navigation/selector.rb
infoboxer-0.2.0 lib/infoboxer/navigation/selector.rb
infoboxer-0.1.2.1 lib/infoboxer/navigation/selector.rb
infoboxer-0.1.2 lib/infoboxer/navigation/selector.rb
infoboxer-0.1.1 lib/infoboxer/navigation/selector.rb
infoboxer-0.1.0 lib/infoboxer/navigation/selector.rb