Class: Inspec::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/errors.rb,
lib/inspec/objects/input.rb,
lib/inspec/objects/input.rb

Defined Under Namespace

Classes: Error, NO_VALUE_SET, RequiredError, TypeError, ValidationError

Constant Summary collapse

VALID_TYPES =
%w{
  String
  Numeric
  Regexp
  Array
  Hash
  Boolean
  Any
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Input

Returns a new instance of Input.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/inspec/objects/input.rb', line 77

def initialize(name, options = {})
  @name = name
  @opts = options
  if @opts.key?(:default)
    Inspec.deprecate(:attrs_value_replaces_default, "input name: '#{name}'")
    if @opts.key?(:value)
      Inspec::Log.warn "Input #{@name} created using both :default and :value options - ignoring :default"
      @opts.delete(:default)
    else
      @opts[:value] = @opts.delete(:default)
    end
  end
  @value = @opts[:value]
  validate_value_type(@value) if @opts.key?(:type) && @opts.key?(:value)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



65
66
67
# File 'lib/inspec/objects/input.rb', line 65

def name
  @name
end

Instance Method Details

#descriptionObject



111
112
113
# File 'lib/inspec/objects/input.rb', line 111

def description
  @opts[:description]
end

#ruby_var_identifierObject



115
116
117
# File 'lib/inspec/objects/input.rb', line 115

def ruby_var_identifier
  @opts[:identifier] || 'attr_' + @name.downcase.strip.gsub(/\s+/, '-').gsub(/[^\w-]/, '')
end

#titleObject



107
108
109
# File 'lib/inspec/objects/input.rb', line 107

def title
  @opts[:title]
end

#to_hashObject



119
120
121
122
123
124
# File 'lib/inspec/objects/input.rb', line 119

def to_hash
  {
    name: @name,
    options: @opts,
  }
end

#to_rubyObject



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/inspec/objects/input.rb', line 126

def to_ruby
  res = ["#{ruby_var_identifier} = attribute('#{@name}',{"]
  res.push "  title: '#{title}'," unless title.to_s.empty?
  res.push "  value: #{value.inspect}," unless value.to_s.empty?
  # to_ruby may generate code that is to be used by older versions of inspec.
  # Anything older than 3.4 will not recognize the value: option, so
  # send the default: option as well. See #3759
  res.push "  default: #{value.inspect}," unless value.to_s.empty?
  res.push "  description: '#{description}'," unless description.to_s.empty?
  res.push '})'
  res.join("\n")
end

#to_sObject



139
140
141
# File 'lib/inspec/objects/input.rb', line 139

def to_s
  "Input #{@name} with #{@value}"
end

#valueObject



98
99
100
101
102
103
104
105
# File 'lib/inspec/objects/input.rb', line 98

def value
  if @value.nil?
    validate_required(@value) if @opts[:required] == true
    @value = value_or_dummy
  else
    @value
  end
end

#value=(new_value) ⇒ Object



93
94
95
96
# File 'lib/inspec/objects/input.rb', line 93

def value=(new_value)
  validate_value_type(new_value) if @opts.key?(:type)
  @value = new_value
end