Class: Sevgi::Graphics::Attribute::Store

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

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Store

Returns a new instance of Store.



28
29
30
31
32
# File 'lib/sevgi/graphics/attribute.rb', line 28

def initialize(attributes = {})
  @store = {}

  import(attributes)
end

Instance Method Details

#[](key) ⇒ Object



42
43
44
# File 'lib/sevgi/graphics/attribute.rb', line 42

def [](key)
  @store[Attribute.id(key)]
end

#[]=(key, value) ⇒ Object



46
47
48
49
50
# File 'lib/sevgi/graphics/attribute.rb', line 46

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



52
53
54
# File 'lib/sevgi/graphics/attribute.rb', line 52

def delete(key)
  @store.delete(Attribute.id(key))
end

#exportObject



56
57
58
59
60
61
62
# File 'lib/sevgi/graphics/attribute.rb', line 56

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

Returns:

  • (Boolean)


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

def has?(key)
  @store.key?(Attribute.id(key))
end

#import(attributes) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/sevgi/graphics/attribute.rb', line 34

def import(attributes)
  hash = attributes.compact.to_a.map do |key, value|
    [ key.to_sym, value.is_a?(::Hash) ? value.transform_keys!(&:to_sym) : value ]
  end.to_h

  @store.merge!(hash)
end

#initialize_copy(original) ⇒ Object



68
69
70
71
72
73
# File 'lib/sevgi/graphics/attribute.rb', line 68

def initialize_copy(original)
  @store = {}
  original.store.each { |key, value| @store[key] = value.dup }

  super
end

#listObject



75
76
77
# File 'lib/sevgi/graphics/attribute.rb', line 75

def list
  export.keys
end

#to_hObject



79
80
81
# File 'lib/sevgi/graphics/attribute.rb', line 79

def to_h
  @store
end

#to_xml_linesObject



83
84
85
# File 'lib/sevgi/graphics/attribute.rb', line 83

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