Class: Jet::Contract

Inherits:
Object
  • Object
show all
Defined in:
lib/jet/contract.rb,
lib/jet/contract/check.rb,
lib/jet/contract/builder.rb,
lib/jet/contract/version.rb,
lib/jet/contract/attribute.rb,
lib/jet/contract/check/set.rb,
lib/jet/contract/attribute/builder.rb

Defined Under Namespace

Classes: Attribute, Builder, Check

Constant Summary collapse

FLATTEN_ERROR_TYPES =
%i[
  contract_validation_failure
  check_each_failure
].freeze
MAJOR =
0
MINOR =
1
TINY =
0
VERSION =
[MAJOR, MINOR, TINY].join(".").freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, keys_in: [String, Symbol], keys_out: :to_sym) ⇒ Contract

Returns a new instance of Contract.



57
58
59
60
61
62
# File 'lib/jet/contract.rb', line 57

def initialize(attributes, keys_in: [String, Symbol], keys_out: :to_sym, **)
  @attributes = Jet.type_check_hash!("`attributes`", attributes, Attribute)
                   .transform_keys(&:to_s)

  @opts = { keys_in: _keys_in(keys_in), keys_out: _keys_out(keys_out) }
end

Class Attribute Details

.checksObject

Returns the value of attribute checks.



17
18
19
# File 'lib/jet/contract.rb', line 17

def checks
  @checks
end

.typesObject

Returns the value of attribute types.



17
18
19
# File 'lib/jet/contract.rb', line 17

def types
  @types
end

Class Method Details

.build(*args, &blk) ⇒ Object

Raises:

  • (ArgumentError)


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

def build(*args, &blk)
  raise ArgumentError, "no block given" unless block_given?
  Builder.new.tap { |b| b.instance_eval(&blk) }.call(*args)
end

.checks!(checks) ⇒ Object



24
25
26
# File 'lib/jet/contract.rb', line 24

def checks!(checks)
  validate_registry!("checks", checks, Check, :eql)
end

.types!(types) ⇒ Object



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

def types!(types)
  case types
  when :http
    Type::HTTP
  when :json
    Type::JSON
  when :strict
    Type::Strict
  else
    validate_registry!("types", types, Type, :string)
  end
end

.versionObject



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

def self.version
  VERSION
end

Instance Method Details

#[](key) ⇒ Object



69
70
71
# File 'lib/jet/contract.rb', line 69

def [](key)
  @attributes[key]
end

#attributesObject



73
74
75
# File 'lib/jet/contract.rb', line 73

def attributes
  @attributes.dup
end

#call(input) ⇒ Object



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

def call(input, **)
  results = check_attributes(filter_keys(input.to_h))
  failure(results, input) || success(results)
end

#optsObject



77
78
79
# File 'lib/jet/contract.rb', line 77

def opts
  @opts.dup
end

#rebuild(*args) ⇒ Object



81
82
83
# File 'lib/jet/contract.rb', line 81

def rebuild(*args)
  to_builder.(*args)
end

#to_builderObject



85
86
87
# File 'lib/jet/contract.rb', line 85

def to_builder
  Builder.new(@attributes.transform_values(&:to_builder))
end

#with(*other_contracts, **opts) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/jet/contract.rb', line 89

def with(*other_contracts, **opts)
  Jet.type_check_each!("`other_contracts`", other_contracts, Contract)

  self.class.new(
    other_contracts.each_with_object(attributes) { |c, atts| atts.merge!(c.attributes) },
    **self.opts.merge(opts)
  )
end