Sha256: b7c675ad2f945250524001d195e62d2220f91a00ba7a52af051747f9159b55c3
Contents?: true
Size: 1.88 KB
Versions: 8
Compression:
Stored size: 1.88 KB
Contents
# encoding: utf-8 module Mongoid #:nodoc module Fields #:nodoc def self.included(base) base.class_eval do extend ClassMethods # Set up the class attributes that must be available to all subclasses. # These include defaults, fields class_inheritable_accessor :fields self.fields = {} delegate :defaults, :fields, :to => "self.class" end end module ClassMethods #:nodoc # Defines all the fields that are accessable on the Document # For each field that is defined, a getter and setter will be # added as an instance method to the Document. # # Options: # # name: The name of the field, as a +Symbol+. # options: A +Hash+ of options to supply to the +Field+. # # Example: # # <tt>field :score, :default => 0</tt> def field(name, options = {}) access = name.to_s set_field(access, options) end # Returns the default values for the fields on the document def defaults fields.inject({}) do |defs,(field_name,field)| next(defs) if field.default.nil? defs[field_name.to_s] = field.default defs end end protected # Define a field attribute for the +Document+. def set_field(name, options = {}) meth = options.delete(:as) || name fields[name] = Field.new(name, options) create_accessors(name, meth, options) add_dirty_methods(name) end # Create the field accessors. def create_accessors(name, meth, options = {}) define_method(meth) { read_attribute(name) } define_method("#{meth}=") { |value| write_attribute(name, value) } define_method("#{meth}?") do attr = read_attribute(name) (options[:type] == Boolean) ? attr == true : attr.present? end end end end end
Version data entries
8 entries across 8 versions & 4 rubygems