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



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

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



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

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)


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

def output_provided?(name)
  name = name.to_sym

  outputs.key?(name)
end

#setup_outputsObject



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

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)


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

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



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

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