Class: Arbre::HTML::Tag

Inherits:
Element show all
Defined in:
lib/arbre/html/tag.rb

Direct Known Subclasses

Document, P, Table

Constant Summary collapse

SELF_CLOSING_ELEMENTS =
[ :area, :base, :br, :col, :embed, :hr, :img, :input, :keygen, :link,
:menuitem, :meta, :param, :source, :track, :wbr ]

Instance Attribute Summary collapse

Attributes inherited from Element

#arbre_context, #children, #parent

Instance Method Summary collapse

Methods inherited from Element

#+, #<<, #add_child, #ancestors, #assigns, #children?, #content, #content=, #each, #find_first_ancestor, #get_elements_by_class_name, #get_elements_by_tag_name, #helpers, #html_safe, #indent_level, #inspect, #parent?, #remove_child, #tag_name, #to_ary, #to_str

Methods included from Element::BuilderMethods

#build_tag, #current_arbre_element, included, #insert_tag, #with_current_arbre_element

Constructor Details

#initializeTag

Returns a new instance of Tag.


13
14
15
16
# File 'lib/arbre/html/tag.rb', line 13

def initialize(*)
  super
  @attributes = Attributes.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Arbre::Element

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes


7
8
9
# File 'lib/arbre/html/tag.rb', line 7

def attributes
  @attributes
end

Instance Method Details

#add_class(class_names) ⇒ Object


70
71
72
# File 'lib/arbre/html/tag.rb', line 70

def add_class(class_names)
  class_list.add class_names
end

#build(*args) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/arbre/html/tag.rb', line 18

def build(*args)
  super
  attributes = extract_arguments(args)
  self.content = args.first if args.first

  set_for_attribute(attributes.delete(:for))

  attributes.each do |key, value|
    set_attribute(key, value)
  end
end

#class_listObject


83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/arbre/html/tag.rb', line 83

def class_list
  list = get_attribute(:class)

  case list
  when ClassList
    list
  when String
    set_attribute(:class, ClassList.build_from_string(list))
  else
    set_attribute(:class, ClassList.new)
  end
end

#class_namesObject

Returns a string of classes


79
80
81
# File 'lib/arbre/html/tag.rb', line 79

def class_names
  class_list.to_s
end

#extract_arguments(args) ⇒ Object


30
31
32
33
34
35
36
# File 'lib/arbre/html/tag.rb', line 30

def extract_arguments(args)
  if args.last.is_a?(Hash)
    args.pop
  else
    {}
  end
end

#get_attribute(name) ⇒ Object Also known as: attr


42
43
44
# File 'lib/arbre/html/tag.rb', line 42

def get_attribute(name)
  @attributes[name.to_sym]
end

#has_attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)

47
48
49
# File 'lib/arbre/html/tag.rb', line 47

def has_attribute?(name)
  @attributes.has_key?(name.to_sym)
end

#idObject


55
56
57
# File 'lib/arbre/html/tag.rb', line 55

def id
  get_attribute(:id)
end

#id!Object

Generates and id for the object if it doesn't exist already


60
61
62
63
64
# File 'lib/arbre/html/tag.rb', line 60

def id!
  return id if id
  self.id = object_id.to_s
  id
end

#id=(id) ⇒ Object


66
67
68
# File 'lib/arbre/html/tag.rb', line 66

def id=(id)
  set_attribute(:id, id)
end

#remove_attribute(name) ⇒ Object


51
52
53
# File 'lib/arbre/html/tag.rb', line 51

def remove_attribute(name)
  @attributes.delete(name.to_sym)
end

#remove_class(class_names) ⇒ Object


74
75
76
# File 'lib/arbre/html/tag.rb', line 74

def remove_class(class_names)
  class_list.delete(class_names)
end

#set_attribute(name, value) ⇒ Object


38
39
40
# File 'lib/arbre/html/tag.rb', line 38

def set_attribute(name, value)
  @attributes[name.to_sym] = value
end

#to_sObject


96
97
98
# File 'lib/arbre/html/tag.rb', line 96

def to_s
  indent(opening_tag, content, closing_tag).html_safe
end