Class: Gammo::Attributes

Inherits:
Array
  • Object
show all
Defined in:
lib/gammo/attributes.rb

Overview

Class for representing attributes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array, owner_element: nil) ⇒ Attributes

Returns a new instance of Attributes.



8
9
10
11
12
13
# File 'lib/gammo/attributes.rb', line 8

def initialize(array, owner_element: nil)
  super(array)
  array.each { |attr| attr.owner_element = owner_element }
  @owner_element = owner_element
  @attributes_hash = attributes_to_hash(array)
end

Instance Attribute Details

#owner_elementObject

Returns the value of attribute owner_element.



6
7
8
# File 'lib/gammo/attributes.rb', line 6

def owner_element
  @owner_element
end

Instance Method Details

#<<(attr) ⇒ Object



15
16
17
18
# File 'lib/gammo/attributes.rb', line 15

def <<(attr)
  super
  @attributes_hash[attr.key] = attr.value
end

#[](key) ⇒ Object



20
21
22
# File 'lib/gammo/attributes.rb', line 20

def [](key)
  @attributes_hash[key.to_s]
end

#[]=(key, value) ⇒ Object



24
25
26
# File 'lib/gammo/attributes.rb', line 24

def []=(key, value)
  self << Attribute.new(key: key.to_s, value: value, owner_element: owner_element)
end

#append(*attrs) ⇒ Object Also known as: push



49
50
51
52
# File 'lib/gammo/attributes.rb', line 49

def append(*attrs)
  super
  attrs.each { |attr| @attributes_hash[attr.key.to_s] = attr.value }
end

#delete(attr) ⇒ Object



55
56
57
58
59
# File 'lib/gammo/attributes.rb', line 55

def delete(attr)
  deleted = super
  @attributes_hash.delete(deleted.key) if deleted
  deleted
end

#delete_at(pos) ⇒ Object



75
76
77
78
79
# File 'lib/gammo/attributes.rb', line 75

def delete_at(pos)
  deleted = super
  deleted.each { |attr| @attributes_hash.delete(attr.key.to_s) }
  deleted
end

#delete_ifObject



68
69
70
71
72
73
# File 'lib/gammo/attributes.rb', line 68

def delete_if
  original = self.dup
  super
  (original - self).each { |attr| @attributes_hash.delete(attr.key.to_s) }
  self
end

#has_key?(key) ⇒ Boolean Also known as: key?

Returns:

  • (Boolean)


89
90
91
# File 'lib/gammo/attributes.rb', line 89

def has_key?(key)
  @attributes_hash.key?(key.to_s)
end

#pop(n = nil) ⇒ Object



42
43
44
45
46
47
# File 'lib/gammo/attributes.rb', line 42

def pop(n = nil)
  original = self.dup
  ret = n ? super : super()
  (original - self).each { |attr| @attributes_hash.delete(attr.key.to_s) }
  ret
end

#prepend(*attrs) ⇒ Object Also known as: unshift



28
29
30
31
32
# File 'lib/gammo/attributes.rb', line 28

def prepend(*attrs)
  prepended = super
  attrs.each { |attr| @attributes_hash[attr.key.to_s] = attr.value }
  prepended
end

#reject!Object



61
62
63
64
65
66
# File 'lib/gammo/attributes.rb', line 61

def reject!
  original = self.dup
  rejected = super
  (original - self).each { |attr| @attributes_hash.delete(attr.key.to_s) }
  rejected
end

#shift(n = nil) ⇒ Object



35
36
37
38
39
40
# File 'lib/gammo/attributes.rb', line 35

def shift(n = nil)
  original = self.dup
  ret = n ? super : super()
  (original - self).each { |attr| @attributes_hash.delete(attr.key.to_s) }
  ret
end

#to_hObject



81
82
83
# File 'lib/gammo/attributes.rb', line 81

def to_h
  @attributes_hash.dup
end

#to_sObject



85
86
87
# File 'lib/gammo/attributes.rb', line 85

def to_s
  @attributes_hash.to_s
end