Class: Sevgi::Graphics::Attribute::Store Private
- Inherits:
-
Object
- Object
- Sevgi::Graphics::Attribute::Store
- 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
-
#[](key) ⇒ Object?
private
Returns a live stored attribute value.
-
#[]=(key, value) ⇒ Object?
private
Assigns a recursively owned attribute snapshot.
-
#delete(key) ⇒ Object?
private
Deletes an attribute by normalized key.
-
#export ⇒ Hash
private
Returns public attributes ready for rendering.
-
#has?(key) ⇒ Boolean
private
Reports whether an attribute exists.
-
#import(attributes) ⇒ Hash
private
Atomically imports recursively owned attribute snapshots.
-
#initialize(attributes = {}) ⇒ void
constructor
private
Creates an attribute store from recursively owned snapshots.
-
#initialize_copy(original) ⇒ void
private
Copies the attribute store with recursively independent values.
-
#list ⇒ Array<Symbol>
private
Returns public attribute names.
-
#to_h ⇒ Hash
private
Returns the live internal attribute Hash.
-
#to_xml_lines ⇒ Array<String>
private
Returns rendered XML attribute lines.
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.
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.
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.
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.
209 210 211 |
# File 'lib/sevgi/graphics/attribute.rb', line 209 def delete(key) @store.delete(Attribute.id(key)) end |
#export ⇒ 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.
Returns public attributes ready for rendering. Nested values remain live store values.
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.
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.
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.
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 |
#list ⇒ Array<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.
244 245 246 |
# File 'lib/sevgi/graphics/attribute.rb', line 244 def list export.keys end |
#to_h ⇒ 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.
Returns the live internal attribute Hash. Mutating it intentionally mutates this store; rendering revalidates names and values.
251 252 253 |
# File 'lib/sevgi/graphics/attribute.rb', line 251 def to_h @store end |
#to_xml_lines ⇒ Array<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.
258 259 260 |
# File 'lib/sevgi/graphics/attribute.rb', line 258 def to_xml_lines export.map { |id, value| to_xml(id, value) } end |