Class: PhlexyUI::AttributeSet

Inherits:
Object
  • Object
show all
Defined in:
lib/phlexy_ui/attribute_set.rb

Overview

Converts component modifiers and options into HTML attributes. This is an internal class used by Base components.

Internal Usage:

# Component defines an attributes map
ATTRIBUTES_MAP = { open: { checked: true }, closed: true }

# Attributes can come from modifiers or options:
#   tab(:open)           #=> { checked: true }
#   tab(open: true)      #=> { checked: true }
#   tab(open: false)     #=> {}
#   tab(:closed)         #=> { closed: true }
#   tab(closed: true)    #=> { closed: true }
#   tab(closed: false)   #=> {}

Instance Method Summary collapse

Constructor Details

#initialize(base_modifiers, options, attributes_map) ⇒ AttributeSet

Returns a new instance of AttributeSet.



21
22
23
24
25
# File 'lib/phlexy_ui/attribute_set.rb', line 21

def initialize(base_modifiers, options, attributes_map)
  @base_modifiers = base_modifiers
  @options = options
  @attributes_map = attributes_map
end

Instance Method Details

#to_hObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/phlexy_ui/attribute_set.rb', line 27

def to_h
  result = {}

  # Process modifiers first
  base_modifiers.each do |modifier|
    next unless attributes_map.key?(modifier)
    next if options.key?(modifier)

    process_attribute(modifier, true, result)
  end

  # Then process options (overriding modifiers)
  options.each do |key, value|
    next unless attributes_map.key?(key)
    next unless value

    process_attribute(key, value, result)
  end

  result
end