Class: EmbeddedHash
- Inherits:
-
Hash
show all
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/mongoid_ext/types/embedded_hash.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(other = {}) ⇒ EmbeddedHash
Returns a new instance of EmbeddedHash.
4
5
6
7
8
9
|
# File 'lib/mongoid_ext/types/embedded_hash.rb', line 4
def initialize(other = {})
other.each do |k,v|
self[k] = v
end
self["_id"] ||= BSON::ObjectId.new.to_s
end
|
Class Method Details
.field(name, opts = {}) ⇒ Object
11
12
13
14
15
16
17
18
19
|
# File 'lib/mongoid_ext/types/embedded_hash.rb', line 11
def self.field(name, opts = {})
define_method(name) do
self[name.to_s] ||= opts[:default].kind_of?(Proc) ? opts[:default].call : opts[:default]
end
define_method("#{name}=") do |v|
self[name.to_s] = v
end
end
|
Instance Method Details
#id ⇒ Object
Also known as:
_id
21
22
23
|
# File 'lib/mongoid_ext/types/embedded_hash.rb', line 21
def id
self["_id"]
end
|