module DigitalOpera::Document::Fields

Public Instance Methods

define_default_method(object) click to toggle source

Define the method for getting the default on the document.

@api private

@example Define the method.

field.define_default_method(doc)

@note Ruby’s instance_exec was just too slow.

@param [ Class, Module ] object The class or module the field is

defined on.

@since 3.0.0

# File lib/digital_opera/document/fields/standard.rb, line 111
def define_default_method(object)
  object.__send__(:define_method, default_name, default_val)
end
evaluate_default_proc(doc) click to toggle source

Evaluate the default proc. In some cases we need to instance exec, in others we don’t.

@example Eval the default proc.

field.evaluate_default_proc(band)

@param [ Document ] doc The document.

@return [ Object ] The called proc.

@since 3.0.0

# File lib/digital_opera/document/fields/standard.rb, line 162
def evaluate_default_proc(doc)
  serialize_default(doc.__send__(default_name))
end
evaluated_default(doc) click to toggle source

Get the evaluated default.

@example Get the evaluated default.

field.evaluated_default.

@param [ Document ] doc The doc being applied to.

@return [ Object ] The default value.

@since 2.4.4

# File lib/digital_opera/document/fields/standard.rb, line 143
def evaluated_default(doc)
  if default_val.respond_to?(:call)
    evaluate_default_proc(doc)
  else
    serialize_default(default_val.__deep_copy__)
  end
end
included?(fields) click to toggle source

Is the field included in the fields that were returned from the database? We can apply the default if:

1. The field is included in an only limitation (field: 1)
2. The field is not excluded in a without limitation (field: 0)

@example Is the field included?

field.included?(fields)

@param [ Hash ] fields The field limitations.

@return [ true, false ] If the field was included.

@since 2.4.4

# File lib/digital_opera/document/fields/standard.rb, line 128
def included?(fields)
  (fields.values.first == 1 && fields[name.to_s] == 1) ||
    (fields.values.first == 0 && !fields.has_key?(name.to_s))
end
serialize_default(object) click to toggle source

This is used when default values need to be serialized. Most of the time just return the object.

@api private

@example Serialize the default value.

field.serialize_default(obj)

@param [ Object ] object The default.

@return [ Object ] The serialized default.

@since 3.0.0

# File lib/digital_opera/document/fields/standard.rb, line 179
def serialize_default(object)
  mongoize(object)
end