Module: PhModel::Concerns::AttributeTypeValidation

Extended by:
ActiveSupport::Concern
Included in:
PhModel
Defined in:
lib/ph_model/concerns/attribute_type_validation.rb

Overview

This will validate any attribute defined with ‘type: ` the type value should be a class, or the special notation `[SomeClass]` which is meant to designate that this is a collection of those classes

‘no_type_check: true` can be optionally passed to disable this check (other features might use the `type` column)

Instance Method Summary collapse

Instance Method Details

#ensure_typed_attributes_classObject



16
17
18
19
20
21
22
# File 'lib/ph_model/concerns/attribute_type_validation.rb', line 16

def ensure_typed_attributes_class
  self.class.attributes.each do |attribute_name, info|
    if info[:type] && !info[:no_type_check] && !type_match?(attribute_name)
      errors.add(attribute_name, "must be #{info[:type].inspect}, was #{type_summary(attribute_name)}")
    end
  end
end

#type_match?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ph_model/concerns/attribute_type_validation.rb', line 34

def type_match?(attribute_name)
  value = send(attribute_name)
  info = self.class.attributes[attribute_name]
  type = info[:type]

  case type
  when nil
    true
  when Array
    if type.count != 1
      raise ArgumentError, "don't know how to handle type: #{type.inspect}"
    else
      if value.kind_of? Array
        value.all? { |item| item.is_a?(type.first) }
      else
        false
      end
    end
  else
    if !info[:required] && value.nil?
      true
    else
      value.is_a?(type)
    end
  end
end

#type_summary(attribute_name) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/ph_model/concerns/attribute_type_validation.rb', line 24

def type_summary(attribute_name)
  value = send(attribute_name)
  case value
  when Array
    value.map { |val| val.respond_to?(:model_name) ? val.model_name.name : val.class.name }.uniq
  else
    value.class
  end
end