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



155
156
157
158
159
# File 'lib/sevgi/graphics/attribute.rb', line 155

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 live stored attribute value. Mutating a returned container intentionally mutates this store; rendering revalidates the resulting value.

Parameters:

  • key (String, Symbol)

    attribute key

Returns:

  • (Object, nil)

Raises:

  • (Sevgi::ArgumentError)

    when key is not a valid XML attribute name



186
187
188
# File 'lib/sevgi/graphics/attribute.rb', line 186

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



197
198
199
200
201
202
203
# File 'lib/sevgi/graphics/attribute.rb', line 197

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



209
210
211
# File 'lib/sevgi/graphics/attribute.rb', line 209

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. Nested values remain live store values.

Returns:

  • (Hash)

    shallow attribute view



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

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)

Raises:

  • (Sevgi::ArgumentError)

    when key is not a valid XML attribute name



227
228
229
# File 'lib/sevgi/graphics/attribute.rb', line 227

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/sevgi/graphics/attribute.rb', line 166

def import(attributes)
  ArgumentError.("Attributes must be imported from a Hash") unless attributes.is_a?(::Hash)

  hash = attributes.each_with_object({}) do |(key, value), captured|
    next if value.nil?

    id = Attribute.id(key)
    ArgumentError.("Attribute names collide after normalization: #{id}") if captured.key?(id)

    captured[id] = Attribute.capture(value, normalize_keys: value.is_a?(::Hash))
  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 with recursively independent values.

Parameters:

Raises:

  • (Sevgi::ArgumentError)

    when live stored values became cyclic or invalid



235
236
237
238
239
240
# File 'lib/sevgi/graphics/attribute.rb', line 235

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 public attribute names.

Returns:

  • (Array<Symbol>)


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

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 live internal attribute Hash. Mutating it intentionally mutates this store; rendering revalidates names and values.

Returns:

  • (Hash)

    live internal store



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

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



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

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