Class: Jet::Contract::Attribute
- Inherits:
-
Object
- Object
- Jet::Contract::Attribute
- Defined in:
- lib/jet/contract/attribute.rb,
lib/jet/contract/attribute/builder.rb
Defined Under Namespace
Classes: Builder
Instance Attribute Summary collapse
-
#checks ⇒ Object
readonly
Returns the value of attribute checks.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #call(input, at = []) ⇒ Object
- #check(output) ⇒ Object
- #coerce(input) ⇒ Object
-
#initialize(type, checks = nil, contract: nil, each: nil, required: true) ⇒ Attribute
constructor
A new instance of Attribute.
- #is? ⇒ Boolean
- #maybe? ⇒ Boolean
- #optional? ⇒ Boolean
- #opts ⇒ Object
- #required? ⇒ Boolean
- #to_builder ⇒ Object
- #to_sym ⇒ Object
Constructor Details
#initialize(type, checks = nil, contract: nil, each: nil, required: true) ⇒ Attribute
Returns a new instance of Attribute.
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
#checks ⇒ Object (readonly)
Returns the value of attribute checks.
9 10 11 |
# File 'lib/jet/contract/attribute.rb', line 9 def checks @checks end |
#type ⇒ Object (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
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
43 44 45 |
# File 'lib/jet/contract/attribute.rb', line 43 def is? !maybe? end |
#maybe? ⇒ Boolean
47 48 49 |
# File 'lib/jet/contract/attribute.rb', line 47 def maybe? type.maybe? end |
#optional? ⇒ Boolean
51 52 53 |
# File 'lib/jet/contract/attribute.rb', line 51 def optional? !required? end |
#opts ⇒ Object
55 56 57 |
# File 'lib/jet/contract/attribute.rb', line 55 def opts @opts.dup end |
#required? ⇒ Boolean
59 60 61 |
# File 'lib/jet/contract/attribute.rb', line 59 def required? @opts[:required] end |
#to_builder ⇒ Object
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_sym ⇒ Object
74 75 76 |
# File 'lib/jet/contract/attribute.rb', line 74 def to_sym name end |