Class: ActiveInteraction::Inputs
- Inherits:
-
Object
- Object
- ActiveInteraction::Inputs
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/active_interaction/inputs.rb
Overview
Holds inputs passed to the interaction.
Class Method Summary collapse
-
.reserved?(name) ⇒ Boolean
Checking ‘syscall` is the result of what appears to be a bug in Ruby.
Instance Method Summary collapse
-
#given?(input, *rest) ⇒ Boolean
Returns ‘true` if the given key was in the hash passed to run.
-
#initialize(raw_inputs, base) ⇒ Inputs
constructor
A new instance of Inputs.
- #normalized ⇒ Object
-
#to_h ⇒ Hash
Turn the inputs into a Hash.
Constructor Details
#initialize(raw_inputs, base) ⇒ Inputs
Returns a new instance of Inputs.
39 40 41 42 43 44 45 46 47 |
# File 'lib/active_interaction/inputs.rb', line 39 def initialize(raw_inputs, base) @base = base @normalized_inputs = normalize(raw_inputs) @inputs = base.class.filters.each_with_object({}) do |(name, filter), inputs| inputs[name] = filter.process(@normalized_inputs[name], base) yield name, inputs[name] if block_given? end end |
Class Method Details
.reserved?(name) ⇒ Boolean
Checking ‘syscall` is the result of what appears to be a bug in Ruby. bugs.ruby-lang.org/issues/15597
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/active_interaction/inputs.rb', line 24 def reserved?(name) name.to_s.start_with?('_interaction_') || name == :syscall || ( Base.method_defined?(name) && !Object.method_defined?(name) ) || ( Base.private_method_defined?(name) && !Object.private_method_defined?(name) ) end |
Instance Method Details
#given?(input, *rest) ⇒ Boolean
Returns ‘true` if the given key was in the hash passed to run. Otherwise returns `false`. Use this to figure out if an input was given, even if it was `nil`. Keys within nested hash filter can also be checked by passing them in series. Arrays can be checked in the same manor as hashes by passing an index.
rubocop:disable all
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/active_interaction/inputs.rb', line 134 def given?(input, *rest) filter_level = @base.class input_level = @normalized_inputs [input, *rest].each do |key_or_index| if key_or_index.is_a?(Symbol) || key_or_index.is_a?(String) key = key_or_index.to_sym key_to_s = key_or_index.to_s filter_level = filter_level.filters[key] break false if filter_level.nil? || input_level.nil? break false unless input_level.key?(key) || input_level.key?(key_to_s) input_level = input_level[key] || input_level[key_to_s] else index = key_or_index filter_level = filter_level.filters.first.last break false if filter_level.nil? || input_level.nil? break false unless index.between?(-input_level.size, input_level.size - 1) input_level = input_level[index] end end && true end |
#normalized ⇒ Object
50 51 52 |
# File 'lib/active_interaction/inputs.rb', line 50 def normalized @normalized_inputs end |
#to_h ⇒ Hash
Turn the inputs into a Hash.
57 58 59 |
# File 'lib/active_interaction/inputs.rb', line 57 def to_h @to_h ||= @inputs.transform_values(&:value).freeze end |