Class: Moon::DataModel::Validators::Type

Inherits:
Base
  • Object
show all
Defined in:
lib/data_model/validators/type.rb

Overview

Ensures that the provided value is the Type required, usually appended first to a validators stack. see Field#validators

Class Attribute Summary collapse

Attributes inherited from Base

#id, #meta, #name, #note, #tags

Attributes inherited from Metal

#dmid

Instance Method Summary collapse

Methods inherited from Metal

#initialize, #pre_initialize, #to_h

Methods included from Fields

adjust_field_options, included

Methods included from Fields::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?

Constructor Details

This class inherits a constructor from Moon::DataModel::Metal

Class Attribute Details

.defaultMoon::DataModel::TypeValidators::Base


15
16
17
# File 'lib/data_model/validators/type.rb', line 15

def default
  @default
end

Instance Method Details

#initialize_options(options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Options Hash (options):


24
25
26
27
28
29
# File 'lib/data_model/validators/type.rb', line 24

def initialize_options(options)
  super
  @type = options.fetch(:type)
  @allow_nil = options.fetch(:allow_nil, false)
  @validator = options[:validator]
end

#post_initializeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


36
37
38
39
# File 'lib/data_model/validators/type.rb', line 36

def post_initialize
  # TODO, find some way of asserting classes
  validator.send :ensure_obj_is_a_type, @type
end

#test_valid(value) ⇒ Object


42
43
44
45
# File 'lib/data_model/validators/type.rb', line 42

def test_valid(value)
  validator.check_type(@type, value,
    quiet: true, allow_nil: @allow_nil, ctx: @ctx)
end

#validatorObject


31
32
33
# File 'lib/data_model/validators/type.rb', line 31

def validator
  @validator || self.class.default
end