Sha256: 06a73e7457d616ba4d80b1bce9ca4cf60a8a3eb10b23ae37f0a1a86093012df7

Contents?: true

Size: 1.37 KB

Versions: 20

Compression:

Stored size: 1.37 KB

Contents

class DocumentScope

  @context = nil

  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

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

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

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

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

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

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

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

end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
cobweb-1.2.1 lib/document.rb
cobweb-1.2.0 lib/document.rb
cobweb-1.1.0 lib/document.rb
cobweb-1.0.29 lib/document.rb
cobweb-1.0.28 lib/document.rb
cobweb-1.0.27 lib/document.rb
cobweb-1.0.26 lib/document.rb
cobweb-1.0.25 lib/document.rb
cobweb-1.0.24 lib/document.rb
cobweb-1.0.23 lib/document.rb
cobweb-1.0.22 lib/document.rb
cobweb-1.0.21 lib/document.rb
cobweb-1.0.20 lib/document.rb
cobweb-1.0.19 lib/document.rb
cobweb-1.0.18 lib/document.rb
cobweb-1.0.17 lib/document.rb
cobweb-1.0.16 lib/document.rb
cobweb-1.0.15 lib/document.rb
cobweb-1.0.12 lib/document.rb
cobweb-1.0.11 lib/document.rb