Class: MongoModel::Map

Inherits:
Hash
  • Object
show all
Defined in:
lib/mongomodel/support/map.rb

Defined Under Namespace

Modules: PropertyDefaults

Constant Summary collapse

HASH_CONVERTER =
Types.converter_for(Hash)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Map

Returns a new instance of Map


73
74
75
76
# File 'lib/mongomodel/support/map.rb', line 73

def initialize(hash={})
  super()
  update(hash)
end

Class Method Details

.[](mapping) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mongomodel/support/map.rb', line 26

def [](mapping)
  raise "Exactly one mapping must be specified" unless mapping.keys.size == 1

  from = mapping.keys.first
  to   = mapping.values.first

  @map_class_cache ||= {}
  @map_class_cache[[from, to]] ||= begin
    map = Class.new(Map)
    map.from = from
    map.to   = to
    map
  end
end

.from_converterObject


55
56
57
# File 'lib/mongomodel/support/map.rb', line 55

def from_converter
  @from_converter ||= Types.converter_for(from)
end

.from_mongo(hash) ⇒ Object


41
42
43
44
45
# File 'lib/mongomodel/support/map.rb', line 41

def from_mongo(hash)
  result = new
  hash.each_pair { |k, v| result[from_converter.from_mongo(k)] = instantiate(v) }
  result
end

.inspectObject


47
48
49
50
51
52
53
# File 'lib/mongomodel/support/map.rb', line 47

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

.to_converterObject


59
60
61
# File 'lib/mongomodel/support/map.rb', line 59

def to_converter
  @to_converter ||= Types.converter_for(to)
end

Instance Method Details

#[](key) ⇒ Object


82
83
84
# File 'lib/mongomodel/support/map.rb', line 82

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

#[]=(key, value) ⇒ Object


86
87
88
# File 'lib/mongomodel/support/map.rb', line 86

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

#delete(key) ⇒ Object


94
95
96
# File 'lib/mongomodel/support/map.rb', line 94

def delete(key)
  super(convert_key(key))
end

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


98
99
100
# File 'lib/mongomodel/support/map.rb', line 98

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

#index(value) ⇒ Object


116
117
118
# File 'lib/mongomodel/support/map.rb', line 116

def index(value)
  super(convert_value(value))
end

#key(value) ⇒ Object


120
121
122
# File 'lib/mongomodel/support/map.rb', line 120

def key(value)
  super(convert_value(value))
end

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

Returns:


102
103
104
# File 'lib/mongomodel/support/map.rb', line 102

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

#merge(hash) ⇒ Object


134
135
136
# File 'lib/mongomodel/support/map.rb', line 134

def merge(hash)
  dup.update(super(hash))
end

#merge!(hash) ⇒ Object


138
139
140
# File 'lib/mongomodel/support/map.rb', line 138

def merge!(hash)
  update(merge(hash))
end

#replace(hash) ⇒ Object


129
130
131
132
# File 'lib/mongomodel/support/map.rb', line 129

def replace(hash)
  clear
  update(hash)
end

#store(key, value) ⇒ Object


90
91
92
# File 'lib/mongomodel/support/map.rb', line 90

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

#to_mongoObject


78
79
80
# File 'lib/mongomodel/support/map.rb', line 78

def to_mongo
  HASH_CONVERTER.to_mongo(self)
end

#update(hash) ⇒ Object


124
125
126
127
# File 'lib/mongomodel/support/map.rb', line 124

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

#value?(value) ⇒ Boolean Also known as: has_value?

Returns:


110
111
112
# File 'lib/mongomodel/support/map.rb', line 110

def value?(value)
  super(convert_value(value))
end

#values_at(*keys) ⇒ Object


142
143
144
# File 'lib/mongomodel/support/map.rb', line 142

def values_at(*keys)
  super(*keys.map { |k| convert_key(k) })
end