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.



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

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

  import(attributes)
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



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

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



56
57
58
# File 'lib/sevgi/graphics/attribute.rb', line 56

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

#exportObject



60
61
62
63
64
65
66
# File 'lib/sevgi/graphics/attribute.rb', line 60

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)


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

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

#import(attributes) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/sevgi/graphics/attribute.rb', line 35

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



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

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

  super
end

#listObject



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

def list
  export.keys
end

#to_hObject



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

def to_h
  @store
end

#to_xml_linesObject



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

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