Class: Mongoid::Field

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

Overview

Defines the behaviour for defined fields in the document.

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.

Examples:

Create the new field.

Field.new(:name, :type => String)

Parameters:

  • options (Hash) (defaults to: {})

    The field options.

Options Hash (options):

  • :type (Class)

    The class of the field.

  • :default (Object)

    The default value for the field.

  • :label (String)

    The field’s label.

Since:

  • 1.0.0



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

def initialize(name, options = {})
  @type = options[:type] || Object
  @name, @default, @label = name, options[:default], options[:label]
  @copyable = (@default.is_a?(Array) || @default.is_a?(Hash))
  @options = options
  check_default!
end

Instance Attribute Details

#copyableObject (readonly)

Returns the value of attribute copyable.



8
9
10
# File 'lib/mongoid/field.rb', line 8

def copyable
  @copyable
end

#klassObject (readonly)

Returns the value of attribute klass.



8
9
10
# File 'lib/mongoid/field.rb', line 8

def klass
  @klass
end

#labelObject (readonly)

Returns the value of attribute label.



8
9
10
# File 'lib/mongoid/field.rb', line 8

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/mongoid/field.rb', line 8

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/mongoid/field.rb', line 8

def options
  @options
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/mongoid/field.rb', line 7

def type
  @type
end

Instance Method Details

#defaultObject

Get the default value for the field.

Examples:

Get the default.

field.default

Returns:

  • (Object)

    The typecast default value.

Since:

  • 1.0.0



18
19
20
# File 'lib/mongoid/field.rb', line 18

def default
  copy.respond_to?(:call) ? copy : set(copy)
end

#get(object) ⇒ Object

Used for retrieving the object out of the attributes hash.

Examples:

Get the value.

field.get("Value")

Parameters:

  • The (Object)

    object to cast from the database.

Returns:

  • (Object)

    The converted value.

Since:

  • 1.0.0



79
80
81
# File 'lib/mongoid/field.rb', line 79

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.

If the field is an identity field, ie an id, it performs the necessary cast.

Examples:

Get the setter value.

field.set("New Value")

Parameters:

  • object (Object)

    The value to cast to a database value.

Returns:

  • (Object)

    The typecast value.

Since:

  • 1.0.0



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mongoid/field.rb', line 57

def set(object)
  unless options[:identity]
    type.set(object)
  else
    if object.blank?
      type.set(object) if object.is_a?(Array)
    else
      options[:metadata].constraint.convert(object)
    end
  end
end