Module: Moon::DataModel::Fields

Includes:
InstanceMethods
Included in:
Metal
Defined in:
lib/data_model/fields.rb,
lib/data_model/fields/modelling.rb,
lib/data_model/fields/class_methods.rb,
lib/data_model/fields/model_coercion.rb,
lib/data_model/fields/instance_methods.rb

Overview

The backbone of DataModel's overall system

Defined Under Namespace

Modules: ClassMethods, InstanceMethods, ModelCoercion, Modelling

Class Method Summary collapse

Methods included from InstanceMethods

#[], #[]=, #assoc, #each_field, #each_field_name, #each_field_with_value, #each_pair, #each_value, #field_get, #field_set, #field_set!, #fields=, #fields_hash, #initialize_fields, #invalid?, #post_initialize_fields, #pre_initialize_fields, #reset_field, #reset_fields, #update_fields, #update_fields!, #valid?, #validate, #validate_fields?

Class Method Details

.adjust_field_options(klass, options) ⇒ Hash

Patches the provided options hash.


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/data_model/fields.rb', line 12

def self.adjust_field_options(klass, options)
  # set the class default settings
  klass.each_field_setting do |key, value|
    options[key] = value
  end

  # if the default value is set to nil, and allow_nil hasn't already
  # been set, then the field is allowed to be nil.
  if options.key?(:default) && options[:default].nil?
    options[:allow_nil] = true unless options.key?(:allow_nil)
  end

  # if default value was not set, but the field allows nil,
  # then the default value is nil
  if !options.key?(:default) && options[:allow_nil]
    options[:default] = nil
  end

  # if no type was given, assume it allows anything, therefore Object
  unless options.key?(:type)
    options[:type] = ::Object
  end

  unless (obj = options[:default]).is_a?(Proc)
    options[:default] = ->(_, _) { obj.maybe_dup }
  end

  options
end

.included(mod) ⇒ Object


45
46
47
# File 'lib/data_model/fields.rb', line 45

def self.included(mod)
  mod.extend         ClassMethods
end