Class: Arbre::HTML::Tag

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

Direct Known Subclasses

Document, P

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 Rails::Rendering

#render

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.



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

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

Instance Method Details

#add_class(class_names) ⇒ Object



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

def add_class(class_names)
  class_list.add class_names
end

#build(*args) ⇒ Object



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

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



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

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



83
84
85
# File 'lib/arbre/html/tag.rb', line 83

def class_names
  class_list.to_s
end

#extract_arguments(args) ⇒ Object



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

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

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



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

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

#has_attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#idObject



59
60
61
# File 'lib/arbre/html/tag.rb', line 59

def id
  get_attribute(:id)
end

#id!Object

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



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

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

#id=(id) ⇒ Object



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

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

#remove_attribute(name) ⇒ Object



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

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

#remove_class(class_names) ⇒ Object



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

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

#set_attribute(name, value) ⇒ Object



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

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

#to_sObject



100
101
102
# File 'lib/arbre/html/tag.rb', line 100

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