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 backing store for SVG attributes.

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 from recursively owned snapshots. Mutable non-container leaves are stringified once; later caller mutation cannot change the store.

Parameters:

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

    initial attributes

Raises:

  • (Sevgi::ArgumentError)

    when input is not a Hash or a name/value is invalid, cyclic, colliding, or cannot be converted



180
181
182
183
184
# File 'lib/sevgi/graphics/attribute.rb', line 180

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 a stored attribute value for the public facade.

Parameters:

  • key (String, Symbol)

    attribute key

Returns:

  • (Object, nil)

Raises:

  • (Sevgi::ArgumentError)

    when key is not a valid XML attribute name



202
203
204
# File 'lib/sevgi/graphics/attribute.rb', line 202

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 a recursively owned attribute snapshot. Mutable non-container leaves are stringified once.

Parameters:

  • key (String, Symbol)

    attribute key

  • value (Object, nil)

    attribute value; nil is ignored

Returns:

  • (Object, nil)

    stored snapshot or nil

Raises:

  • (Sevgi::ArgumentError)

    when update syntax receives incompatible values

  • (Sevgi::ArgumentError)

    when update syntax receives an unsupported value type

  • (Sevgi::ArgumentError)

    when a name/value is invalid, cyclic, colliding, or cannot be converted



213
214
215
216
217
218
219
# File 'lib/sevgi/graphics/attribute.rb', line 213

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

  id = Attribute.id(key)
  value = Attribute.capture(value, normalize_keys: value.is_a?(::Hash))
  @store[id] = @store.key?(id) && Attribute.updateable?(key) ? update(@store[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

Raises:

  • (Sevgi::ArgumentError)

    when key is not a valid XML attribute name



225
226
227
# File 'lib/sevgi/graphics/attribute.rb', line 225

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 rendering attributes, excluding non-rendering metadata. Nested values remain live store values.

Returns:

  • (Hash)

    shallow attribute view



231
232
233
234
235
236
237
# File 'lib/sevgi/graphics/attribute.rb', line 231

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

  # Keep id first for stable, readable SVG output.
  {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)

Raises:

  • (Sevgi::ArgumentError)

    when key is not a valid XML attribute name



243
244
245
# File 'lib/sevgi/graphics/attribute.rb', line 243

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.

Atomically imports recursively owned attribute snapshots.

Parameters:

  • attributes (Hash)

    attributes to merge

Returns:

  • (Hash)

    internal store

Raises:

  • (Sevgi::ArgumentError)

    when input is not a Hash or a name/value is invalid, cyclic, colliding, or cannot be converted



191
192
193
194
195
196
# File 'lib/sevgi/graphics/attribute.rb', line 191

def import(attributes)
  updated = @store.dup
  entries(attributes).each { |entry| assign(updated, *entry) }

  @store.replace(updated)
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 with recursively independent values.

Parameters:



250
251
252
253
254
255
# File 'lib/sevgi/graphics/attribute.rb', line 250

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

  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 rendering attribute names, excluding non-rendering metadata.

Returns:

  • (Array<Symbol>)


259
260
261
# File 'lib/sevgi/graphics/attribute.rb', line 259

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 and metadata Hash.

Returns:

  • (Hash)

    backing store



265
266
267
# File 'lib/sevgi/graphics/attribute.rb', line 265

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>)

Raises:

  • (Sevgi::ArgumentError)

    when stored names or values are invalid, cyclic, or cannot be stringified



272
273
274
# File 'lib/sevgi/graphics/attribute.rb', line 272

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