Module: Sevgi::Derender::Attributes Private

Extended by:
Attributes
Included in:
Attributes
Defined in:
lib/sevgi/derender/attributes.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Converts SVG/XML attribute hashes into Sevgi DSL keyword source.

Constant Summary collapse

ATTRIBUTES_SHOULD_COME_FIRST =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Attribute keys rendered before ordinary attributes.

%w[
id
inkscape:label
class
xmlns
xmlns:svg
xmlns:inkscape
xmlns:sodipodi
xmlns:_
      ]
.freeze
ATTRIBUTES_SHOULD_COME_LAST =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Attribute keys rendered after ordinary attributes.

%w[
  style
].freeze

Instance Method Summary collapse

Instance Method Details

#decompile(hash) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts an attribute hash into Sevgi DSL keyword source.

Parameters:

  • hash (Hash{String, Symbol => Object})

    attributes to render

Returns:

  • (String)

    Ruby keyword or hash source



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sevgi/derender/attributes.rb', line 29

def decompile(hash)
  hash = hash.dup
  pre, post = {}, {}

  ATTRIBUTES_SHOULD_COME_FIRST.each { |key| pre[key] = hash.delete(key) if hash.key?(key) }
  ATTRIBUTES_SHOULD_COME_LAST.each { |key| post[key] = hash.delete(key) if hash.key?(key) }

  {**pre, **hash, **post}
    .map do |key, value|
      key = Css.to_key(key) if key.is_a?(::String)

      if key == "style"
        "{ #{Attributes.decompile(Css.to_h!(value))} }"
      elsif value.is_a?(::String)
        Css.to_value(value)
      elsif value.is_a?(::Hash)
        "{ #{Attributes.decompile(value)} }"
      else
        value
      end => value

      key.match?(/\W/) ? "#{Ruby.literal(key)}: #{value}" : "#{key}: #{value}"
    end
    .join(", ")
end