Class: Recliner::Map

Inherits:
Hash show all
Defined in:
lib/recliner/properties/map.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](hash) ⇒ Object



82
83
84
85
86
# File 'lib/recliner/properties/map.rb', line 82

def self.[](hash)
  result = new
  result.update(hash)
  result
end

.from_couch(hash) ⇒ Object



67
68
69
70
71
# File 'lib/recliner/properties/map.rb', line 67

def from_couch(hash)
  result = new
  hash.each_pair { |key, value| result[from.from_couch(key)] = to.from_couch(value) }
  result
end

.inspectObject



73
74
75
76
77
78
79
# File 'lib/recliner/properties/map.rb', line 73

def inspect
  if self == Map
    super
  else
    "#<Map[#{from} -> #{to}]>"
  end
end

Instance Method Details

#[](key) ⇒ Object



88
89
90
# File 'lib/recliner/properties/map.rb', line 88

def [](key)
  super(convert_key(key))
end

#[]=(key, value) ⇒ Object

Assigns a new value to the map:

map[:key] = "value"


101
102
103
# File 'lib/recliner/properties/map.rb', line 101

def []=(key, value)
  super(convert_key(key), convert_value(value))
end

#fetch(key, *args, &block) ⇒ Object

Fetches the value for the specified key, same as doing hash



93
94
95
# File 'lib/recliner/properties/map.rb', line 93

def fetch(key, *args, &block)
  super(convert_key(key), *args, &block)
end

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

Checks the map for a key matching the argument passed in:

map["key"] = "value"
map.key? :key  # => true
map.key? "key" # => true

Returns:



130
131
132
# File 'lib/recliner/properties/map.rb', line 130

def key?(key)
  super(convert_key(key))
end

#replace(hash) ⇒ Object



138
139
140
141
# File 'lib/recliner/properties/map.rb', line 138

def replace(hash)
  clear
  update(hash)
end

#store(key, value) ⇒ Object



105
106
107
# File 'lib/recliner/properties/map.rb', line 105

def store(key, value)
  super(convert_key(key), convert_value(value))
end

#update(hash) ⇒ Object

Updates the instantized map with values from the second:

map_1 = Map.new
map_1[:key] = "value"

map_2 = Map.new
map_2[:key] = "New Value!"

map_1.update(map_2) # => {"key"=>"New Value!"}


119
120
121
122
# File 'lib/recliner/properties/map.rb', line 119

def update(hash)
  hash.each_pair { |key, value| self[key] = value }
  self
end