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



64
65
66
67
68
# File 'lib/sevgi/graphics/attribute.rb', line 64

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)


87
88
89
# File 'lib/sevgi/graphics/attribute.rb', line 87

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



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

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



106
107
108
# File 'lib/sevgi/graphics/attribute.rb', line 106

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)


112
113
114
115
116
117
118
# File 'lib/sevgi/graphics/attribute.rb', line 112

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)


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

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



73
74
75
76
77
78
79
80
81
82
# File 'lib/sevgi/graphics/attribute.rb', line 73

def import(attributes)
  hash = attributes
    .compact
    .to_a
    .to_h do |key, value|
      [key.to_sym, import_value(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:



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

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


139
140
141
# File 'lib/sevgi/graphics/attribute.rb', line 139

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)


145
146
147
# File 'lib/sevgi/graphics/attribute.rb', line 145

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


151
152
153
# File 'lib/sevgi/graphics/attribute.rb', line 151

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