Sha256: 19f9cc90c7cefe97f84aebc76a5d59778781dbdd8785f905e7a9cbcbdcb78bc0

Contents?: true

Size: 893 Bytes

Versions: 48

Compression:

Stored size: 893 Bytes

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  class Field

    attr_reader \
      :default,
      :name,
      :type

    # 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:
    #
    # <tt>Field.new(:score, :default => 0)</tt>
    def initialize(name, options = {})
      @name = name
      @default = options[:default]
      @type = options[:type] || String
    end

    # Used for setting an object in the attributes hash. If nil is provided the
    # default will get returned if it exists.
    def set(object)
      object.nil? ? default : type.set(object)
    end

    # Used for retrieving the object out of the attributes hash.
    def get(object)
      type.get(object)
    end

  end
end

Version data entries

48 entries across 48 versions & 2 rubygems

Version Path
mongoid-1.2.8 lib/mongoid/field.rb
mongoid-1.2.7 lib/mongoid/field.rb
mongoid-pre-2.0.0.pre lib/mongoid/field.rb
mongoid-1.2.6 lib/mongoid/field.rb
mongoid-1.2.5 lib/mongoid/field.rb
mongoid-1.2.4 lib/mongoid/field.rb
mongoid-1.2.3 lib/mongoid/field.rb
mongoid-1.2.2 lib/mongoid/field.rb
mongoid-1.2.1 lib/mongoid/field.rb
mongoid-1.2.0 lib/mongoid/field.rb
mongoid-1.1.4 lib/mongoid/field.rb
mongoid-1.1.3 lib/mongoid/field.rb
mongoid-1.1.2 lib/mongoid/field.rb
mongoid-1.1.1 lib/mongoid/field.rb
mongoid-1.1.0 lib/mongoid/field.rb
mongoid-1.0.6 lib/mongoid/field.rb
mongoid-1.0.5 lib/mongoid/field.rb
mongoid-1.0.4 lib/mongoid/field.rb
mongoid-1.0.3 lib/mongoid/field.rb
mongoid-1.0.2 lib/mongoid/field.rb