Class: ActionSpec::Schema::Scalar
- Defined in:
- lib/action_spec/schema/scalar.rb
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Attributes inherited from Base
#blank, #default, #description, #enum, #example, #examples, #length, #pattern, #range
Instance Method Summary collapse
- #blank_value(value) ⇒ Object
- #cast(value, context: nil, coerce:, result:, path:, field: nil) ⇒ Object
- #copy ⇒ Object
-
#initialize(type, options = {}) ⇒ Scalar
constructor
A new instance of Scalar.
Methods inherited from Base
#blank_allowed?, #custom_validation?, #materialize_missing, #validate_constraints
Constructor Details
#initialize(type, options = {}) ⇒ Scalar
Returns a new instance of Scalar.
8 9 10 11 |
# File 'lib/action_spec/schema/scalar.rb', line 8 def initialize(type, = {}) super() @type = type end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/action_spec/schema/scalar.rb', line 6 def type @type end |
Instance Method Details
#blank_value(value) ⇒ Object
33 34 35 |
# File 'lib/action_spec/schema/scalar.rb', line 33 def blank_value(value) TypeCaster.normalize(type) == :string ? value : nil end |
#cast(value, context: nil, coerce:, result:, path:, field: nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/action_spec/schema/scalar.rb', line 13 def cast(value, context: nil, coerce:, result:, path:, field: nil) candidate = TypeCaster.cast(type, value) rescue TypeCaster::CastError => error if field field.add_error(result, path:, type: :invalid_type, value:, context:, expected: error.expected) else result.add_error(path.join("."), :invalid_type, expected: error.expected) end Schema::Missing else return candidate if candidate.nil? validate_constraints(candidate, result:, path:, field:, context:) coerce ? candidate : value end |
#copy ⇒ Object
29 30 31 |
# File 'lib/action_spec/schema/scalar.rb', line 29 def copy self.class.new(type, default:, enum:, range:, pattern:, length:, blank:, desc: description, example:, examples:) end |