Class: Jet::Contract::Attribute::Builder
- Inherits:
-
Object
- Object
- Jet::Contract::Attribute::Builder
- Defined in:
- lib/jet/contract/attribute/builder.rb
Instance Method Summary collapse
- #call(types = nil, checks = nil) ⇒ Object
- #checks(checks) ⇒ Object
- #contract(contract = nil, &blk) ⇒ Object
- #each(*args, &blk) ⇒ Object
-
#initialize(opts = {}) ⇒ Builder
constructor
A new instance of Builder.
- #is(type, *checks) ⇒ Object
- #maybe(type, *checks) ⇒ Object
- #opts ⇒ Object
- #type(type, *checks) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Builder
Returns a new instance of Builder.
7 8 9 10 11 12 |
# File 'lib/jet/contract/attribute/builder.rb', line 7 def initialize(opts = {}) @opts = { required: true }.merge(opts) checks(opts[:checks]) if opts[:checks] contract(opts[:contract]) if opts[:contract] each(*opts[:each]) if opts[:each] end |
Instance Method Details
#call(types = nil, checks = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/jet/contract/attribute/builder.rb', line 14 def call(types = nil, checks = nil) types = types.nil? ? Contract.types : Contract.types!(types) checks = checks.nil? ? Contract.checks : Contract.checks!(checks) Attribute.new( type_with(types), check_set_with(checks), contract: @opts[:contract]&.(types, checks), each: @opts[:each]&.(types, checks), required: @opts[:required] ) end |
#checks(checks) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/jet/contract/attribute/builder.rb', line 27 def checks(checks) @opts[:checks] = checks.each_with_object([]) do |check, a| case check when Hash check.each { |(k, v)| a << [k.to_sym, v] } when Array a << [check.first.to_sym].concat(check[1..-1]) else a << [check.to_sym] end end self end |
#contract(contract = nil, &blk) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/jet/contract/attribute/builder.rb', line 41 def contract(contract = nil, &blk) raise ArgumentError, "cannot provide :contract if :each is set" if @opts[:each] auto_type!("contract", :hash) raise ArgumentError, "must provide either `contract` or a block" unless !contract.nil? ^ block_given? @opts[:contract] = if block_given? Contract::Builder.new.tap { |b| b.instance_eval(&blk) } else Jet.type_check!(":contract", contract, Contract, Contract::Builder) end self end |
#each(*args, &blk) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/jet/contract/attribute/builder.rb', line 56 def each(*args, &blk) raise ArgumentError, "cannot provide :each if :contract is set" if @opts[:contract] auto_type!("each", :array) raise ArgumentError, "must provide either `args` or a block" unless args.any? ^ block_given? @opts[:each] = if block_given? self.class.new.instance_eval(&blk) elsif args.size == 1 && args.first.is_a?(self.class) args.first else self.class.new.is(*args) end self end |
#is(type, *checks) ⇒ Object
73 74 75 76 77 |
# File 'lib/jet/contract/attribute/builder.rb', line 73 def is(type, *checks) @opts[:maybe] = false type(type, *checks) self end |
#maybe(type, *checks) ⇒ Object
79 80 81 82 83 |
# File 'lib/jet/contract/attribute/builder.rb', line 79 def maybe(type, *checks) @opts[:maybe] = true type(type, *checks) self end |
#opts ⇒ Object
91 92 93 |
# File 'lib/jet/contract/attribute/builder.rb', line 91 def opts @opts.dup end |
#type(type, *checks) ⇒ Object
85 86 87 88 89 |
# File 'lib/jet/contract/attribute/builder.rb', line 85 def type(type, *checks) @opts[:type] = Jet.type_check!("`type`", type, Symbol, Type).to_sym checks(checks) self end |