module Jet

Public Class Methods

all_are?(obj, *types) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/jet/core.rb, line 21
def failure?(obj)
  obj.respond_to?(:failure?) ? obj.failure? : !obj
end
is_a?(obj, *types) click to toggle source
# File lib/jet/core.rb, line 25
def is_a?(obj, *types)
  types.any? { |t| obj.is_a?(t) }
end
output(obj) click to toggle source
# File lib/jet/core.rb, line 65
def output(obj)
  obj.respond_to?(:output) ? obj.output : obj
end
success?(obj) click to toggle source
# File lib/jet/core.rb, line 29
def success?(obj)
  obj.respond_to?(:success?) ? obj.success? : obj
end
type_check!(desc, obj, *types) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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