Class: Sevgi::Graphics::Content::CSS

Inherits:
Content
  • Object
show all
Defined in:
lib/sevgi/graphics/auxiliary/content.rb

Overview

CSS content rendered inside a CDATA section.

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ void

Creates CSS content.

Parameters:

  • content (Hash)

    CSS rules

Raises:

  • (Sevgi::ArgumentError)

    when content is not a hash



84
85
86
87
88
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 84

def initialize(content)
  ArgumentError.("CSS content must be a hash: #{content}") unless content.is_a?(::Hash)

  super
end

Instance Method Details

#render(renderer, depth) ⇒ void

This method returns an undefined value.

rubocop:disable Metrics/MethodLength Renders CSS content.

Parameters:

  • renderer (Object)

    renderer receiving output

  • depth (Integer)

    current render depth

Raises:

  • (Sevgi::ArgumentError)

    when a style value cannot be rendered



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 96

def render(renderer, depth)
  depth += 1

  renderer.append(depth, "<![CDATA[")

  depth += 1
  content.each do |rule, styles|
    case styles
    when ::Hash
      renderer.append(depth, "#{rule} {")
      renderer.append(depth + 1, *styles.map { |key, value| "#{key}: #{value};" })
      renderer.append(depth, "}")
    when ::String, ::Symbol, ::Numeric
      renderer.append(depth, "#{rule}: #{styles};")
    else
      ArgumentError.("Malformed style: #{styles}")
    end
  end

  depth -= 1

  renderer.append(depth, "]]>")
end