Class: Paggio::HTML::Element

Inherits:
BasicObject
Defined in:
lib/paggio/html/element.rb,
lib/paggio/html/helpers.rb,
lib/paggio/html/element/a.rb,
lib/paggio/html/element/td.rb,
lib/paggio/html/element/img.rb,
lib/paggio/html/element/base.rb,
lib/paggio/html/element/input.rb,
lib/paggio/html/element/button.rb,
lib/paggio/html/element/canvas.rb,
lib/paggio/html/element/object.rb,
lib/paggio/html/element/blockquote.rb

Direct Known Subclasses

A, Base, Blockquote, Button, Canvas, Img, Input, Object, Td

Defined Under Namespace

Classes: A, Base, Blockquote, Button, Canvas, Img, Input, Object, Td

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Element.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

def method_missing(name, content = nil, &block)
  if content
    self << ::Paggio::Utils.heredoc(content.to_s)
  end

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

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

  self
end

Class Method Details

.defhelper(name, &block) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/paggio/html/helpers.rb', line 14

def self.defhelper(name, &block)
  define_method name do |*args, &body|
    instance_exec(*args, &block)

    self.do(&body) if body
    self
  end
end

.defhelper!(name, attribute = name) ⇒ Object



23
24
25
26
27
# File 'lib/paggio/html/helpers.rb', line 23

def self.defhelper!(name, attribute = name)
  defhelper "#{name}!" do
    @attributes[attribute] = true
  end
end

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



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/paggio/html/element.rb', line 24

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



48
49
50
51
52
# File 'lib/paggio/html/element.rb', line 48

def <<(what)
  @children << what

  self
end

#[](*names) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/paggio/html/element.rb', line 71

def [](*names)
  return unless @last

  @class_names.pop
  @class_names.push([@last, *names].join('-'))

  self
end

#do(&block) ⇒ Object



80
81
82
83
84
# File 'lib/paggio/html/element.rb', line 80

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

  self
end

#each(&block) ⇒ Object



44
45
46
# File 'lib/paggio/html/element.rb', line 44

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

#inspectObject



98
99
100
101
102
103
104
# File 'lib/paggio/html/element.rb', line 98

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