Class: Packr::Map
- Inherits:
-
Object
- Object
- Packr::Map
- Defined in:
- lib/packr/map.rb
Overview
This is effectively a wrapper for Hash instances - we’re including it to maintain similarity with the JavaScript version for easier maintainance.
Direct Known Subclasses
Instance Method Summary collapse
- #clear ⇒ Object
- #copy ⇒ Object
- #each ⇒ Object
- #get(key) ⇒ Object
- #get_keys ⇒ Object
- #get_values ⇒ Object
- #has?(key) ⇒ Boolean
-
#initialize(values = nil) ⇒ Map
constructor
A new instance of Map.
- #merge(*args) ⇒ Object
- #put(key, value = nil) ⇒ Object
- #remove(key) ⇒ Object
- #size ⇒ Object
- #union(*values) ⇒ Object
Constructor Details
#initialize(values = nil) ⇒ Map
Returns a new instance of Map.
6 7 8 9 |
# File 'lib/packr/map.rb', line 6 def initialize(values = nil) @values = {} merge(values) unless values.nil? end |
Instance Method Details
#clear ⇒ Object
11 12 13 |
# File 'lib/packr/map.rb', line 11 def clear @values.clear end |
#copy ⇒ Object
15 16 17 |
# File 'lib/packr/map.rb', line 15 def copy self.class.new(@values) end |
#each ⇒ Object
19 20 21 |
# File 'lib/packr/map.rb', line 19 def each @values.each { |key, value| yield(value, key) } end |
#get(key) ⇒ Object
23 24 25 |
# File 'lib/packr/map.rb', line 23 def get(key) @values[key.to_s] end |
#get_keys ⇒ Object
27 28 29 |
# File 'lib/packr/map.rb', line 27 def get_keys @values.keys end |
#get_values ⇒ Object
31 32 33 |
# File 'lib/packr/map.rb', line 31 def get_values @values.values end |
#has?(key) ⇒ Boolean
35 36 37 |
# File 'lib/packr/map.rb', line 35 def has?(key) @values.has_key?(key.to_s) end |
#merge(*args) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/packr/map.rb', line 39 def merge(*args) args.each do |values| values = values.get_values if values.is_a?(Map) values.each { |key, value| put(key, value) } end self end |
#put(key, value = nil) ⇒ Object
51 52 53 54 55 |
# File 'lib/packr/map.rb', line 51 def put(key, value = nil) value ||= key # Create the new entry (or overwrite the old entry). @values[key.to_s] = value end |
#remove(key) ⇒ Object
47 48 49 |
# File 'lib/packr/map.rb', line 47 def remove(key) @values.delete(key.to_s) end |
#size ⇒ Object
57 58 59 |
# File 'lib/packr/map.rb', line 57 def size @values.length end |
#union(*values) ⇒ Object
61 62 63 |
# File 'lib/packr/map.rb', line 61 def union(*values) copy.merge(*values) end |