Sha256: 3b2f4a11131a09b3ece00d626b32887fbf30d19988619beefa286afc67f25514

Contents?: true

Size: 1.32 KB

Versions: 8

Compression:

Stored size: 1.32 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  class Field
    attr_reader :name, :type

    # Determine if the field is able to be accessible via a mass update.
    #
    # Returns:
    #
    # true if accessible, false if not.
    def accessible?
      !!@accessible
    end

    # Get the default value for the field.
    #
    # Returns:
    #
    # The primitive value or a copy of the default.
    def default
      (@default.is_a?(Array) || @default.is_a?(Hash)) ? @default.dup : @default
    end

    # 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, @default = name, options[:default]
      @type = options[:type] || String
      @accessible = options.has_key?(:accessible) ? options[:accessible] : true
    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

8 entries across 8 versions & 2 rubygems

Version Path
mongoid-pre-2.0.0.beta1 lib/mongoid/field.rb
mongoid-2.0.0.alpha lib/mongoid/field.rb
mongoid-1.2.14 lib/mongoid/field.rb
mongoid-1.2.13 lib/mongoid/field.rb
mongoid-1.2.12 lib/mongoid/field.rb
mongoid-1.2.11 lib/mongoid/field.rb
mongoid-1.2.10 lib/mongoid/field.rb
mongoid-1.2.9 lib/mongoid/field.rb