class ActiveInteraction::Inputs

Holds inputs passed to the interaction.

Constants

GROUPED_INPUT_PATTERN

matches inputs like “key(1i)”

Public Class Methods

keys_for_group?(keys, group_key) click to toggle source

@private

# File lib/active_interaction/inputs.rb, line 17
def keys_for_group?(keys, group_key)
  search_key = /\A#{group_key}\(\d+i\)\z/
  keys.any? { |key| search_key.match?(key) }
end
new(inputs = {}) click to toggle source
Calls superclass method
# File lib/active_interaction/inputs.rb, line 76
def initialize(inputs = {})
  super(inputs)
end
process(inputs) click to toggle source

@param inputs [Hash, ActionController::Parameters, ActiveInteraction::Inputs] Attribute values to set.

@private

# File lib/active_interaction/inputs.rb, line 41
def process(inputs)
  normalize_inputs!(inputs)
    .stringify_keys
    .sort
    .each_with_object({}) do |(k, v), h|
      next if reserved?(k)

      if (group = GROUPED_INPUT_PATTERN.match(k))
        assign_to_grouped_input!(h, group[:key], group[:index], v)
      else
        h[k.to_sym] = v
      end
    end
end
reserved?(name) click to toggle source

Checking `syscall` is the result of what appears to be a bug in Ruby. bugs.ruby-lang.org/issues/15597 @private

# File lib/active_interaction/inputs.rb, line 25
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

Private Class Methods

assign_to_grouped_input!(inputs, key, index, value) click to toggle source
# File lib/active_interaction/inputs.rb, line 68
def assign_to_grouped_input!(inputs, key, index, value)
  key = key.to_sym

  inputs[key] = GroupedInput.new unless inputs[key].is_a?(GroupedInput)
  inputs[key][index] = value
end
normalize_inputs!(inputs) click to toggle source
# File lib/active_interaction/inputs.rb, line 58
def normalize_inputs!(inputs)
  return inputs if inputs.is_a?(Hash) || inputs.is_a?(Inputs)

  parameters = 'ActionController::Parameters'
  klass = parameters.safe_constantize
  return inputs.to_unsafe_h if klass && inputs.is_a?(klass)

  raise ArgumentError, "inputs must be a hash or #{parameters}"
end