Class: Inspec::Attribute
- Inherits:
-
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
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
#name ⇒ Object
Returns the value of attribute name.
7
8
9
|
# File 'lib/inspec/objects/attribute.rb', line 7
def name
@name
end
|
Instance Method Details
#description ⇒ Object
78
79
80
|
# File 'lib/inspec/objects/attribute.rb', line 78
def description
@opts[:description]
end
|
#ruby_var_identifier ⇒ Object
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
|
#title ⇒ Object
74
75
76
|
# File 'lib/inspec/objects/attribute.rb', line 74
def title
@opts[:title]
end
|
#to_hash ⇒ Object
86
87
88
89
90
91
|
# File 'lib/inspec/objects/attribute.rb', line 86
def to_hash
{
name: @name,
options: @opts,
}
end
|
#to_ruby ⇒ Object
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?
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_s ⇒ Object
106
107
108
|
# File 'lib/inspec/objects/attribute.rb', line 106
def to_s
"Attribute #{@name} with #{@value}"
end
|
#value ⇒ Object
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
|