Class: Arbo::HTML::Tag

Inherits:
Element show all
Defined in:
lib/arbo/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

#arbo_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, #render_in_or_to_s, #tag_name, #to_ary, #to_str

Methods included from Element::BuilderMethods

#build_tag, #current_arbo_element, included, #insert_tag, #with_current_arbo_element

Constructor Details

#initializeTag

Returns a new instance of Tag.



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

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

Instance Method Details

#add_class(class_names) ⇒ Object



73
74
75
# File 'lib/arbo/html/tag.rb', line 73

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
29
30
31
# File 'lib/arbo/html/tag.rb', line 18

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

  for_value = attributes[:for]
  unless for_value.is_a?(String) || for_value.is_a?(Symbol)
    set_for_attribute(attributes.delete(:for))
  end

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

#class_listObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/arbo/html/tag.rb', line 86

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



82
83
84
# File 'lib/arbo/html/tag.rb', line 82

def class_names
  class_list.to_s
end

#extract_arguments(args) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/arbo/html/tag.rb', line 33

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

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



45
46
47
# File 'lib/arbo/html/tag.rb', line 45

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

#has_attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/arbo/html/tag.rb', line 50

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

#idObject



58
59
60
# File 'lib/arbo/html/tag.rb', line 58

def id
  get_attribute(:id)
end

#id!Object

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



63
64
65
66
67
# File 'lib/arbo/html/tag.rb', line 63

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

#id=(id) ⇒ Object



69
70
71
# File 'lib/arbo/html/tag.rb', line 69

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

#remove_attribute(name) ⇒ Object



54
55
56
# File 'lib/arbo/html/tag.rb', line 54

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

#remove_class(class_names) ⇒ Object



77
78
79
# File 'lib/arbo/html/tag.rb', line 77

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

#render_in(context = arbo_context) ⇒ Object



103
104
105
# File 'lib/arbo/html/tag.rb', line 103

def render_in(context = arbo_context)
  indent_in_context(context)
end

#set_attribute(name, value) ⇒ Object



41
42
43
# File 'lib/arbo/html/tag.rb', line 41

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

#to_sObject



99
100
101
# File 'lib/arbo/html/tag.rb', line 99

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