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.



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

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

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



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

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

    if value.is_a? Hash
      style = Attributes.new(value).to_style
      "#{key}=\"#{style}\""
    elsif value.is_a? 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