Class: StrongCSV::ValueResult

Inherits:
Object
  • Object
show all
Defined in:
lib/strong_csv/value_result.rb

Overview

ValueResult represents whether a CSV field is valid or not, and contains its casted value if it’s valid.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_value:, value: DEFAULT_VALUE, error_messages: nil) ⇒ ValueResult

Returns a new instance of ValueResult.



12
13
14
15
16
# File 'lib/strong_csv/value_result.rb', line 12

def initialize(original_value:, value: DEFAULT_VALUE, error_messages: nil)
  @value = value
  @original_value = original_value
  @error_messages = error_messages
end

Instance Attribute Details

#error_messagesArray<String>? (readonly)

Returns The error messages for the field.

Returns:

  • (Array<String>, nil)

    The error messages for the field.



10
11
12
# File 'lib/strong_csv/value_result.rb', line 10

def error_messages
  @error_messages
end

Instance Method Details

#success?Boolean

It returns true if the value is successfully casted.

Returns:

  • (Boolean)


26
27
28
# File 'lib/strong_csv/value_result.rb', line 26

def success?
  @error_messages.nil?
end

#value::Float, ...

Returns The casted value if it’s valid. Otherwise, returns the original value.

Returns:

  • (::Float, ::Integer, ::Object, ::String, ::Range, Boolean, ::Time, nil)

    The casted value if it’s valid. Otherwise, returns the original value.



19
20
21
# File 'lib/strong_csv/value_result.rb', line 19

def value
  success? ? @value : @original_value
end