Class: Inspec::Attribute

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

Defined Under Namespace

Classes: Error, RequiredError, TypeError, ValidationError

Constant Summary collapse

VALID_TYPES =
%w{
  String
  Numeric
  Regexp
  Array
  Hash
  Boolean
  Any
}.freeze
DEFAULT_ATTRIBUTE =
Class.new do
  def initialize(name)
    @name = name

    # output warn message if we are in a exec call
    Inspec::Log.warn(
      "Attribute '#{@name}' does not have a value. "\
      "Use --attrs to provide a value for '#{@name}' or specify a  "\
      "value with `attribute('#{@name}', value: 'somevalue', ...)`.",
    ) if Inspec::BaseCLI.inspec_cli_command == :exec
  end

  def method_missing(*_)
    self
  end

  def respond_to_missing?(_, _)
    true
  end

  def to_s
    "Attribute '#{@name}' does not have a value. Skipping test."
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Attribute.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/inspec/objects/attribute.rb', line 44

def initialize(name, options = {})
  @name = name
  @opts = options
  if @opts.key?(:default)
    Inspec.deprecate(:attrs_value_replaces_default, "attribute name: '#{name}'")
    if @opts.key?(:value)
      Inspec::Log.warn "Attribute #{@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.



7
8
9
# File 'lib/inspec/objects/attribute.rb', line 7

def name
  @name
end

Instance Method Details

#descriptionObject



78
79
80
# File 'lib/inspec/objects/attribute.rb', line 78

def description
  @opts[:description]
end

#ruby_var_identifierObject



82
83
84
# File 'lib/inspec/objects/attribute.rb', line 82

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

#titleObject



74
75
76
# File 'lib/inspec/objects/attribute.rb', line 74

def title
  @opts[:title]
end

#to_hashObject



86
87
88
89
90
91
# File 'lib/inspec/objects/attribute.rb', line 86

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

#to_rubyObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/inspec/objects/attribute.rb', line 93

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



106
107
108
# File 'lib/inspec/objects/attribute.rb', line 106

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

#valueObject



65
66
67
68
69
70
71
72
# File 'lib/inspec/objects/attribute.rb', line 65

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

#value=(new_value) ⇒ Object



60
61
62
63
# File 'lib/inspec/objects/attribute.rb', line 60

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