Module: Subroutine::Outputs

Extended by:
ActiveSupport::Concern
Included in:
Op
Defined in:
lib/subroutine/outputs.rb,
lib/subroutine/outputs/configuration.rb,
lib/subroutine/outputs/output_not_set_error.rb,
lib/subroutine/outputs/unknown_output_error.rb,
lib/subroutine/outputs/invalid_output_type_error.rb

Defined Under Namespace

Modules: ClassMethods Classes: Configuration, InvalidOutputTypeError, LazyExecutor, OutputNotSetError, UnknownOutputError

Instance Method Summary collapse

Instance Method Details

#ensure_output_type_valid!(name) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/subroutine/outputs.rb', line 107

def ensure_output_type_valid!(name)
  return if valid_output_type?(name)

  name = name.to_sym
  raise ::Subroutine::Outputs::InvalidOutputTypeError.new(
    name: name,
    actual_type: @outputs[name].class,
    expected_type: output_configurations[name][:type]
  )
end

#get_output(name) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/subroutine/outputs.rb', line 78

def get_output(name)
  name = name.to_sym
  raise ::Subroutine::Outputs::UnknownOutputError, name unless output_configurations.key?(name)

  output = @outputs[name]
  unless output.is_a?(LazyExecutor)
    output
  else
    # if its not executed, validate the type
    unless output.executed?
      @outputs[name] = output.value
      ensure_output_type_valid!(name)
    end

    @outputs[name]
  end
end

#output(name, value) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/subroutine/outputs.rb', line 69

def output(name, value)
  name = name.to_sym
  unless output_configurations.key?(name)
    raise ::Subroutine::Outputs::UnknownOutputError, name
  end

  @outputs[name] = output_configurations[name].lazy? ? LazyExecutor.new(value) : value
end

#output_provided?(name) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
# File 'lib/subroutine/outputs.rb', line 118

def output_provided?(name)
  name = name.to_sym

  @outputs.key?(name)
end

#outputsObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/subroutine/outputs.rb', line 58

def outputs
  unless @outputs_executed
    @outputs.each_pair do |key, value|
      @outputs[key] = value.is_a?(LazyExecutor) ? value.value : value
    end
    @outputs_executed = true
  end

  @outputs
end

#setup_outputsObject



54
55
56
# File 'lib/subroutine/outputs.rb', line 54

def setup_outputs
  @outputs = {} # don't do with_indifferent_access because it will turn provided objects into with_indifferent_access objects, which may not be the desired behavior
end

#valid_output_type?(name) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
128
129
130
131
132
133
134
# File 'lib/subroutine/outputs.rb', line 124

def valid_output_type?(name)
  name = name.to_sym

  return true unless output_configurations.key?(name)

  output_configuration = output_configurations[name]
  return true unless output_configuration[:type]
  return true if !output_configuration.required? && @outputs[name].nil?

  @outputs[name].is_a?(output_configuration[:type])
end

#validate_outputs!Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/subroutine/outputs.rb', line 96

def validate_outputs!
  output_configurations.each_pair do |name, config|
    if config.required? && !output_provided?(name)
      raise ::Subroutine::Outputs::OutputNotSetError, name
    end
    unless output_configurations[name].lazy?
      ensure_output_type_valid!(name)
    end
  end
end