Module: Brut::Framework::FussyTypeEnforcement
- Included in:
- Brut::FrontEnd::Forms::InputDefinition, Brut::FrontEnd::Forms::RadioButtonGroupInputDefinition, Brut::FrontEnd::Forms::SelectInputDefinition
- Defined in:
- lib/brut/framework/fussy_type_enforcement.rb
Overview
Include this to enable methods to help with type checking. Generally, you should not use this. You should only really use this if all of the following are true:
- Passing in the wrong type Would Be Bad.
- The developer passing it in would not easily be able to figure out what went wrong.
Instance Method Summary collapse
-
#type!(value, type_descriptor, variable_name_for_error_message, required: false, coerce: false) ⇒ Object
Perform basic type checking, ideally inside a constructor when assigning ivars.
Instance Method Details
#type!(value, type_descriptor, variable_name_for_error_message, required: false, coerce: false) ⇒ Object
Perform basic type checking, ideally inside a constructor when assigning ivars. This is really intended for internal classes that will not be exposed to user input, but rather to catch programmer bugs and programmer mis-use.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/brut/framework/fussy_type_enforcement.rb', line 20 def type!(value,type_descriptor,, required: false, coerce: false) value_blank = value.nil? || ( value.kind_of?(String) && value.strip == "" ) if !required && value_blank return value end if required && value_blank raise ArgumentError.new("'#{}' must have a value") end if type_descriptor.kind_of?(Class) coerced_value = coerce ? value.send(coerce) : value if !coerced_value.kind_of?(type_descriptor) class_description = if coerce "but was a #{value.class}, coerced to a #{coerced_value.class} via #{coerce}" else "but was a #{value.class}" end raise ArgumentError.new("'#{}' must be a #{type_descriptor}, #{class_description} (value as a string is #{value})") end value = coerced_value elsif type_descriptor.kind_of?(Array) if !type_descriptor.include?(value) description_of_values = type_descriptor.map { |value| "#{value} (a #{value.class})" }.join(", ") raise ArgumentError.new("'#{}' must be one of #{description_of_values}, but was a #{value.class} (value as a string is #{value})") end else raise ArgumentError.new("Use of type! with a #{type_descriptor.class} (#{type_descriptor}) is not supported") end value end |