Class: Paggio::HTML::Element

Inherits:
BasicObject
Defined in:
lib/paggio/format.rb,
lib/paggio/html/element.rb

Constant Summary collapse

Format =
::Paggio::Format

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, name, attributes = {}) ⇒ Element

Returns a new instance of Element.



29
30
31
32
33
34
35
# File 'lib/paggio/html/element.rb', line 29

def initialize(owner, name, attributes = {})
  @owner      = owner
  @name       = name
  @attributes = attributes
  @children   = []
  @class      = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, content = nil, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/paggio/html/element.rb', line 57

def method_missing(name, content = nil, &block)
  if content
    self << content
  end

  if name.to_s.end_with? ?!
    @attributes[:id] = name[0 .. -2]
  else
    @last = name
    @class.push(name)
  end

  @owner.extend!(self, &block) if block

  self
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



26
27
28
# File 'lib/paggio/html/element.rb', line 26

def attributes
  @attributes
end

#inner_htmlObject

Returns the value of attribute inner_html.



27
28
29
# File 'lib/paggio/html/element.rb', line 27

def inner_html
  @inner_html
end

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/paggio/html/element.rb', line 26

def name
  @name
end

Class Method Details

.new(owner, name, attributes = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/paggio/html/element.rb', line 14

def self.new(owner, name, attributes = {})
  return super unless self == Element

  const = name.capitalize

  if const_defined?(const)
    const_get(const).new(owner, name, attributes)
  else
    super
  end
end

Instance Method Details

#<<(what) ⇒ Object



45
46
47
48
49
# File 'lib/paggio/html/element.rb', line 45

def <<(what)
  @children << what

  self
end

#[](*names) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/paggio/html/element.rb', line 74

def [](*names)
  return unless @last

  @class.delete(@last)
  @class.push([@last, *names].join('-'))

  self
end

#do(&block) ⇒ Object



83
84
85
86
87
# File 'lib/paggio/html/element.rb', line 83

def do(&block)
  @owner.extend!(self, &block)

  self
end

#each(&block) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/paggio/html/element.rb', line 37

def each(&block)
  return enum_for :each unless block

  @children.each(&block)

  self
end

#format(io = ::StringIO.new, options = { indent: 0 }) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/paggio/format.rb', line 50

def format(io = ::StringIO.new, options = { indent: 0 })
  if @attributes.empty? && @class.empty?
    Format.puts io, "<#{name}>", options[:indent]
  else
    attrs = @attributes.map {|name, value|
      "#{Format.escape(name)}=\"#{Format.escape(value)}\""
    }

    unless @class.empty?
      attrs << "class=\"#{Format.escape(@class.join(' '))}\""
    end

    Format.puts io, "<#{name} #{attrs.join(' ')}>", options[:indent]
  end

  each {|child|
    case child
    when ::String
      child.lines.each {|line|
        Format.puts io, line.strip, options[:indent] + 1
      }

    when CSS
      Format.puts io, "<style>", options[:indent] + 1
      child.format(io, indent: options[:indent] + 2)
      Format.puts io, "</style>", options[:indent] + 1

    else
      child.format(io, indent: options[:indent] + 1)
    end

    io.puts
  }

  io.seek(-1, ::IO::SEEK_CUR)

  Format.puts io, "</#{name}>", @children.empty? ? 0 : options[:indent]

  io
end

#inspectObject



89
90
91
92
93
94
95
# File 'lib/paggio/html/element.rb', line 89

def inspect
  if @children.empty?
    "#<HTML::Element(#{@name.upcase})>"
  else
    "#<HTML::Element(#{@name.upcase}): #{@children.inspect[1 .. -2]}>"
  end
end

#text(text) ⇒ Object



51
52
53
54
55
# File 'lib/paggio/html/element.rb', line 51

def text(text)
  @children << text.to_s

  self
end