Module: Map::Ordering
- Included in:
- Map
- Defined in:
- lib/map/ordering.rb
Class Method Summary collapse
-
.included(base) ⇒ Object
Hook called when module is included into Map class This allows us to inject class methods into Map.
Instance Method Summary collapse
- #[]=(key, val) ⇒ Object (also: #store)
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #each ⇒ Object (also: #each_pair)
- #each_key ⇒ Object
- #each_value ⇒ Object
- #each_with_index ⇒ Object
- #first ⇒ Object
-
#keys ⇒ Object
Instance methods that track ordering via @keys array.
- #last ⇒ Object
- #pop ⇒ Object
- #push(*args) ⇒ Object
-
#shift ⇒ Object
Array-like ordered operations.
- #unshift(*args) ⇒ Object
- #values ⇒ Object (also: #vals)
Class Method Details
.included(base) ⇒ Object
Hook called when module is included into Map class This allows us to inject class methods into Map
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/map/ordering.rb', line 17 def self.included(base) base.class_eval do # Override Map.allocate to initialize @keys array def self.allocate super.instance_eval do @keys = [] self end end end end |
Instance Method Details
#[]=(key, val) ⇒ Object Also known as: store
35 36 37 38 39 |
# File 'lib/map/ordering.rb', line 35 def []=(key, val) key, val = convert(key, val) keys.push(key) unless has_key?(key) __set__(key, val) end |
#clear ⇒ Object
84 85 86 87 |
# File 'lib/map/ordering.rb', line 84 def clear keys.clear super end |
#delete(key) ⇒ Object
78 79 80 81 82 |
# File 'lib/map/ordering.rb', line 78 def delete(key) key = convert_key(key) keys.delete(key) super(key) end |
#each ⇒ Object Also known as: each_pair
72 73 74 75 |
# File 'lib/map/ordering.rb', line 72 def each keys.each{|key| yield(key, self[key])} self end |
#each_key ⇒ Object
62 63 64 65 |
# File 'lib/map/ordering.rb', line 62 def each_key keys.each{|key| yield(key)} self end |
#each_value ⇒ Object
67 68 69 70 |
# File 'lib/map/ordering.rb', line 67 def each_value keys.each{|key| yield self[key]} self end |
#each_with_index ⇒ Object
57 58 59 60 |
# File 'lib/map/ordering.rb', line 57 def each_with_index keys.each_with_index{|key, index| yield([key, self[key]], index)} self end |
#first ⇒ Object
49 50 51 |
# File 'lib/map/ordering.rb', line 49 def first [keys.first, self[keys.first]] end |
#keys ⇒ Object
Instance methods that track ordering via @keys array
31 32 33 |
# File 'lib/map/ordering.rb', line 31 def keys @keys ||= [] end |
#last ⇒ Object
53 54 55 |
# File 'lib/map/ordering.rb', line 53 def last [keys.last, self[keys.last]] end |
#pop ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/map/ordering.rb', line 118 def pop unless empty? key = keys.last val = delete(key) [key, val] end end |
#push(*args) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/map/ordering.rb', line 108 def push(*args) Map.each_pair(*args) do |key, val| key = convert_key(key) delete(key) keys.push(key) __set__(key, val) end self end |
#shift ⇒ Object
Array-like ordered operations
90 91 92 93 94 95 96 |
# File 'lib/map/ordering.rb', line 90 def shift unless empty? key = keys.first val = delete(key) [key, val] end end |
#unshift(*args) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/map/ordering.rb', line 98 def unshift(*args) Map.each_pair(*args) do |key, val| key = convert_key(key) delete(key) keys.unshift(key) __set__(key, val) end self end |
#values ⇒ Object Also known as: vals
42 43 44 45 46 |
# File 'lib/map/ordering.rb', line 42 def values array = [] keys.each{|key| array.push(self[key])} array end |