Class: Mongoo::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, ActiveModel::Naming
Includes:
ActiveModel::Validations, Changelog, Modifiers, Persistence
Defined in:
lib/mongoo/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Modifiers

#mod, #mod!

Methods included from Persistence

#collection, #destroyed?, included, #insert, #insert!, #new_record?, #persisted?, #reload, #remove, #remove!, #to_key, #to_model, #to_param, #update, #update!

Methods included from Changelog

#changelog

Constructor Details

#initialize(hash = {}, persisted = false) ⇒ Base

Returns a new instance of Base.



73
74
75
76
77
# File 'lib/mongoo/base.rb', line 73

def initialize(hash={}, persisted=false)
  @persisted = persisted
  init_from_hash(hash)
  set_persisted_mongohash((persisted? ? mongohash.deep_clone : nil))
end

Class Method Details

.attribute(name, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
# File 'lib/mongoo/base.rb', line 17

def self.attribute(name, opts={})
  raise ArgumentError.new("missing :type") unless opts[:type]
  self.attributes[name.to_s] = opts
  define_attribute_methods
  true
end

.attributesObject



24
25
26
# File 'lib/mongoo/base.rb', line 24

def self.attributes
  Mongoo::ATTRIBUTE_META[self.to_s] ||= {}
end

.attributes_treeObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mongoo/base.rb', line 28

def self.attributes_tree
  tree = {}
  self.attributes.each do |name, opts|
    parts = name.split(".")
    curr_branch = tree
    while part = parts.shift
      if !parts.empty?
        curr_branch[part.to_s] ||= {}
        curr_branch = curr_branch[part.to_s]
      else
        curr_branch[part.to_s] = opts[:type]
      end
    end
  end
  tree
end

.define_attribute_methodsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mongoo/base.rb', line 45

def self.define_attribute_methods
  define_method("id") do
    get("_id")
  end
  define_method("id=") do |val|
    set("_id", val)
  end
  
  self.attributes_tree.each do |name, val|
    if val.is_a?(Hash)
      define_method(name) do
        AttributeProxy.new(val, [name], self)
      end
    else
      define_method(name) do
        get(name)
      end
      define_method("#{name}=") do |val|
        set(name, val)
      end
    end
  end
end

.known_attribute?(k) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/mongoo/base.rb', line 69

def self.known_attribute?(k)
  k == "_id" || self.attributes[k.to_s]
end

Instance Method Details

#==(val) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/mongoo/base.rb', line 79

def ==(val)
  if val.class.to_s == self.class.to_s
    if val.persisted?
      val.id == self.id
    else
      self.mongohash.raw_hash == val.mongohash.raw_hash
    end
  end
end

#attributesObject



148
149
150
# File 'lib/mongoo/base.rb', line 148

def attributes
  mongohash.to_key_value
end

#get_attribute(k) ⇒ Object Also known as: get, g



97
98
99
100
101
102
# File 'lib/mongoo/base.rb', line 97

def get_attribute(k)
  unless known_attribute?(k)
    raise UnknownAttributeError, k
  end
  mongohash.dot_get(k.to_s)
end

#get_attributes(keys) ⇒ Object Also known as: gets



136
137
138
139
140
# File 'lib/mongoo/base.rb', line 136

def get_attributes(keys)
  found = {}
  keys.each { |k| found[k.to_s] = get_attribute(k) }
  found
end

#known_attribute?(k) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/mongoo/base.rb', line 89

def known_attribute?(k)
  self.class.known_attribute?(k)
end

#merge!(hash) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/mongoo/base.rb', line 152

def merge!(hash)
  if hash.is_a?(Mongoo::Mongohash)
    hash = hash.raw_hash
  end
  hash.deep_stringify_keys!
  hash = mongohash.raw_hash.deep_merge(hash)
  set_mongohash( Mongoo::Mongohash.new(hash) )
  mongohash
end

#mongohashObject



176
177
178
# File 'lib/mongoo/base.rb', line 176

def mongohash
  @mongohash
end

#persisted_mongohashObject



185
186
187
# File 'lib/mongoo/base.rb', line 185

def persisted_mongohash
  @persisted_mongohash
end

#read_attribute_for_validation(key) ⇒ Object



93
94
95
# File 'lib/mongoo/base.rb', line 93

def read_attribute_for_validation(key)
  get_attribute(key)
end

#set_attribute(k, v) ⇒ Object Also known as: set, s



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/mongoo/base.rb', line 106

def set_attribute(k,v)
  unless known_attribute?(k)
    if self.respond_to?("#{k}=")
      return self.send("#{k}=", v)
    else
      raise UnknownAttributeError, k
    end
  end
  unless k.to_s == "_id" || v.nil?
    field_type = self.class.attributes[k.to_s][:type]
    v = Mongoo::AttributeSanitizer.sanitize(field_type, v)
  end
  mongohash.dot_set(k.to_s,v)
end

#set_attributes(k_v_pairs) ⇒ Object Also known as: sets



129
130
131
132
133
# File 'lib/mongoo/base.rb', line 129

def set_attributes(k_v_pairs)
  k_v_pairs.each do |k,v|
    set_attribute(k,v)
  end
end

#unset_attribute(k) ⇒ Object Also known as: unset, u



123
124
125
# File 'lib/mongoo/base.rb', line 123

def unset_attribute(k)
  mongohash.dot_delete(k); true
end

#unset_attributes(keys) ⇒ Object Also known as: unsets



143
144
145
# File 'lib/mongoo/base.rb', line 143

def unset_attributes(keys)
  keys.each { |k| unset_attribute(k) }; true
end

#verify_attributes_in_mongohash(hash) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/mongoo/base.rb', line 189

def verify_attributes_in_mongohash(hash)
  known_keys = self.class.attributes.keys
  known_keys << "_id"
  hash.dot_list.each do |k|
    unless known_keys.include?(k)
      k.split(".").each do |part|
        if opts = self.class.attributes[part]
          if opts[:type] == :hash
            known_keys << k
          end
        end
      end
      unless known_keys.include?(k)
        # this is just annoying.
        #raise Mongoo::UnknownAttributeError, k.to_s
      end
    end
  end
end