Class: StrongCSV::Types::String
- Defined in:
- lib/strong_csv/types/string.rb
Overview
String type
Instance Method Summary collapse
- #cast(value) ⇒ ValueResult
-
#initialize(within: nil) ⇒ String
constructor
A new instance of String.
Constructor Details
#initialize(within: nil) ⇒ String
Returns a new instance of String.
8 9 10 11 12 13 |
# File 'lib/strong_csv/types/string.rb', line 8 def initialize(within: nil) raise ArgumentError, "`within` must be a Range" unless within.nil? || within.is_a?(Range) super() @within = within end |
Instance Method Details
#cast(value) ⇒ ValueResult
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/strong_csv/types/string.rb', line 17 def cast(value) return ValueResult.new(original_value: value, error_messages: ["`#{value.inspect}` can't be casted to String"]) if value.nil? casted = String(value) if @within && !@within.cover?(casted.size) ValueResult.new(original_value: value, error_messages: ["The length of `#{value.inspect}` is out of range `#{@within.inspect}`"]) else ValueResult.new(value: casted, original_value: value) end end |