Class: Mongoo::AttributeProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoo/attribute_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(curr_hash, path, doc) ⇒ AttributeProxy

Returns a new instance of AttributeProxy.



4
5
6
7
8
# File 'lib/mongoo/attribute_proxy.rb', line 4

def initialize(curr_hash, path, doc)
  @curr_hash = curr_hash
  @path      = path
  @doc       = doc
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mongoo/attribute_proxy.rb', line 24

def method_missing(name, *args)
  name_s = name.to_s
  setter = false
  setter = true if name_s =~ /\=$/
  name_s.gsub!(/\=$/, '')
  
  if val = @curr_hash[name_s]
    key = (@path + [name_s]).join(".")
    if val.is_a?(Hash) && !@doc.known_attribute?(key)
      if setter
        super
      else
        AttributeProxy.new(val, (@path + [name_s]), @doc)
      end
    else
      setter ? @doc.set(key, args[0]) : @doc.get(key)
    end
  else
    super
  end
end

Instance Method Details

#mod(opts = {}, &block) ⇒ Object



14
15
16
17
18
# File 'lib/mongoo/attribute_proxy.rb', line 14

def mod(opts={}, &block)
  builder = ModifierBuilder.new(opts.merge(:key_prefix => "#{@path.join(".")}."), @doc)
  block.call(builder)
  builder.run!
end

#mod!(opts = {}, &block) ⇒ Object



20
21
22
# File 'lib/mongoo/attribute_proxy.rb', line 20

def mod!(opts={}, &block)
  mod(opts.merge(:safe => true), &block)
end

#unsetObject



10
11
12
# File 'lib/mongoo/attribute_proxy.rb', line 10

def unset
  @doc.unset @path.join(".")
end