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/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/blockquote.rb

Direct Known Subclasses

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

Defined Under Namespace

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Element.



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

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



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

def method_missing(name, content = nil, &block)
  if content
    self << ::Paggio::Utils.heredoc(content)
  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



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

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



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

def <<(what)
  @children << what

  self
end

#[](*names) ⇒ Object



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

def [](*names)
  return unless @last

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

  self
end

#do(&block) ⇒ Object



78
79
80
81
82
# File 'lib/paggio/html/element.rb', line 78

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

  self
end

#each(&block) ⇒ Object



42
43
44
# File 'lib/paggio/html/element.rb', line 42

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

#inspectObject



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

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