Class: Igniter::Contract
- Inherits:
-
Object
- Object
- Igniter::Contract
- Defined in:
- lib/igniter/contracts/contract.rb
Defined Under Namespace
Classes: ResultReader
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#execution_result ⇒ Object
readonly
Returns the value of attribute execution_result.
-
#inputs ⇒ Object
readonly
Returns the value of attribute inputs.
-
#profile ⇒ Object
readonly
Returns the value of attribute profile.
Class Method Summary collapse
- .call(inputs = nil, profile: self.profile, **keyword_inputs) ⇒ Object
- .compile(profile: self.profile) ⇒ Object
- .define(&block) ⇒ Object
- .definition_block ⇒ Object
- .execute(inputs = nil, profile: self.profile, **keyword_inputs) ⇒ Object
- .inherited(subclass) ⇒ Object
Instance Method Summary collapse
- #failure? ⇒ Boolean
-
#initialize(inputs = nil, profile: self.class.profile, **keyword_inputs) ⇒ Contract
constructor
A new instance of Contract.
- #output(name) ⇒ Object
- #outputs ⇒ Object
- #result ⇒ Object
- #success? ⇒ Boolean
- #to_h ⇒ Object
- #update_inputs(inputs = nil, **keyword_inputs) ⇒ Object
Constructor Details
#initialize(inputs = nil, profile: self.class.profile, **keyword_inputs) ⇒ Contract
Returns a new instance of Contract.
81 82 83 84 85 |
# File 'lib/igniter/contracts/contract.rb', line 81 def initialize(inputs = nil, profile: self.class.profile, **keyword_inputs) @profile = profile @inputs = normalize_inputs(inputs, keyword_inputs) run! end |
Class Attribute Details
.profile ⇒ Object
53 54 55 |
# File 'lib/igniter/contracts/contract.rb', line 53 def profile @profile || Contracts.default_profile end |
Instance Attribute Details
#execution_result ⇒ Object (readonly)
Returns the value of attribute execution_result.
79 80 81 |
# File 'lib/igniter/contracts/contract.rb', line 79 def execution_result @execution_result end |
#inputs ⇒ Object (readonly)
Returns the value of attribute inputs.
79 80 81 |
# File 'lib/igniter/contracts/contract.rb', line 79 def inputs @inputs end |
#profile ⇒ Object (readonly)
Returns the value of attribute profile.
79 80 81 |
# File 'lib/igniter/contracts/contract.rb', line 79 def profile @profile end |
Class Method Details
.call(inputs = nil, profile: self.profile, **keyword_inputs) ⇒ Object
63 64 65 |
# File 'lib/igniter/contracts/contract.rb', line 63 def call(inputs = nil, profile: self.profile, **keyword_inputs) new(inputs, profile: profile, **keyword_inputs) end |
.compile(profile: self.profile) ⇒ Object
57 58 59 60 61 |
# File 'lib/igniter/contracts/contract.rb', line 57 def compile(profile: self.profile) compiled_graphs.fetch(profile.fingerprint) do compiled_graphs[profile.fingerprint] = Contracts.compile(profile: profile, &definition_block) end end |
.define(&block) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/igniter/contracts/contract.rb', line 41 def define(&block) raise ArgumentError, "contract definition requires a block" unless block @definition_block = block @compiled_graphs = {} self end |
.definition_block ⇒ Object
49 50 51 |
# File 'lib/igniter/contracts/contract.rb', line 49 def definition_block @definition_block || raise(Contracts::Error, "#{name || self} does not define a contract") end |
.execute(inputs = nil, profile: self.profile, **keyword_inputs) ⇒ Object
67 68 69 70 |
# File 'lib/igniter/contracts/contract.rb', line 67 def execute(inputs = nil, profile: self.profile, **keyword_inputs) contract = new(inputs, profile: profile, **keyword_inputs) contract.execution_result end |
.inherited(subclass) ⇒ Object
36 37 38 39 |
# File 'lib/igniter/contracts/contract.rb', line 36 def inherited(subclass) super subclass.profile = profile end |
Instance Method Details
#failure? ⇒ Boolean
103 104 105 |
# File 'lib/igniter/contracts/contract.rb', line 103 def failure? !success? end |
#output(name) ⇒ Object
95 96 97 |
# File 'lib/igniter/contracts/contract.rb', line 95 def output(name) result.output(name) end |
#outputs ⇒ Object
91 92 93 |
# File 'lib/igniter/contracts/contract.rb', line 91 def outputs execution_result.outputs end |
#result ⇒ Object
87 88 89 |
# File 'lib/igniter/contracts/contract.rb', line 87 def result ResultReader.new(execution_result.outputs) end |
#success? ⇒ Boolean
99 100 101 |
# File 'lib/igniter/contracts/contract.rb', line 99 def success? true end |
#to_h ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/igniter/contracts/contract.rb', line 113 def to_h { contract: self.class.name, inputs: inputs.dup, outputs: outputs.to_h, success: success? } end |
#update_inputs(inputs = nil, **keyword_inputs) ⇒ Object
107 108 109 110 111 |
# File 'lib/igniter/contracts/contract.rb', line 107 def update_inputs(inputs = nil, **keyword_inputs) @inputs = @inputs.merge(normalize_inputs(inputs, keyword_inputs)) run! self end |