Module: MongoDoc::Associations

Defined in:
lib/mongo_doc/associations.rb,
lib/mongo_doc/associations/hash_proxy.rb,
lib/mongo_doc/associations/proxy_base.rb,
lib/mongo_doc/associations/document_proxy.rb,
lib/mongo_doc/associations/collection_proxy.rb

Defined Under Namespace

Modules: ProxyBase Classes: CollectionProxy, DocumentProxy, HashProxy

Instance Method Summary collapse

Instance Method Details

#class_from_name(name) ⇒ Object



101
102
103
# File 'lib/mongo_doc/associations.rb', line 101

def class_from_name(name)
  type_name_with_module(name.to_s.classify).constantize rescue nil
end

#embed(*args) ⇒ Object Also known as: has_one



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mongo_doc/associations.rb', line 9

def embed(*args)
  options = args.extract_options!
  assoc_class = if class_name = options.delete(:class_name)
    self.class_from_name(class_name)
  end

  args.each do |name|
    _add_association(name)

    attr_reader name

    define_method("#{name}=") do |value|
      association = instance_variable_get("@#{name}")
      unless association
        association = Associations::DocumentProxy.new(:path => _selector_path,
                                                      :root => _root || self,
                                                      :assoc_name => name,
                                                      :assoc_class => assoc_class || self.class.class_from_name(name))
        instance_variable_set("@#{name}", association)
      end
      association.document = value
    end

    validates_embedded name, :if => Proc.new { !send(name).nil? }
  end
end

#embed_hash(*args) ⇒ Object Also known as: has_hash



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mongo_doc/associations.rb', line 71

def embed_hash(*args)
  options = args.extract_options!
  assoc_class = if class_name = options.delete(:class_name)
    self.class_from_name(class_name)
  end

  args.each do |name|
    _add_association(name)

    define_method("#{name}") do
      association = instance_variable_get("@#{name}")
      unless association
        association = Associations::HashProxy.new(:path => _selector_path,
                                                  :root => _root || self,
                                                  :assoc_name => name,
                                                  :assoc_class => assoc_class || self.class.class_from_name(name))
        instance_variable_set("@#{name}", association)
      end
      association
    end

    validates_embedded name

    define_method("#{name}=") do |hash|
      send("#{name}").replace(hash)
    end
  end
end

#embed_many(*args) ⇒ Object Also known as: has_many



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mongo_doc/associations.rb', line 37

def embed_many(*args)
  options = args.extract_options!
  assoc_class = if class_name = options.delete(:class_name)
    self.class_from_name(class_name)
  end

  args.each do |name|
    _add_association(name)

    define_method("#{name}") do
      association = instance_variable_get("@#{name}")
      unless association
        association = Associations::CollectionProxy.new(:path => _selector_path,
                                                        :root => _root || self,
                                                        :assoc_name => name,
                                                        :assoc_class => assoc_class || self.class.class_from_name(name))
        instance_variable_set("@#{name}", association)
      end
      association
    end

    validates_embedded name

    define_method("#{name}=") do |arrayish|
      proxy = send("#{name}")
      proxy.clear
      Array.wrap(arrayish).each do|item|
        proxy << item
      end
    end
  end
end

#type_name_with_module(type_name) ⇒ Object



105
106
107
# File 'lib/mongo_doc/associations.rb', line 105

def type_name_with_module(type_name)
  (/^::/ =~ type_name) ? type_name : "#{parent}::#{type_name}"
end