Class: StrongCSV::TypeWrapper
- Inherits:
-
Struct
- Object
- Struct
- StrongCSV::TypeWrapper
- Defined in:
- lib/strong_csv/type_wrapper.rb
Overview
TypeWrapper holds the ‘type` along with `name` and `block`. It might be useful to store metadata for the type, such as `error_message`.
Instance Attribute Summary collapse
- #block ⇒ Proc
-
#error_message ⇒ String?
The error message returned if #cast fails.
-
#name ⇒ Symbol, Integer
The name for the type.
- #type ⇒ StrongCSV::Type::Base
Instance Method Summary collapse
Instance Attribute Details
#block ⇒ Proc
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/strong_csv/type_wrapper.rb', line 17 TypeWrapper = Struct.new(:name, :type, :error_message, :block, keyword_init: true) do def cast(value) value_result = type.cast(value) casted = block && value_result.success? ? block.call(value_result.value) : value_result.value error = if value_result.success? nil else ? [] : value_result. end [casted, error] end end |
#error_message ⇒ String?
Returns The error message returned if #cast fails. If omitted, the default error message will be used.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/strong_csv/type_wrapper.rb', line 17 TypeWrapper = Struct.new(:name, :type, :error_message, :block, keyword_init: true) do def cast(value) value_result = type.cast(value) casted = block && value_result.success? ? block.call(value_result.value) : value_result.value error = if value_result.success? nil else ? [] : value_result. end [casted, error] end end |
#name ⇒ Symbol, Integer
Returns The name for the type. This is the CSV header name. If the CSV does not have its header, Integer should be set.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/strong_csv/type_wrapper.rb', line 17 TypeWrapper = Struct.new(:name, :type, :error_message, :block, keyword_init: true) do def cast(value) value_result = type.cast(value) casted = block && value_result.success? ? block.call(value_result.value) : value_result.value error = if value_result.success? nil else ? [] : value_result. end [casted, error] end end |
#type ⇒ StrongCSV::Type::Base
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/strong_csv/type_wrapper.rb', line 17 TypeWrapper = Struct.new(:name, :type, :error_message, :block, keyword_init: true) do def cast(value) value_result = type.cast(value) casted = block && value_result.success? ? block.call(value_result.value) : value_result.value error = if value_result.success? nil else ? [] : value_result. end [casted, error] end end |
Instance Method Details
#cast(value) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/strong_csv/type_wrapper.rb', line 18 def cast(value) value_result = type.cast(value) casted = block && value_result.success? ? block.call(value_result.value) : value_result.value error = if value_result.success? nil else ? [] : value_result. end [casted, error] end |