Module: Jet

Defined in:
lib/jet/core.rb,
lib/jet/result.rb,
lib/jet/core/version.rb,
lib/jet/core/instance_registry.rb

Defined Under Namespace

Modules: Core Classes: Result

Class Method Summary collapse

Class Method Details

.all_are?(obj, *types) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/jet/core.rb', line 7

def all_are?(obj, *types)
  obj.all? { |v| is_a?(v, *types) }
end

.context(obj, additional_context = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/jet/core.rb', line 11

def context(obj, additional_context = nil)
  if obj.respond_to?(:context)
    obj.context
  elsif obj.is_a?(Hash)
    obj
  else
    {}
  end.yield_self { |ctx| additional_context ? ctx.merge(additional_context) : ctx }
end

.failure?(obj) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/jet/core.rb', line 21

def failure?(obj)
  obj.respond_to?(:failure?) ? obj.failure? : !obj
end

.is_a?(obj, *types) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/jet/core.rb', line 25

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

.output(obj) ⇒ Object



65
66
67
# File 'lib/jet/core.rb', line 65

def output(obj)
  obj.respond_to?(:output) ? obj.output : obj
end

.success?(obj) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/jet/core.rb', line 29

def success?(obj)
  obj.respond_to?(:success?) ? obj.success? : obj
end

.type_check!(desc, obj, *types) ⇒ Object

Raises:

  • (TypeError)


33
34
35
36
37
# File 'lib/jet/core.rb', line 33

def type_check!(desc, obj, *types)
  raise TypeError, "#{desc} must be #{type_error_desc(*types)}" unless
    is_a?(obj, *types)
  obj
end

.type_check_each!(desc, obj, *types) ⇒ Object

Raises:

  • (TypeError)


39
40
41
42
43
44
45
46
# File 'lib/jet/core.rb', line 39

def type_check_each!(desc, obj, *types)
  type_check!(desc, obj, Array)

  raise TypeError, "elements of #{desc} must be #{type_error_desc(*types)}" unless
    all_are?(obj, *types)

  obj
end

.type_check_hash!(desc, obj, *types, key_type: nil) ⇒ Object

Raises:

  • (TypeError)


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jet/core.rb', line 48

def type_check_hash!(desc, obj, *types, key_type: nil)
  type_check!(desc, obj, Hash)

  raise TypeError, "#{desc} keys must all be #{type_error_desc(*key_type)}" unless
    !key_type || all_are?(obj.keys, *key_type)

  raise TypeError, "#{desc} values must all be #{type_error_desc(*types)}" unless
    all_are?(obj.values, *types)

  obj
end

.type_error_desc(*types) ⇒ Object



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

def type_error_desc(*types)
  return types.join(" or ") if types.size < 3
  "#{types[0..-2].join(', ')} or #{types.last}"
end