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,
  :enable_when
].freeze
UNINHERITABLE_ATTRIBUTES =

These attributes require special handling when merging input definitions.

[
  # Locking, hiding, or conditional display only have meaning at the level
  # they are applied. Consider:
  # - ParentGroup
  #   - Group 1, input :a
  #   - Group 2, input :a, locked: true, hidden: true, enable_when: {...}, optional: true
  # The input 'a' should only be locked, hidden, or conditionally shown when
  # running Group 2 in isolation. It should not inherit those when running
  # Group 1 or the ParentGroup.
  :locked,
  :hidden,
  :enable_when,
  # 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.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/inferno/entities/input.rb', line 52

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

  assert_enable_when_shape!(params)

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

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

Instance Method Details

#==(other) ⇒ Object



209
210
211
212
213
# File 'lib/inferno/entities/input.rb', line 209

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

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

#assert_enable_when_shape!(params) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/inferno/entities/input.rb', line 74

def assert_enable_when_shape!(params)
  enable_when = params[:enable_when]
  return if enable_when.blank?
  return if enable_when_valid?(enable_when)

  raise Exceptions::InvalidAttributeException.new(
    :enable_when,
    self.class,
    'must be a Hash with a non-empty String :input_name and a String :value'
  )
end

#enable_when_valid?(enable_when) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
# File 'lib/inferno/entities/input.rb', line 86

def enable_when_valid?(enable_when)
  type_is_hash = enable_when.is_a?(Hash)
  input_name_string_exists = enable_when[:input_name].is_a?(String) && enable_when[:input_name].present?
  value_string_exists = enable_when.key?(:value) && enable_when[:value].is_a?(String)

  type_is_hash && input_name_string_exists && value_string_exists
end

#to_hashObject



200
201
202
203
204
205
206
207
# File 'lib/inferno/entities/input.rb', line 200

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

    hash[attribute] = value
  end
end