class Victor::Attributes

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

Attributes

attributes[R]

Public Class Methods

new(attributes={}) click to toggle source
# File lib/victor/attributes.rb, line 8
def initialize(attributes={})
  @attributes = attributes
end

Public Instance Methods

[](key) click to toggle source
# File lib/victor/attributes.rb, line 38
def [](key)
  attributes[key]
end
[]=(key, value) click to toggle source
# File lib/victor/attributes.rb, line 42
def []=(key, value)
  attributes[key] = value
end
to_s() click to toggle source
# 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_style() click to toggle source
# 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