Module: Alchemrest::Transforms

Defined in:
lib/alchemrest/transforms.rb,
lib/alchemrest/transforms/enum.rb,
lib/alchemrest/transforms/typed.rb,
lib/alchemrest/transforms/union.rb,
lib/alchemrest/transforms/number.rb,
lib/alchemrest/transforms/to_type.rb,
lib/alchemrest/transforms/iso_time.rb,
lib/alchemrest/transforms/from_type.rb,
lib/alchemrest/transforms/constraint.rb,
lib/alchemrest/transforms/epoch_time.rb,
lib/alchemrest/transforms/from_chain.rb,
lib/alchemrest/transforms/loose_hash.rb,
lib/alchemrest/transforms/to_decimal.rb,
lib/alchemrest/transforms/from_number.rb,
lib/alchemrest/transforms/from_string.rb,
lib/alchemrest/transforms/json_number.rb,
lib/alchemrest/transforms/output_type.rb,
lib/alchemrest/transforms/constrainable.rb,
lib/alchemrest/transforms/date_transform.rb,
lib/alchemrest/transforms/money_transform.rb,
lib/alchemrest/transforms/with_constraint.rb,
lib/alchemrest/transforms/constraint/block.rb,
lib/alchemrest/transforms/constraint/in_list.rb,
lib/alchemrest/transforms/constraint/is_uuid.rb,
lib/alchemrest/transforms/constraint_builder.rb,
lib/alchemrest/transforms/constraint/less_than.rb,
lib/alchemrest/transforms/constraint/max_length.rb,
lib/alchemrest/transforms/constraint/min_length.rb,
lib/alchemrest/transforms/constraint/greater_than.rb,
lib/alchemrest/transforms/constraint/matches_regex.rb,
lib/alchemrest/transforms/constraint/is_instance_of.rb,
lib/alchemrest/transforms/constraint/less_than_or_eq.rb,
lib/alchemrest/transforms/to_type/transforms_selector.rb,
lib/alchemrest/transforms/constraint/greater_than_or_eq.rb,
lib/alchemrest/transforms/constraint_builder/for_number.rb,
lib/alchemrest/transforms/constraint_builder/for_string.rb,
lib/alchemrest/transforms/base_to_type_transform_registry.rb,
lib/alchemrest/transforms/to_type/from_string_to_time_selector.rb,
lib/alchemrest/transforms/from_number/to_type_transform_registry.rb,
lib/alchemrest/transforms/from_string/to_type_transform_registry.rb,
lib/alchemrest/transforms/from_type/empty_to_type_transform_registry.rb

Overview

The Transforms module is passed into the block provided to ‘Alchemrest::Data.schema`, giving developers an easy way to define the transforms for a given data class

Examples:

Using ‘Alchemrest::Transforms` inside a `schema` block

class User < Alchemrest::Data
  schema do |s|
    # `s` is `Alchemrest::Transforms`
  end
end

Defined Under Namespace

Modules: FromChain Classes: BaseToTypeTransformRegistry, Constrainable, Constraint, ConstraintBuilder, DateTransform, Enum, EpochTime, FromNumber, FromString, FromType, IsoTime, JsonNumber, LooseHash, MoneyTransform, Number, OutputType, ToDecimal, ToType, Typed, Union, WithConstraint

Class Method Summary collapse

Class Method Details

.booleanObject

A transformation that results in a boolean. If the input is not a boolean, will result in a ‘Alchemrest::MorpherTransformError`



44
45
46
# File 'lib/alchemrest/transforms.rb', line 44

def self.boolean
  Typed.new(transform: Morpher::Transform::BOOLEAN, output_type: OutputType.simple(T::Boolean))
end

.dateObject

A transformation that results in a Date object. If the input is not an iso8601 date string, will result in a ‘Alchemrest::MorpherTransformError`



50
51
52
# File 'lib/alchemrest/transforms.rb', line 50

def self.date
  Typed.new(transform: DateTransform.new, output_type: OutputType.simple(Date))
end

.enum(enum) ⇒ Object

A transformation that results in a String from a predefined list. If the original value is not a string from the predefined list, it will result in a ‘Alchemrest::MorpherTransformError`

Parameters:

  • the (Array<Symbol>)

    list of valid values for the field



69
70
71
# File 'lib/alchemrest/transforms.rb', line 69

def self.enum(enum)
  Typed.new(transform: Enum.new(enum), output_type: OutputType.simple(T.any(Symbol, String)))
end

.floatObject

A transformation that results in a float. Must be a float in JSON, we will coerce strings into numbers. Will result in a ‘Alchemrest::MorpherTransformError` if not a float. Note, if the api sometimes returns a float and sometimes returns an integer, see #number instead.



29
30
31
# File 'lib/alchemrest/transforms.rb', line 29

def self.float
  Typed.new(transform: Morpher::Transform::FLOAT, output_type: OutputType.simple(Float))
end

.fromObject



33
34
35
# File 'lib/alchemrest/transforms.rb', line 33

def self.from
  FromChain
end

.integerObject

A transformation that results in an integer number. Must be a true integer in JSON, not a string as number. Will result in a ‘Alchemrest::MorpherTransformError` if not an integer number.



16
17
18
# File 'lib/alchemrest/transforms.rb', line 16

def self.integer
  Typed.new(transform: Morpher::Transform::INTEGER, output_type: OutputType.simple(Integer))
end

.many_of(klass) ⇒ Object



89
90
91
# File 'lib/alchemrest/transforms.rb', line 89

def self.many_of(klass)
  klass::TRANSFORM.array
end

.money(unit) ⇒ Object

A transformation that creates a Money object. You can indicate whether the original amount is in dollars our cents via the unit param. If the original value is not numeric, it will result in a ‘Alchemrest::MorpherTransformError`

Parameters:

  • (:dollars (Symbol)

    | :cents) the unit for the original amount



60
61
62
# File 'lib/alchemrest/transforms.rb', line 60

def self.money(unit)
  Typed.new(transform: MoneyTransform.new(unit), output_type: OutputType.simple(Money))
end

.numberObject

A transformation that results in some kind of numeric type, generally either a Float or Integer. If the input is not Numeric, will result in a ‘Alchemrest::MorpherTransformError`



39
40
41
# File 'lib/alchemrest/transforms.rb', line 39

def self.number
  Typed.new(transform: Number.new, output_type: OutputType.simple(T.any(Float, Integer)))
end

.one_of(klass_or_hash) ⇒ Object

A transformation that results in an Alchemrest::Data class. You can either provide a klass or a Hash with a ‘discriminator` value for polymorphic data types

Parameters:

  • Either (Class, Hash<Symbol, Class>)

    an ‘Alchemrest::Data` class, or a hash of classes along with a `discriminator` key for polymorphic use cases



80
81
82
83
84
85
86
87
# File 'lib/alchemrest/transforms.rb', line 80

def self.one_of(klass_or_hash)
  if klass_or_hash.instance_of? Hash
    klasses = klass_or_hash.except(:discriminator)
    Union.new(types: klasses, discriminator: klass_or_hash.fetch(:discriminator))
  else
    klass_or_hash::TRANSFORM
  end
end

.stringObject

A transformation that results in a string. Must be a true string in JSON, we will not coerce booleans or number to strings. Will result in a ‘Alchemrest::MorpherTransformError` if not a string.



22
23
24
# File 'lib/alchemrest/transforms.rb', line 22

def self.string
  Typed.new(transform: Morpher::Transform::STRING, output_type: OutputType.simple(String))
end