Class: Elementor::Result

Inherits:
Object show all
Defined in:
lib/elementor/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, opts = {}, &block) ⇒ Result

Returns a new instance of Result.



8
9
10
11
12
13
14
# File 'lib/elementor/result.rb', line 8

def initialize(context, opts={}, &block)
  @opts = opts
  @context = context
  @doc_ready = false
  block.call(naming_context)
  define_elements!
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/elementor/result.rb', line 6

def context
  @context
end

#doc_ready=(value) ⇒ Object (writeonly)

Sets the attribute doc_ready

Parameters:

  • value

    the value to set the attribute doc_ready to.



5
6
7
# File 'lib/elementor/result.rb', line 5

def doc_ready=(value)
  @doc_ready = value
end

#optsObject (readonly)

Returns the value of attribute opts.



6
7
8
# File 'lib/elementor/result.rb', line 6

def opts
  @opts
end

Instance Method Details

#dispatcherObject

Returns a blank slate object that delegates to either an instance of Result or the original Nokogiri doc.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/elementor/result.rb', line 24

def dispatcher
  @dispatcher ||= blank_context(:this => self) do
    def method_missing(sym, *args, &block)
      @this.doc_ready!
      [@this, @this.doc].each do |context|
        next unless context.respond_to?(sym)
        return context.send(sym, *args, &block)
      end
      super # raise NoMethodError if no context can handle
    end
  end
end

#doc(markup = nil) ⇒ Object

Returns the raw Nokogiri doc once a method has been called on the dispatcher. Up until that point, returns nil.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/elementor/result.rb', line 45

def doc(markup=nil)
  if html = markup || content
    @doc = nil if markup
    parser = opts[:as] ? opts[:as].to_s : nil
    @doc ||= case parser
             when nil, 'html' then Nokogiri::HTML(html)
             when 'xml' then Nokogiri::XML(html)
             else raise InvalidParser.new("Nokogiri cannot parse as '#{opts[:as]}'. Please request :xml or :html.")
             end
  end
end

#doc_ready!Object



63
64
65
# File 'lib/elementor/result.rb', line 63

def doc_ready!
  @doc_ready = true
end

#doc_ready?Boolean

Indicates whether or not the dispatcher has received messages, meaning the content method can be called.

Returns:

  • (Boolean)


59
60
61
# File 'lib/elementor/result.rb', line 59

def doc_ready?
  @doc_ready
end

#element_namesObject

The list of name/selector pairs you specify in the elements block.



39
40
41
# File 'lib/elementor/result.rb', line 39

def element_names
  @element_names ||= { }
end

#parse!(markup) ⇒ Object

Allows for the parsing of raw markup that doesn’t come from the :from option.



18
19
20
# File 'lib/elementor/result.rb', line 18

def parse!(markup)
  doc(markup)
end