Module: MongoDoc::Attributes::ClassMethods

Defined in:
lib/mongodoc/attributes.rb

Instance Method Summary collapse

Instance Method Details

#_attributesObject



40
41
42
# File 'lib/mongodoc/attributes.rb', line 40

def _attributes
  _keys + _associations
end

#class_from_name(name) ⇒ Object



131
132
133
# File 'lib/mongodoc/attributes.rb', line 131

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

#has_hash(*args) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/mongodoc/attributes.rb', line 105

def has_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|
    _associations << name unless _associations.include?(name)

    define_method("#{name}") do
      association = instance_variable_get("@#{name}")
      unless association
        association = Associations::HashProxy.new(:root => _root || self, :parent => 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

#has_many(*args) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mongodoc/attributes.rb', line 75

def has_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|
    _associations << name unless _associations.include?(name)

    define_method("#{name}") do
      association = instance_variable_get("@#{name}")
      unless association
        association = Associations::CollectionProxy.new(:root => _root || self, :parent => 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

#has_one(*args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mongodoc/attributes.rb', line 51

def has_one(*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|
    _associations << name unless _associations.include?(name)

    attr_reader name

    define_method("#{name}=") do |value|
      association = instance_variable_get("@#{name}")
      unless association
        association = Associations::DocumentProxy.new(:root => _root || self, :parent => 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

#key(*args) ⇒ Object



44
45
46
47
48
49
# File 'lib/mongodoc/attributes.rb', line 44

def key(*args)
  args.each do |name|
    _keys << name unless _keys.include?(name)
    attr_accessor name
  end
end

#type_name_with_module(type_name) ⇒ Object



135
136
137
# File 'lib/mongodoc/attributes.rb', line 135

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