Class: StrongCSV::Types::String

Inherits:
Base
  • Object
show all
Defined in:
lib/strong_csv/types/string.rb

Overview

String type

Instance Method Summary collapse

Constructor Details

#initialize(within: nil) ⇒ String

Returns a new instance of String.

Parameters:

  • within (Range, nil) (defaults to: nil)

Raises:

  • (ArgumentError)


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

Parameters:

  • value (Object)

    Value to be casted to Boolean

Returns:



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