Class: Igniter::Contract

Inherits:
Object
  • Object
show all
Defined in:
lib/igniter/contracts/contract.rb

Defined Under Namespace

Classes: ResultReader

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

.profileObject



53
54
55
# File 'lib/igniter/contracts/contract.rb', line 53

def profile
  @profile || Contracts.default_profile
end

Instance Attribute Details

#execution_resultObject (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

#inputsObject (readonly)

Returns the value of attribute inputs.



79
80
81
# File 'lib/igniter/contracts/contract.rb', line 79

def inputs
  @inputs
end

#profileObject (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

Raises:

  • (ArgumentError)


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_blockObject



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

Returns:

  • (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

#outputsObject



91
92
93
# File 'lib/igniter/contracts/contract.rb', line 91

def outputs
  execution_result.outputs
end

#resultObject



87
88
89
# File 'lib/igniter/contracts/contract.rb', line 87

def result
  ResultReader.new(execution_result.outputs)
end

#success?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/igniter/contracts/contract.rb', line 99

def success?
  true
end

#to_hObject



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