Class: Tailmix::HTML::Attributes

Inherits:
Hash
  • Object
show all
Defined in:
lib/tailmix/html/attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_hash = {}, element_name: nil, context: nil) ⇒ Attributes

Returns a new instance of Attributes.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tailmix/html/attributes.rb', line 12

def initialize(initial_hash = {}, element_name: nil, context: nil)
  @element_name = element_name
  @context = context
  super()

  attrs_to_merge = initial_hash.dup

  initial_classes = attrs_to_merge.delete(:class)
  initial_data = attrs_to_merge.delete(:data)
  initial_aria = attrs_to_merge.delete(:aria)

  self[:class] = ClassList.new(initial_classes)
  self[:data]  = DataMap.new("data", initial_data || {})
  self[:aria]  = DataMap.new("aria", initial_aria || {})

  merge!(attrs_to_merge)
end

Instance Attribute Details

#element_nameObject (readonly)

Returns the value of attribute element_name.



10
11
12
# File 'lib/tailmix/html/attributes.rb', line 10

def element_name
  @element_name
end

Instance Method Details

#add(class_names) ⇒ Object



77
78
79
80
# File 'lib/tailmix/html/attributes.rb', line 77

def add(class_names)
  classes.add(class_names)
  self
end

#ariaObject



64
65
66
# File 'lib/tailmix/html/attributes.rb', line 64

def aria
  self[:aria]
end

#classesObject



56
57
58
# File 'lib/tailmix/html/attributes.rb', line 56

def classes
  self[:class]
end

#componentObject Also known as: root



91
92
93
94
95
96
97
98
99
100
# File 'lib/tailmix/html/attributes.rb', line 91

def component
  raise "No context available to build component root" unless @context

  root_attrs = {
    "data-tailmix-component" => @context.component_name,
    "data-tailmix-state" => @context.state_payload,
  }

  self.class.new(self.to_h.merge(root_attrs), element_name: @element_name, context: @context)
end

#dataObject



60
61
62
# File 'lib/tailmix/html/attributes.rb', line 60

def data
  self[:data]
end

#each(&block) ⇒ Object Also known as: each_pair



31
32
33
# File 'lib/tailmix/html/attributes.rb', line 31

def each(&block)
  to_h.each(&block)
end

#each_attribute(&block) ⇒ Object



87
88
89
# File 'lib/tailmix/html/attributes.rb', line 87

def each_attribute(&block)
  [ classes: classes, data: data.to_h, aria: aria.to_h ].each(&block)
end

#remove(class_names) ⇒ Object



82
83
84
85
# File 'lib/tailmix/html/attributes.rb', line 82

def remove(class_names)
  classes.remove(class_names)
  self
end

#stimulusObject



68
69
70
# File 'lib/tailmix/html/attributes.rb', line 68

def stimulus
  data.stimulus
end

#to_hObject Also known as: to_hash



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tailmix/html/attributes.rb', line 36

def to_h
  final_attrs = select { |k, _| !i[class data aria].include?(k.to_sym) }

  class_string = self[:class].to_s
  final_attrs[:class] = class_string unless class_string.empty?

  final_attrs.merge!(self[:data].to_h)
  final_attrs.merge!(self[:aria].to_h)

  final_attrs["data-tailmix-element"] = @element_name if @element_name
  final_attrs["data-tailmix-id"] = @context.id if @context.id

  final_attrs
end

#to_sObject



52
53
54
# File 'lib/tailmix/html/attributes.rb', line 52

def to_s
  classes.to_s
end

#toggle(class_names) ⇒ Object



72
73
74
75
# File 'lib/tailmix/html/attributes.rb', line 72

def toggle(class_names)
  classes.toggle(class_names)
  self
end