Class: Pincers::Core::Helpers::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/pincers/core/helpers/query.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_backend, _lang, _query, _limit) ⇒ Query

Returns a new instance of Query.



34
35
36
37
38
39
# File 'lib/pincers/core/helpers/query.rb', line 34

def initialize(_backend, _lang, _query, _limit)
  @backend = _backend
  @lang = _lang
  @query = _query
  @limit = _limit
end

Instance Attribute Details

#langObject (readonly)

Returns the value of attribute lang.



32
33
34
# File 'lib/pincers/core/helpers/query.rb', line 32

def lang
  @lang
end

#limitObject (readonly)

Returns the value of attribute limit.



32
33
34
# File 'lib/pincers/core/helpers/query.rb', line 32

def limit
  @limit
end

#queryObject (readonly)

Returns the value of attribute query.



32
33
34
# File 'lib/pincers/core/helpers/query.rb', line 32

def query
  @query
end

Class Method Details

.build_from_options(_backend, _selector, _options = {}, &_block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pincers/core/helpers/query.rb', line 7

def self.build_from_options(_backend, _selector, _options = {}, &_block)
  limit = _options.delete(:limit)
  lang = :xpath
  exp = nil

  if _selector
    parser = Pincers::CSS::Parser.new _selector
    if parser.is_extended?
      exp = parser.to_xpath('.//') # Should we use // for root?
      exp = exp.first if exp.length == 1
    else
      lang = :css
      exp = _selector
    end
  elsif _options[:xpath]
    exp = _options[:xpath]
  else
    builder = Pincers::Support::XPathBuilder.new _options
    _block.call builder unless _block.nil?
    exp = builder.expression
  end

  self.new _backend, lang, exp, limit
end

Instance Method Details

#execute(_elements, _force_limit = nil) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/pincers/core/helpers/query.rb', line 41

def execute(_elements, _force_limit = nil)
  fun = case @lang
  when :xpath then :search_by_xpath
  else :search_by_css end

  query_elements _elements, fun, _force_limit || @limit
end