class Victor::CSS

Attributes

attributes[R]

Public Class Methods

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

Public Instance Methods

render() click to toggle source
# File lib/victor/css.rb, line 13
def render
  return '' if attributes.empty?

  %[<style>\n#{self}\n</style>\n]
end
to_s() click to toggle source
# File lib/victor/css.rb, line 9
def to_s
  convert_hash attributes
end

Protected Instance Methods

convert_hash(hash, indent = 2) click to toggle source
# File lib/victor/css.rb, line 21
def convert_hash(hash, indent = 2)
  return hash unless hash.is_a? Hash

  result = []
  hash.each do |key, value|
    key = key.to_s.tr '_', '-'
    result += css_block(key, value, indent)
  end

  result.join "\n"
end
css_block(key, value, indent) click to toggle source
# File lib/victor/css.rb, line 33
def css_block(key, value, indent)
  result = []

  my_indent = ' ' * indent

  case value
  when Hash
    result.push "#{my_indent}#{key} {"
    result.push convert_hash(value, indent + 2)
    result.push "#{my_indent}}"
  when Array
    value.each do |row|
      result.push "#{my_indent}#{key} #{row};"
    end
  else
    result.push "#{my_indent}#{key}: #{value};"
  end

  result
end