Class: Inferno::Entities::Input

Inherits:
Object
  • Object
show all
Includes:
Attributes
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,
  :value
].freeze
UNINHERITABLE_ATTRIBUTES =

These attributes require special handling when merging input definitions.

[
  # Locking an input only has meaning at the level it is locked.
  # Consider:
  # - ParentGroup
  #   - Group 1, input :a
  #   - Group 2, input :a, locked: true
  # The input 'a' should be only be locked when running Group 2 in
  # isolation. It should not be locked when running Group 1 or the
  # ParentGroup.
  :locked,
  # 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]).freeze

Instance Method Summary collapse

Methods included from Attributes

included

Constructor Details

#initialize(**params) ⇒ Input

Returns a new instance of Input.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/inferno/entities/input.rb', line 48

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

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

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

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

Instance Method Details

#==(other) ⇒ Object



115
116
117
118
119
# File 'lib/inferno/entities/input.rb', line 115

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

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

#merge(other_input) ⇒ Object

Merge this input with an input from a configuration. Fields defined in the configuration take precedence over those defined on this input.



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/inferno/entities/input.rb', line 78

def merge(other_input)
  return self if other_input.nil?

  MERGEABLE_ATTRIBUTES.each do |attribute|
    merge_attribute(attribute, primary_source: other_input, secondary_source: self)
  end

  self.type = other_input.type if other_input.type.present? && other_input.type != 'text'

  self
end

#merge_attribute(attribute, primary_source:, secondary_source:) ⇒ Object

Merge an individual attribute. If the primary source contains the attribute, that value will be used. Otherwise the value from the secondary source will be used.

Parameters:

  • attribute (Symbol)
  • primary_source (Input)
  • secondary_source (Input)


97
98
99
100
101
102
103
104
# File 'lib/inferno/entities/input.rb', line 97

def merge_attribute(attribute, primary_source:, secondary_source:)
  value = primary_source.send(attribute)
  value = secondary_source.send(attribute) if value.nil?

  return if value.nil?

  send("#{attribute}=", value)
end

#merge_with_child(child_input) ⇒ Object

Merge this input with an input belonging to a child. Fields defined on this input take precedence over those defined on the child input.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/inferno/entities/input.rb', line 63

def merge_with_child(child_input)
  return self if child_input.nil?

  INHERITABLE_ATTRIBUTES.each do |attribute|
    merge_attribute(attribute, primary_source: self, secondary_source: child_input)
  end

  self.type = child_input.type if child_input.present? && child_input.type != 'text'

  self
end

#to_hashObject



106
107
108
109
110
111
112
113
# File 'lib/inferno/entities/input.rb', line 106

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

    hash[attribute] = value
  end
end