Class: DomRender

Inherits:
Object
  • Object
show all
Includes:
InspectArray
Defined in:
lib/dom_render.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InspectArray

#scan

Constructor Details

#initialize(x) ⇒ DomRender

Returns a new instance of DomRender.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dom_render.rb', line 49

def initialize(x)
  
  raise "DomRender#initialize: supplied parameter cannot be nil" unless x
  
  doc = if x.kind_of? Rexle then
    x
  else
    Rexle.new x.gsub(/\n/,'')
  end
  
  @a = render doc.root
end

Instance Attribute Details

#to_a(inspect: false, verbose: false) ⇒ Object (readonly)

Returns the value of attribute to_a.



47
48
49
# File 'lib/dom_render.rb', line 47

def to_a
  @to_a
end

Instance Method Details

#render(x) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dom_render.rb', line 62

def render(x)
  
  style = x.attributes.has_key?(:style) ? fetch_style(x.attributes) : {}
  args = [x]
  args.concat([x.attributes, style])
  
  r = method(x.name.to_sym).call(*args)
  
  return unless r and r.length > 0

  if r.last.nil? or r.last.empty? then
    r[0..-2].flatten(1)
  else
    r
  end
end

#render_all(x) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dom_render.rb', line 79

def render_all(x)

  len = x.children.length - 1

  r = x.children.map.with_index do |obj,i|

    if obj.is_a? String then
      if obj.strip.length > 0 then
        i == 0 ? obj.lstrip.sub(/\s+$/,' ') : obj.sub(/^\s+/,' ')          
      else
        ''
      end
    elsif obj.is_a? Rexle::Element
      render obj
    end

  end

  r.compact

end