Class: Docp::TableRow

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/docp/table_row.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tr, header_parser) ⇒ TableRow

Returns a new instance of TableRow.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/docp/table_row.rb', line 9

def initialize tr, header_parser
  @tr = tr
  @row = row_elements.select {|td| td[:class] }
  @no_hash_keys = header_parser.no_hash_keys
  @after_to_hash = header_parser.after_to_hash
  @formats = {}
  header_parser.columns.each {|col|
    [col, col.children].flatten.each {|ch|
      if @tr[:class] == "table-header"
        @formats[ch.name] = format(self[ch.name], :text)
      else
        @formats[ch.name] = format(self[ch.name], ch.format)
      end
    }
  }
end

Instance Attribute Details

#formatsObject (readonly)

Returns the value of attribute formats.



8
9
10
# File 'lib/docp/table_row.rb', line 8

def formats
  @formats
end

#trObject (readonly)

Returns the value of attribute tr.



7
8
9
# File 'lib/docp/table_row.rb', line 7

def tr
  @tr
end

Instance Method Details

#[](name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/docp/table_row.rb', line 26

def [] name
  ret = case name
        when Symbol, String
          name = name.to_s if name.is_a?(Symbol)
          @row.find {|r| 
            r[:class] == name || r[:class].split(',').map {|cl| cl == name}.any?
          }
        else
          @row[name]
        end
  if ret
    ret
  else
    doc = Nokogiri::HTML::DocumentFragment.parse ""
    Nokogiri::XML::Element.new "td", doc
  end
end

#eachObject



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

def each
  @row.each {|td| yield td}
end

#format(td, format) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/docp/table_row.rb', line 44

def format td, format
  if format.is_a?(Symbol)
    td.send(format)
  elsif format.is_a?(Proc)
    par = format.parameters.map(&:last).map 
    if par.include?(:formats)
      -> { format.call(*par.map {|name| name == :row ? self : eval(name.to_s) })  }
    else
      format.call(*par.map{|name| name == :row ? self : eval(name.to_s) }) 
    end
  else
    format
  end
end

#to_hashObject



63
64
65
66
67
68
69
70
71
# File 'lib/docp/table_row.rb', line 63

def to_hash
  ret = {}
  @formats.each {|k, v|
    next if @no_hash_keys.include?(k)
    ret[k] = v.is_a?(Proc) ? v.call : v 
  }
  @after_to_hash.call(ret, self) if @after_to_hash
  ret
end