Class: ActiveInteraction::Inputs

Inherits:
Hash
  • Object
show all
Defined in:
lib/active_interaction/inputs.rb

Overview

Holds inputs passed to the interaction.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInputs

Returns a new instance of Inputs.



53
54
55
56
57
58
# File 'lib/active_interaction/inputs.rb', line 53

def initialize
  @groups = {}
  @groups.default_proc = ->(hash, key) { hash[key] = [] }

  super(@inputs = {})
end

Class Method Details

.process(inputs) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_interaction/inputs.rb', line 31

def process(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) ⇒ Boolean

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

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_interaction/inputs.rb', line 18

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

#group(name) ⇒ Hash

Returns inputs from the group name given.

Examples:

inputs.group(:a)
# => {key: :value}

Parameters:

  • name (Object)

    Name of the group to return.

Returns:

  • (Hash)

    Inputs from the group name given.



91
92
93
# File 'lib/active_interaction/inputs.rb', line 91

def group(name)
  @inputs.select { |k, _| @groups[name].include?(k) }
end

#store(key, value, groups = []) ⇒ Object

Associates the ‘value` with the `key`. Allows the `key`/`value` pair to

be associated with one or more groups.

Examples:

inputs.store(:key, :value)
# => :value
inputs.store(:key, :value, %i[a b])
# => :value

Parameters:

  • key (Object)

    The key to store the value under.

  • value (Object)

    The value to store.

  • groups (Array<Object>) (defaults to: [])

    The groups to store the pair under.

Returns:

  • (Object)

    value



74
75
76
77
78
79
80
# File 'lib/active_interaction/inputs.rb', line 74

def store(key, value, groups = [])
  groups.each do |group|
    @groups[group] << key
  end

  super(key, value)
end