Class: Mongoid::Field

Inherits:
Object show all
Defined in:
lib/mongoid/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Field

Create the new field with a name and optional additional options. Valid options are :default

Options:

name: The name of the field as a Symbol. options: A Hash of options for the field.

Example:

Field.new(:score, :default => 0)



35
36
37
38
39
40
# File 'lib/mongoid/field.rb', line 35

def initialize(name, options = {})
  @name, @default = name, options[:default]
  @copyable = (@default.is_a?(Array) || @default.is_a?(Hash))
  @type = options[:type] || String
  @accessible = options.has_key?(:accessible) ? options[:accessible] : true
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/mongoid/field.rb', line 4

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/mongoid/field.rb', line 4

def type
  @type
end

Instance Method Details

#accessible?Boolean

Determine if the field is able to be accessible via a mass update.

Returns:

true if accessible, false if not.

Returns:



11
12
13
# File 'lib/mongoid/field.rb', line 11

def accessible?
  !!@accessible
end

#defaultObject

Get the default value for the field.

Returns:

The primitive value or a copy of the default.



20
21
22
# File 'lib/mongoid/field.rb', line 20

def default
  copy
end

#get(object) ⇒ Object

Used for retrieving the object out of the attributes hash.



49
50
51
# File 'lib/mongoid/field.rb', line 49

def get(object)
  type.get(object)
end

#set(object) ⇒ Object

Used for setting an object in the attributes hash. If nil is provided the default will get returned if it exists.



44
45
46
# File 'lib/mongoid/field.rb', line 44

def set(object)
  type.set(object)
end