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/link.rb,
lib/paggio/html/element/embed.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/option.rb,
lib/paggio/html/element/select.rb,
lib/paggio/html/element/optgroup.rb,
lib/paggio/html/element/blockquote.rb

Direct Known Subclasses

A, Base, Blockquote, Button, Canvas, Embed, Img, Input, Link, Object, Optgroup, Option, Select, Td

Defined Under Namespace

Classes: A, Base, Blockquote, Button, Canvas, Embed, Img, Input, Link, Object, Optgroup, Option, Select, Td

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Element.



41
42
43
44
45
46
47
# File 'lib/paggio/html/element.rb', line 41

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/paggio/html/element.rb', line 59

def method_missing(name, content = nil, &block)
  if name.to_s.end_with? ?!
    @attributes[:id] = name[0 .. -2]
  else
    @class_names << name
  end

  if ::Hash === content
    if content.has_key?(:class) || content.has_key?(:classes)
      @class_names.unshift(*(content.delete(:class).to_s.split | content.delete(:classes).to_a))
    end

    ::Paggio::Utils.deep_merge!(@attributes, content)
  elsif content
    self >> content
  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



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

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



53
54
55
56
57
# File 'lib/paggio/html/element.rb', line 53

def <<(what)
  @children << what

  self
end

#>>(content) ⇒ Object



95
96
97
98
# File 'lib/paggio/html/element.rb', line 95

def >>(content)
  self << ::Paggio::Utils.heredoc(content.to_s)
  self
end

#[](*names) ⇒ Object



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

def [](*names)
  if last = @class_names.pop
    @class_names << [last, *names].join('-')
  end

  self
end

#do(&block) ⇒ Object



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

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

  self
end

#each(&block) ⇒ Object



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

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

#inspectObject



112
113
114
115
116
117
118
# File 'lib/paggio/html/element.rb', line 112

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