Class: Sevgi::Graphics::Attributes

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

Overview

Names beginning with META_PREFIX can be read, assigned, deleted, merged, and copied like ordinary attributes. They appear in #to_h, but public SVG attribute enumeration and rendered XML omit them. All values entering or leaving this facade are recursively owned snapshots. Appending UPDATE_SUFFIX to a name requests a same-family update: Strings and Symbols are space-joined, Arrays are concatenated, and Hashes are merged. An update to an absent name behaves as replacement, and nil assignments are ignored.

Examples:

Inspect and update element attributes

element = Sevgi::Graphics.SVG { rect id: "copy", "-source": "original" }.children.first
element.attributes[:"-source"] # => "original"
element.attributes.merge!(fill: "red")
element.attributes.to_h # => { id: "copy", :"-source" => "original", fill: "red" }

Constant Summary collapse

META_PREFIX =

Prefix marking supported non-rendering metadata names. Prefixed entries remain available through the facade but are omitted from SVG output.

"-"
UPDATE_SUFFIX =

Suffix requesting an update of an existing value. String and Symbol values are space-joined, Arrays are concatenated, and Hashes are merged. When the attribute is absent, assignment uses normal replacement behavior.

"+"

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ void

Creates an attribute facade from recursively owned snapshots.

Parameters:

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

    initial attributes and non-rendering metadata

Raises:

  • (Sevgi::ArgumentError)

    when input is not a Hash or contains an invalid name or value



345
346
347
# File 'lib/sevgi/graphics/attribute.rb', line 345

def initialize(attributes = {})
  @store = Attribute::Store.new(attributes)
end

Instance Method Details

#[](key) ⇒ Object?

Returns an owned snapshot of an attribute value.

Parameters:

  • key (String, Symbol)

    attribute key

Returns:

  • (Object, nil)

    recursively owned value or nil when absent

Raises:

  • (Sevgi::ArgumentError)

    when key is not a valid attribute name



353
# File 'lib/sevgi/graphics/attribute.rb', line 353

def [](key) = snapshot(@store[key])

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

Assigns or updates a recursively owned attribute value.

Parameters:

  • key (String, Symbol)

    attribute key, optionally ending in UPDATE_SUFFIX

  • value (Object, nil)

    attribute value; nil is ignored

Returns:

  • (Object, nil)

    recursively owned resulting value or nil when absent

Raises:

  • (Sevgi::ArgumentError)

    when the name or value is invalid, or an existing update uses incompatible or unsupported value families



361
362
363
364
# File 'lib/sevgi/graphics/attribute.rb', line 361

def []=(key, value)
  @store[key] = value
  snapshot(@store[key])
end

#delete(key) ⇒ Object?

Deletes an attribute and returns an owned snapshot of its value.

Parameters:

  • key (String, Symbol)

    attribute key

Returns:

  • (Object, nil)

    deleted value or nil when absent

Raises:

  • (Sevgi::ArgumentError)

    when key is not a valid attribute name



370
# File 'lib/sevgi/graphics/attribute.rb', line 370

def delete(key) = snapshot(@store.delete(key))

#has?(key) ⇒ Boolean

Reports whether an attribute exists.

Parameters:

  • key (String, Symbol)

    attribute key

Returns:

  • (Boolean)

Raises:

  • (Sevgi::ArgumentError)

    when key is not a valid attribute name



376
# File 'lib/sevgi/graphics/attribute.rb', line 376

def has?(key) = @store.has?(key)

#keysArray<Symbol>

Returns rendering attribute names, excluding non-rendering metadata.

Returns:

  • (Array<Symbol>)

    frozen name snapshot



392
# File 'lib/sevgi/graphics/attribute.rb', line 392

def keys = @store.list.freeze

#merge!(attributes) ⇒ Sevgi::Graphics::Attributes

Atomically assigns or updates recursively owned attributes.

Parameters:

  • attributes (Hash)

    attributes and non-rendering metadata; names may end in UPDATE_SUFFIX

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when input is not a Hash, names collide, a name or value is invalid, or an existing update uses incompatible or unsupported value families



399
400
401
402
# File 'lib/sevgi/graphics/attribute.rb', line 399

def merge!(attributes)
  @store.import(attributes)
  self
end

#to_hHash{Symbol => Object}

Returns a recursively owned Hash snapshot including non-rendering metadata.

Returns:

  • (Hash{Symbol => Object})

    owned attribute and metadata snapshot



406
# File 'lib/sevgi/graphics/attribute.rb', line 406

def to_h = snapshot(@store.to_h)