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, OutputNotSetError, UnknownOutputError

Instance Method Summary collapse

Instance Method Details

#get_output(name) ⇒ Object



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

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

  outputs[name]
end

#output(name, value) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/subroutine/outputs.rb', line 43

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

  outputs[name] = value
end

#output_provided?(name) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
# File 'lib/subroutine/outputs.rb', line 75

def output_provided?(name)
  name = name.to_sym

  outputs.key?(name)
end

#setup_outputsObject



39
40
41
# File 'lib/subroutine/outputs.rb', line 39

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)


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/subroutine/outputs.rb', line 81

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/subroutine/outputs.rb', line 59

def validate_outputs!
  output_configurations.each_pair do |name, config|
    if config.required? && !output_provided?(name)
      raise ::Subroutine::Outputs::OutputNotSetError, name
    end
    unless 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
  end
end