Class: Packr::Map

Inherits:
Object
  • Object
show all
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

Collection

Instance Method Summary collapse

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

#clearObject



11
12
13
# File 'lib/packr/map.rb', line 11

def clear
  @values.clear
end

#copyObject



15
16
17
# File 'lib/packr/map.rb', line 15

def copy
  self.class.new(@values)
end

#eachObject



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_keysObject



27
28
29
# File 'lib/packr/map.rb', line 27

def get_keys
  @values.keys
end

#get_valuesObject



31
32
33
# File 'lib/packr/map.rb', line 31

def get_values
  @values.values
end

#has?(key) ⇒ Boolean

Returns:

  • (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

#sizeObject



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