Sha256: 4a86c40df9196805f7e37603b475f06783e7f4876cd0a08505eb851a84d7f2f5

Contents?: true

Size: 1.77 KB

Versions: 17

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true
module Mechanize::ElementMatcher

  def elements_with singular, plural = "#{singular}s"
    class_eval <<-CODE
      def #{plural}_with criteria = {}
        selector = method = nil
        if String === criteria then
          criteria = {:name => criteria}
        else
          criteria = criteria.each_with_object({}) { |(k, v), h|
            case k = k.to_sym
            when :id
              h[:dom_id] = v
            when :class
              h[:dom_class] = v
            when :search, :xpath, :css
              if v
                if method
                  warn "multiple search selectors are given; previous selector (\#{method}: \#{selector.inspect}) is ignored."
                end
                selector = v
                method = k
              end
            else
              h[k] = v
            end
          }
        end

        f = select_#{plural}(selector, method).find_all do |thing|
          criteria.all? do |k,v|
            v === thing.__send__(k)
          end
        end
        yield f if block_given?
        f
      end

      def #{singular}_with criteria = {}
        f = #{plural}_with(criteria).first
        yield f if block_given?
        f
      end

      def #{singular}_with! criteria = {}
        f = #{singular}_with(criteria)
        raise Mechanize::ElementNotFoundError.new(self, :#{singular}, criteria) if f.nil?
        yield f if block_given?
        f
      end

      def select_#{plural} selector, method = :search
        if selector.nil? then
          #{plural}
        else
          nodes = __send__(method, selector)
          #{plural}.find_all do |element|
            nodes.include?(element.node)
          end
        end
      end

      alias :#{singular} :#{singular}_with
    CODE
  end

end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
mechanize-2.14.0 lib/mechanize/element_matcher.rb
mechanize-2.13.0 lib/mechanize/element_matcher.rb
mechanize-2.12.2 lib/mechanize/element_matcher.rb
mechanize-2.12.1 lib/mechanize/element_matcher.rb
mechanize-2.12.0 lib/mechanize/element_matcher.rb
mechanize-2.11.0 lib/mechanize/element_matcher.rb
mechanize-2.10.1 lib/mechanize/element_matcher.rb
mechanize-2.10.0 lib/mechanize/element_matcher.rb
mechanize-2.9.2 lib/mechanize/element_matcher.rb
mechanize-2.9.1 lib/mechanize/element_matcher.rb
mechanize-2.9.0 lib/mechanize/element_matcher.rb
mechanize-2.8.5 lib/mechanize/element_matcher.rb
mechanize-2.8.4 lib/mechanize/element_matcher.rb
mechanize-2.8.3 lib/mechanize/element_matcher.rb
mechanize-2.8.2 lib/mechanize/element_matcher.rb
mechanize-2.8.1 lib/mechanize/element_matcher.rb
mechanize-2.8.0 lib/mechanize/element_matcher.rb