Module: CMDx::Coercions::Array

Extended by:
Array
Included in:
Array
Defined in:
lib/cmdx/coercions/array.rb

Overview

Converts various input types to Array format

Handles conversion from strings that look like JSON arrays and other values that can be converted to arrays using Ruby’s Array() method.

Instance Method Summary collapse

Instance Method Details

#call(value, options = {}) ⇒ Array

Converts a value to an Array

Examples:

Convert a JSON-like string to an array

Array.call("[1, 2, 3]") # => [1, 2, 3]

Convert other values using Array()

Array.call("hello")     # => ["hello"]
Array.call(42)          # => [42]
Array.call(nil)         # => []

Parameters:

  • value (Object)

    The value to convert to an array

  • options (Hash) (defaults to: {})

    Optional configuration parameters (currently unused)

Options Hash (options):

  • :unused (Object)

    Currently no options are used

Returns:

  • (Array)

    The converted array value

Raises:

  • (JSON::ParserError)

    If the string value contains invalid JSON



31
32
33
34
35
36
37
# File 'lib/cmdx/coercions/array.rb', line 31

def call(value, options = {})
  if value.is_a?(::String) && value.start_with?("[")
    JSON.parse(value)
  else
    Array(value)
  end
end