Class: Jet::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/jet/type.rb,
lib/jet/type/http.rb,
lib/jet/type/json.rb,
lib/jet/type/strict.rb,
lib/jet/type/version.rb,
lib/jet/type/coercion.rb

Defined Under Namespace

Modules: HTTP, JSON, Strict Classes: Coercion

Constant Summary collapse

MAJOR =
0
MINOR =
1
TINY =
0
VERSION =
[MAJOR, MINOR, TINY].join(".").freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *types, coercions: [], filter: nil, &blk) ⇒ Type

Returns a new instance of Type.



22
23
24
25
26
27
28
29
# File 'lib/jet/type.rb', line 22

def initialize(name, *types, coercions: [], filter: nil, &blk)
  @name = name.to_sym
  @coercions = coercions.dup
  @filter = filter
  @types = Jet.type_check_each!("`types`", types, Class, Module)
  instance_eval(&blk) if block_given?
  @coercions.freeze
end

Instance Attribute Details

#coercionsObject (readonly)

Returns the value of attribute coercions.



20
21
22
# File 'lib/jet/type.rb', line 20

def coercions
  @coercions
end

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/jet/type.rb', line 20

def name
  @name
end

#typesObject (readonly)

Returns the value of attribute types.



20
21
22
# File 'lib/jet/type.rb', line 20

def types
  @types
end

Class Method Details

.versionObject



10
11
12
# File 'lib/jet/type/version.rb', line 10

def self.version
  VERSION
end

.with(type, *types, name: nil, &blk) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/jet/type.rb', line 9

def self.with(type, *types, name: nil, &blk)
  Jet.type_check!("`type`", type, Type)
  new(
    name || type.name,
    *[type.types, types].flatten.uniq,
    coercions: type.coercions,
    filter: type.filter,
    &blk
  )
end

Instance Method Details

#call(input) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jet/type.rb', line 31

def call(input)
  return process_output(input) if type_match?(input)

  @coercions.each do |coercion|
    result = coercion.(input)
    return process_output(result.output) if result.success?
    return result if result.failure? && result != :no_coercion_match
  end

  failure(input: input)
end

#filter(callable = nil, &blk) ⇒ Object



43
44
45
46
# File 'lib/jet/type.rb', line 43

def filter(callable = nil, &blk)
  return @filter unless callable || block_given?
  @filter = Core.block_or_callable!(callable, &blk)
end

#inspectObject



48
49
50
# File 'lib/jet/type.rb', line 48

def inspect
  "#<#{self.class.name}:#{name}>"
end

#maybeObject



52
53
54
# File 'lib/jet/type.rb', line 52

def maybe
  @maybe ||= maybe? ? self : self.class.with(self, NilClass)
end

#maybe?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/jet/type.rb', line 56

def maybe?
  types.include?(NilClass)
end

#to_symObject



60
61
62
# File 'lib/jet/type.rb', line 60

def to_sym
  name
end

#type_match?(obj) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/jet/type.rb', line 64

def type_match?(obj)
  types.any? { |t| obj.is_a?(t) }
end