Module: CommandTower::Deserializers::Clients::Types

Defined in:
app/deserializers/command_tower/deserializers/clients/types.rb

Overview

Composable type processors for response Result validation. Each raises CommandTower::Clients::Errors::DeserializationError intentionally — no broad rescue.

Defined Under Namespace

Classes: ArrayType, BooleanType, FloatType, IntegerType, ResultInstanceType, StringType, Type, UnionType

Class Method Summary collapse

Class Method Details

.array(element_type) ⇒ Object



29
30
31
# File 'app/deserializers/command_tower/deserializers/clients/types.rb', line 29

def array(element_type)
  ArrayType.new(normalize(element_type))
end

.booleanObject



21
22
23
# File 'app/deserializers/command_tower/deserializers/clients/types.rb', line 21

def boolean
  BooleanType.instance
end

.floatObject



25
26
27
# File 'app/deserializers/command_tower/deserializers/clients/types.rb', line 25

def float
  FloatType.instance
end

.integerObject



13
14
15
# File 'app/deserializers/command_tower/deserializers/clients/types.rb', line 13

def integer
  IntegerType.instance
end

.normalize(type) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/deserializers/command_tower/deserializers/clients/types.rb', line 39

def normalize(type)
  case type
  when Type
    type
  when Class
    unless type < Result
      raise ::CommandTower::Clients::Errors::ConfigurationError,
            "nested type must be a CommandTower::Deserializers::Clients::Result subclass (got: #{type})"
    end

    ResultInstanceType.new(type)
  when nil
    raise ::CommandTower::Clients::Errors::ConfigurationError, "type must not be nil"
  else
    raise ::CommandTower::Clients::Errors::ConfigurationError,
          "unknown type declaration: #{type.inspect}"
  end
end

.stringObject



17
18
19
# File 'app/deserializers/command_tower/deserializers/clients/types.rb', line 17

def string
  StringType.instance
end

.union(*member_types) ⇒ Object



33
34
35
36
37
# File 'app/deserializers/command_tower/deserializers/clients/types.rb', line 33

def union(*member_types)
  raise ::CommandTower::Clients::Errors::ConfigurationError, "Types.union requires at least one member" if member_types.empty?

  UnionType.new(member_types.map { |t| normalize(t) })
end