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 an attribute by normalized key.
-
#[]=(key, value) ⇒ Object?
private
Assigns an attribute value.
-
#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
Imports attributes into the store.
-
#initialize(attributes = {}) ⇒ void
constructor
private
Creates an attribute store.
-
#initialize_copy(original) ⇒ void
private
Copies the attribute store.
-
#list ⇒ Array<Symbol>
private
Returns public attribute names.
-
#to_h ⇒ Hash
private
Returns the 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.
49 50 51 52 53 |
# File 'lib/sevgi/graphics/attribute.rb', line 49 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.
72 73 74 |
# File 'lib/sevgi/graphics/attribute.rb', line 72 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.
82 83 84 85 86 |
# File 'lib/sevgi/graphics/attribute.rb', line 82 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.
91 92 93 |
# File 'lib/sevgi/graphics/attribute.rb', line 91 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.
97 98 99 100 101 102 103 |
# File 'lib/sevgi/graphics/attribute.rb', line 97 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.
108 109 110 |
# File 'lib/sevgi/graphics/attribute.rb', line 108 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.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/sevgi/graphics/attribute.rb', line 58 def import(attributes) hash = attributes .compact .to_a .to_h do |key, value| [key.to_sym, value.is_a?(::Hash) ? value.transform_keys(&:to_sym) : 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.
115 116 117 118 119 120 |
# File 'lib/sevgi/graphics/attribute.rb', line 115 def initialize_copy(original) @store = {} original.store.each { |key, value| @store[key] = value.dup } 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.
124 125 126 |
# File 'lib/sevgi/graphics/attribute.rb', line 124 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 internal attribute hash.
130 131 132 |
# File 'lib/sevgi/graphics/attribute.rb', line 130 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.
136 137 138 |
# File 'lib/sevgi/graphics/attribute.rb', line 136 def to_xml_lines export.map { |id, value| to_xml(id, value) } end |