Class: Victor::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/victor/attributes.rb

Overview

Handles conversion from a Hash of attributes, to an XML string or a CSS string.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Attributes

Returns a new instance of Attributes.



7
8
9
# File 'lib/victor/attributes.rb', line 7

def initialize(attributes = {})
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/victor/attributes.rb', line 5

def attributes
  @attributes
end

Instance Method Details

#[](key) ⇒ Object



38
39
40
# File 'lib/victor/attributes.rb', line 38

def [](key)
  attributes[key]
end

#[]=(key, value) ⇒ Object



42
43
44
# File 'lib/victor/attributes.rb', line 42

def []=(key, value)
  attributes[key] = value
end

#to_sObject



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

def to_s
  mapped = attributes.map do |key, value|
    key = key.to_s.tr '_', '-'

    case value
    when Hash
      style = Attributes.new(value).to_style
      "#{key}=\"#{style}\""
    when Array
      "#{key}=\"#{value.join ' '}\""
    else
      "#{key}=#{value.to_s.encode(xml: :attr)}"
    end
  end

  mapped.join ' '
end

#to_styleObject



29
30
31
32
33
34
35
36
# File 'lib/victor/attributes.rb', line 29

def to_style
  mapped = attributes.map do |key, value|
    key = key.to_s.tr '_', '-'
    "#{key}:#{value}"
  end

  mapped.join '; '
end