Module: Lean::Attributes::CoercionHelpers Private

Defined in:
lib/lean-attributes/attributes/coercion_helpers.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Incredibly straightforward methods that coerce some types into other common types. You may find that you need to override these methods, depending on your use case.

Since:

  • 0.1.0

Constant Summary collapse

METHOD_BODIES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Method body strings used to precompile coerce_<attribute> methods

{
  Array:      'Array(value)',
  BigDecimal: 'BigDecimal.new(value, 0)',
  Boolean:    'value ? true : false',
  Date:       'Date.parse(value)',
  DateTime:   'DateTime.parse(value)',
  Float:      'value.to_f',
  Integer:    'value.to_i',
  String:     'value.to_s',
  Symbol:     'value.to_s.to_sym',
  Time:       'Time.parse(value).utc'
}.freeze

Class Method Summary collapse

Class Method Details

.method_body_for_type(type) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Fetches or generates the method body for coercing a value to this type

Parameters:

  • type (Symbol)

    based on class name e.g. :DateTime

Returns:

  • (String)

    method body for coercion to type

See Also:

Since:

  • 0.2.0



37
38
39
40
# File 'lib/lean-attributes/attributes/coercion_helpers.rb', line 37

def self.method_body_for_type(type)
  METHOD_BODIES[type] ||
    "value.is_a?(#{type}) ? value : #{type}.new(value)"
end