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 default  "\
      "value with `attribute('#{@name}', default: 'somedefault', ...)`.",
    ) 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.



42
43
44
45
46
47
# File 'lib/inspec/objects/attribute.rb', line 42

def initialize(name, options = {})
  @name = name
  @opts = options
  validate_value_type(default) if @opts.key?(:type) && @opts.key?(:default)
  @value = nil
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/inspec/objects/attribute.rb', line 5

def name
  @name
end

Instance Method Details

#descriptionObject



67
68
69
# File 'lib/inspec/objects/attribute.rb', line 67

def description
  @opts[:description]
end

#ruby_var_identifierObject



71
72
73
# File 'lib/inspec/objects/attribute.rb', line 71

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

#titleObject



63
64
65
# File 'lib/inspec/objects/attribute.rb', line 63

def title
  @opts[:title]
end

#to_hashObject



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

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

#to_rubyObject



82
83
84
85
86
87
88
89
# File 'lib/inspec/objects/attribute.rb', line 82

def to_ruby
  res = ["#{ruby_var_identifier} = attribute('#{@name}',{"]
  res.push "  title: '#{title}'," unless title.to_s.empty?
  res.push "  default: #{default.inspect}," unless default.to_s.empty?
  res.push "  description: '#{description}'," unless description.to_s.empty?
  res.push '})'
  res.join("\n")
end

#to_sObject



91
92
93
# File 'lib/inspec/objects/attribute.rb', line 91

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

#valueObject



54
55
56
57
58
59
60
61
# File 'lib/inspec/objects/attribute.rb', line 54

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

#value=(new_value) ⇒ Object



49
50
51
52
# File 'lib/inspec/objects/attribute.rb', line 49

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