Sha256: 178aeb751ca9336baf86f15d82753ffd206ae8460ec61c8b9288290034e4fa8b

Contents?: true

Size: 1.7 KB

Versions: 41

Compression:

Stored size: 1.7 KB

Contents

# This file is deprecated and will be removed in the next major release of InSpec.
# The Inspec::Input class will remain but these methods will be removed.
# Use the Inspec::Object::Input class from the inspec-objects rubygem instead.

require "inspec/input"

module Inspec

  # NOTE: due to namespacing, this reopens and extends the existing
  # Inspec::Input. This should be under Inspec::Objects but that ship
  # has sailed.

  class Input

    # NOTE: No initialize method or accessors for the reasons listed above

    #--------------------------------------------------------------------------#
    #                               Marshalling
    #--------------------------------------------------------------------------#

    def to_hash
      as_hash = { name: name, options: {} }
      %i{description title identifier type required value}.each do |field|
        val = send(field)
        next if val.nil?

        as_hash[:options][field] = val
      end
      as_hash
    end

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

    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
  end
end

Version data entries

41 entries across 41 versions & 2 rubygems

Version Path
inspec-core-4.22.0 lib/inspec/objects/input.rb
inspec-core-4.21.3 lib/inspec/objects/input.rb
inspec-core-4.21.1 lib/inspec/objects/input.rb
inspec-core-4.20.10 lib/inspec/objects/input.rb
inspec-core-4.20.6 lib/inspec/objects/input.rb
inspec-core-4.20.2 lib/inspec/objects/input.rb
inspec-core-4.19.2 lib/inspec/objects/input.rb
inspec-core-4.19.0 lib/inspec/objects/input.rb
inspec-core-4.18.114 lib/inspec/objects/input.rb
inspec-core-4.18.111 lib/inspec/objects/input.rb
inspec-core-4.18.108 lib/inspec/objects/input.rb
inspec-core-4.18.104 lib/inspec/objects/input.rb
inspec-core-4.18.100 lib/inspec/objects/input.rb
inspec-core-4.18.97 lib/inspec/objects/input.rb
inspec-core-4.18.85 lib/inspec/objects/input.rb
inspec-core-4.18.51 lib/inspec/objects/input.rb
inspec-4.18.51 lib/inspec/objects/input.rb
inspec-core-4.18.39 lib/inspec/objects/input.rb
inspec-4.18.39 lib/inspec/objects/input.rb
inspec-core-4.18.38 lib/inspec/objects/input.rb