Class: StrongCSV::TypeWrapper

Inherits:
Struct
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#blockProc

Returns:

  • (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
              error_message ? [error_message] : value_result.error_messages
            end

    [casted, error]
  end
end

#error_messageString?

Returns The error message returned if #cast fails. If omitted, the default error message will be used.

Returns:

  • (String, nil)

    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
              error_message ? [error_message] : value_result.error_messages
            end

    [casted, error]
  end
end

#nameSymbol, 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.

Returns:

  • (Symbol, Integer)

    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
              error_message ? [error_message] : value_result.error_messages
            end

    [casted, error]
  end
end

#typeStrongCSV::Type::Base

Returns:

  • (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
              error_message ? [error_message] : value_result.error_messages
            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
            error_message ? [error_message] : value_result.error_messages
          end

  [casted, error]
end