Class: Jet::Contract::Attribute

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

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, checks = nil, contract: nil, each: nil, required: true) ⇒ Attribute

Returns a new instance of Attribute.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jet/contract/attribute.rb', line 16

def initialize(type, checks = nil, contract: nil, each: nil, required: true, **)
  @type = Jet.type_check!("`type`", type, Type)
  @checks = Jet.type_check!("`checks`", checks, Check::Set, NilClass)

  raise ArgumentError, "cannot set both :contract and :each" if contract && each

  @opts = {
    contract: Jet.type_check!(":contract", contract, Contract, NilClass),
    each: Jet.type_check!(":each", each, Attribute, NilClass),
    required: required ? true : false
  }
end

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



9
10
11
# File 'lib/jet/contract/attribute.rb', line 9

def checks
  @checks
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/jet/contract/attribute.rb', line 9

def type
  @type
end

Class Method Details

.build(*args, &blk) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
# File 'lib/jet/contract/attribute.rb', line 11

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

Instance Method Details

#call(input, at = []) ⇒ Object



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

def call(input, at = [])
  coerce(input).yield_self { |r| result_at(Jet.failure?(r) ? r : check(r.output), at) }
end

#check(output) ⇒ Object



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

def check(output)
  return Result.success if output.nil?
  checks&.(output)&.tap { |r| return r if r.failure? }
  check_contract(output) || check_each(output) || Result.success(output)
end

#coerce(input) ⇒ Object



39
40
41
# File 'lib/jet/contract/attribute.rb', line 39

def coerce(input)
  type.(input)
end

#is?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/jet/contract/attribute.rb', line 43

def is?
  !maybe?
end

#maybe?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/jet/contract/attribute.rb', line 47

def maybe?
  type.maybe?
end

#optional?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/jet/contract/attribute.rb', line 51

def optional?
  !required?
end

#optsObject



55
56
57
# File 'lib/jet/contract/attribute.rb', line 55

def opts
  @opts.dup
end

#required?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/jet/contract/attribute.rb', line 59

def required?
  @opts[:required]
end

#to_builderObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/jet/contract/attribute.rb', line 63

def to_builder
  Builder.new(
    checks: @checks&.to_builder,
    contract: @opts[:contract]&.to_builder,
    each: @opts[:each]&.to_builder,
    is: is?,
    required: required?,
    type: type.name
  )
end

#to_symObject



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

def to_sym
  name
end