Class: DocumentScope

Inherits:
Object
  • Object
show all
Defined in:
lib/document.rb

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ DocumentScope

Returns a new instance of DocumentScope.



34
35
36
# File 'lib/document.rb', line 34

def initialize(body)
  @context = Nokogiri::HTML.parse(body)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/document.rb', line 5

def method_missing(m, *args, &block)
  if m.to_s =~ /^(.*?)_tags$/
    tag_name = $1
    @context = @context.search($1) if @context
    self
  elsif m.to_s =~ /^(.*?)_tag$/
    tag_name = $1
    @context = @context.at($1) if @context
    self
  elsif m.to_s =~ /^(.*?)_tags_with_(.*?)$/
    tag_name = $1
    attribute_name = $2
    attribute_value = "=#{args[0]}" unless args[0].nil?

    selector = "#{tag_name}[#{attribute_name}#{attribute_value}]"
    @context = @context.search(selector) if @context
    self
  elsif m.to_s =~ /^(.*?)_tag_with_(.*?)$/
    tag_name = $1
    attribute_name = $2
    attribute_value = "='#{args[0]}'" unless args[0].nil?
    selector = "#{tag_name}[#{attribute_name}#{attribute_value}]"
    @context = @context.at(selector) if @context
    self
  else
    super
  end
end

Instance Method Details

#[](value) ⇒ Object



50
51
52
# File 'lib/document.rb', line 50

def [](value)
  @context ? @context[value] : ""
end

#contentsObject Also known as: text



54
55
56
# File 'lib/document.rb', line 54

def contents
  @context ? @context.text.gsub("\n","") : ""
end

#countObject



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

def count
  @context ? @context.count : 0
end

#each(&block) ⇒ Object



38
39
40
# File 'lib/document.rb', line 38

def each(&block)
  @context.each(&block)
end

#map(&block) ⇒ Object



42
43
44
# File 'lib/document.rb', line 42

def map(&block)
  @context.map(&block)
end

#select(&block) ⇒ Object



46
47
48
# File 'lib/document.rb', line 46

def select(&block)
  @context.select(&block)
end

#to_sObject



62
63
64
# File 'lib/document.rb', line 62

def to_s
  @context ? @context.to_s.gsub("\n","") : ""
end