Class: MongoDoc::Associations::HashProxy

Inherits:
Object
  • Object
show all
Includes:
ProxyBase
Defined in:
lib/mongo_doc/associations/hash_proxy.rb

Constant Summary collapse

HASH_METHODS =
(Hash.instance_methods - Object.instance_methods).map { |n| n.to_s }
MUST_DEFINE =
%w[to_a inspect to_bson ==]
DO_NOT_DEFINE =
%w[merge! replace store update]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ProxyBase

#_root=, included, is_document?

Constructor Details

#initialize(options) ⇒ HashProxy

Returns a new instance of HashProxy.



35
36
37
38
# File 'lib/mongo_doc/associations/hash_proxy.rb', line 35

def initialize(options)
  @hash = {}
  super
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



22
23
24
# File 'lib/mongo_doc/associations/hash_proxy.rb', line 22

def hash
  @hash
end

Instance Method Details

#[]=(key, value) ⇒ Object Also known as: store



41
42
43
44
# File 'lib/mongo_doc/associations/hash_proxy.rb', line 41

def []=(key, value)
  raise InvalidEmbeddedHashKey.new("Key name [#{key}] must be a valid element name, see http://www.mongodb.org/display/DOCS/BSON#BSON-noteonelementname") unless valid_key?(key)
  put(key, attach(key, value))
end

#build(key, attrs) ⇒ Object



47
48
49
50
# File 'lib/mongo_doc/associations/hash_proxy.rb', line 47

def build(key, attrs)
  item = _assoc_class.new(attrs)
  store(key, item)
end

#is_a?(klass) ⇒ Boolean Also known as: kind_of?

Lie about our class. Borrowed from Rake::FileList Note: Does not work for case equality (===)

Returns:



54
55
56
# File 'lib/mongo_doc/associations/hash_proxy.rb', line 54

def is_a?(klass)
  klass == Hash || super(klass)
end

#merge!(other) ⇒ Object Also known as: update



59
60
61
62
63
64
65
66
67
# File 'lib/mongo_doc/associations/hash_proxy.rb', line 59

def merge!(other)
  other.each_pair do |key, value|
    self[key] = if block_given?
      yield key, [key], value
    else
      value
    end
  end
end

#putObject



40
# File 'lib/mongo_doc/associations/hash_proxy.rb', line 40

alias put []=

#replace(other) ⇒ Object



70
71
72
73
# File 'lib/mongo_doc/associations/hash_proxy.rb', line 70

def replace(other)
  clear
  merge!(other)
end

#valid?Boolean

Returns:



75
76
77
78
79
80
81
82
83
# File 'lib/mongo_doc/associations/hash_proxy.rb', line 75

def valid?
  values.all? do |child|
    if ProxyBase.is_document?(child)
      child.valid?
    else
      true
    end
  end
end