Class: Inferno::Entities::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/inferno/entities/input.rb

Overview

This class represents an Input for a runnable.

Constant Summary collapse

ATTRIBUTES =
[
  :name,
  :title,
  :description,
  :type,
  :default,
  :optional,
  :options,
  :locked,
  :hidden,
  :value
].freeze
UNINHERITABLE_ATTRIBUTES =

These attributes require special handling when merging input definitions.

[
  # Locking or hiding an input only has meaning at the level it is applied.
  # Consider:
  # - ParentGroup
  #   - Group 1, input :a
  #   - Group 2, input :a, locked: true, hidden: true, optional: true
  # The input 'a' should only be locked or hidden when running Group 2 in isolation.
  # It should not be locked or hidden when running Group 1 or the ParentGroup.
  :locked,
  :hidden,
  # Input type is sometimes only a UI concern (e.g. text vs. textarea), so
  # it is common to not redeclare the type everywhere it's used and needs
  # special handling to avoid clobbering the type with the default (text)
  # type.
  :type
].freeze
INHERITABLE_ATTRIBUTES =

These are the attributes that can be directly copied when merging a runnable’s input with the input of one of its children.

(ATTRIBUTES - UNINHERITABLE_ATTRIBUTES).freeze
MERGEABLE_ATTRIBUTES =

These are the attributes that can be directly copied when merging a runnable’s input with an input configuration.

(ATTRIBUTES - [:type, :options]).freeze

Instance Method Summary collapse

Constructor Details

#initialize(**params) ⇒ Input

Returns a new instance of Input.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/inferno/entities/input.rb', line 49

def initialize(**params)
  bad_params = params.keys - ATTRIBUTES

  raise Exceptions::UnknownAttributeException.new(bad_params, self.class) if bad_params.present?

  if params[:hidden] && !params[:optional] && !params[:locked]
    raise Exceptions::InvalidAttributeException.new(
      :hidden,
      self.class,
      "Input '#{params[:name]}' cannot be hidden unless it is optional or locked."
    )
  end

  params
    .compact
    .each { |key, value| send("#{key}=", value) }

  self.name = name.to_s if params[:name].present?
end

Instance Method Details

#==(other) ⇒ Object



184
185
186
187
188
# File 'lib/inferno/entities/input.rb', line 184

def ==(other)
  return false unless other.is_a? Input

  ATTRIBUTES.all? { |attribute| send(attribute) == other.send(attribute) }
end

#to_hashObject



175
176
177
178
179
180
181
182
# File 'lib/inferno/entities/input.rb', line 175

def to_hash
  ATTRIBUTES.each_with_object({}) do |attribute, hash|
    value = send(attribute)
    next if value.nil?

    hash[attribute] = value
  end
end