Class: Sumi::Graphics::Attributes

Inherits:
Object
  • Object
show all
Defined in:
graphics/lib/sumi/graphics/attribute.rb,
graphics/lib/sumi/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 = Sumi::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:



319
320
321
# File 'graphics/lib/sumi/graphics/attribute.rb', line 319

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:



327
# File 'graphics/lib/sumi/graphics/attribute.rb', line 327

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:

  • (Sumi::ArgumentError) —

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



335
336
337
338
# File 'graphics/lib/sumi/graphics/attribute.rb', line 335

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:



344
# File 'graphics/lib/sumi/graphics/attribute.rb', line 344

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

#empty? ⇒ Boolean

Reports whether any attributes or non-rendering metadata entries are stored. Consistent with #has? and #to_h, this reflects stored entries including non-rendering metadata.

Returns:

  • (Boolean)


349
# File 'graphics/lib/sumi/graphics/attribute.rb', line 349

def empty? = @store.empty?

#has?(key) ⇒ Boolean Also known as: key?

Reports whether an attribute exists.

Parameters:

  • key (String, Symbol) —

    attribute key

Returns:

  • (Boolean)

Raises:



355
# File 'graphics/lib/sumi/graphics/attribute.rb', line 355

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

#keys ⇒ Array<Symbol>

Returns rendering attribute names, excluding non-rendering metadata.

Returns:

  • (Array<Symbol>) —

    frozen name snapshot



361
# File 'graphics/lib/sumi/graphics/attribute.rb', line 361

def keys = @store.list.freeze

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

Atomically assigns or updates recursively owned attributes.

Parameters:

  • attributes (Hash) —

    attributes and non-rendering metadata. Names can end in UPDATE_SUFFIX

Returns:

Raises:

  • (Sumi::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



368
369
370
371
# File 'graphics/lib/sumi/graphics/attribute.rb', line 368

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

#size ⇒ Integer

Returns the number of stored attributes, including non-rendering metadata. Consistent with #has? and #to_h, this count includes entries prefixed with META_PREFIX.

Returns:

  • (Integer)


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

def size = @store.size

#to_h ⇒ Hash{Symbol => Object}

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

Returns:

  • (Hash{Symbol => Object}) —

    owned attribute and metadata snapshot



380
# File 'graphics/lib/sumi/graphics/attribute.rb', line 380

def to_h = snapshot(@store.to_h)