Class: Sevgi::Graphics::Attribute::Store Private

Inherits:
Object
  • Object
show all
Defined in:
lib/sevgi/graphics/attribute.rb

Overview

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

Mutable SVG attribute store with Sevgi update syntax.

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ void

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.

Creates an attribute store.

Parameters:

  • attributes (Hash) (defaults to: {})

    initial attributes



49
50
51
52
53
# File 'lib/sevgi/graphics/attribute.rb', line 49

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

  import(attributes)
end

Instance Method Details

#[](key) ⇒ Object?

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.

Returns an attribute by normalized key.

Parameters:

  • key (String, Symbol)

    attribute key

Returns:

  • (Object, nil)


72
73
74
# File 'lib/sevgi/graphics/attribute.rb', line 72

def [](key)
  @store[Attribute.id(key)]
end

#[]=(key, value) ⇒ Object?

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.

Assigns an attribute value.

Parameters:

  • key (String, Symbol)

    attribute key

  • value (Object, nil)

    attribute value; nil is ignored

Returns:

  • (Object, nil)

    assigned value or nil

Raises:

  • (Sevgi::ArgumentError)

    when update syntax receives incompatible values

  • (Sevgi::ArgumentError)

    when update syntax receives an unsupported value type



82
83
84
85
86
# File 'lib/sevgi/graphics/attribute.rb', line 82

def []=(key, value)
  return if value.nil?

  @store[id = Attribute.id(key)] = @store.key?(id) && Attribute.updateable?(key) ? update(id, value) : value
end

#delete(key) ⇒ Object?

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.

Deletes an attribute by normalized key.

Parameters:

  • key (String, Symbol)

    attribute key

Returns:

  • (Object, nil)

    deleted value



91
92
93
# File 'lib/sevgi/graphics/attribute.rb', line 91

def delete(key)
  @store.delete(Attribute.id(key))
end

#exportHash

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.

Returns public attributes ready for rendering.

Returns:

  • (Hash)


97
98
99
100
101
102
103
# File 'lib/sevgi/graphics/attribute.rb', line 97

def export
  hash = @store.reject { |id, _| Attribute.internal?(id) }
  return hash unless hash.key?(:id)

  # A small aesthetic touch: always keep the id attribute first
  {id: hash.delete(:id), **hash}
end

#has?(key) ⇒ Boolean

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.

Reports whether an attribute exists.

Parameters:

  • key (String, Symbol)

    attribute key

Returns:

  • (Boolean)


108
109
110
# File 'lib/sevgi/graphics/attribute.rb', line 108

def has?(key)
  @store.key?(Attribute.id(key))
end

#import(attributes) ⇒ Hash

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.

Imports attributes into the store.

Parameters:

  • attributes (Hash)

    attributes to merge

Returns:

  • (Hash)

    internal store



58
59
60
61
62
63
64
65
66
67
# File 'lib/sevgi/graphics/attribute.rb', line 58

def import(attributes)
  hash = attributes
    .compact
    .to_a
    .to_h do |key, value|
      [key.to_sym, value.is_a?(::Hash) ? value.transform_keys(&:to_sym) : value]
    end

  @store.merge!(hash)
end

#initialize_copy(original) ⇒ void

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.

This method returns an undefined value.

Copies the attribute store.

Parameters:



115
116
117
118
119
120
# File 'lib/sevgi/graphics/attribute.rb', line 115

def initialize_copy(original)
  @store = {}
  original.store.each { |key, value| @store[key] = value.dup }

  super
end

#listArray<Symbol>

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.

Returns public attribute names.

Returns:

  • (Array<Symbol>)


124
125
126
# File 'lib/sevgi/graphics/attribute.rb', line 124

def list
  export.keys
end

#to_hHash

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.

Returns the internal attribute hash.

Returns:

  • (Hash)


130
131
132
# File 'lib/sevgi/graphics/attribute.rb', line 130

def to_h
  @store
end

#to_xml_linesArray<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.

Returns rendered XML attribute lines.

Returns:

  • (Array<String>)


136
137
138
# File 'lib/sevgi/graphics/attribute.rb', line 136

def to_xml_lines
  export.map { |id, value| to_xml(id, value) }
end