Class: Inspec::Attribute
- Inherits:
-
Object
- Object
- Inspec::Attribute
- Defined in:
- lib/inspec/objects/attribute.rb
Constant Summary collapse
- DEFAULT_ATTRIBUTE =
Class.new do def method_missing(*_) self end def respond_to_missing?(_, _) true end end
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#value ⇒ Object
implicit call is done by inspec to determine the value of an attribute.
Instance Method Summary collapse
- #default ⇒ Object
- #description ⇒ Object
-
#initialize(name, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #ruby_var_identifier ⇒ Object
- #title ⇒ Object
- #to_hash ⇒ Object
- #to_ruby ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
18 19 20 21 22 |
# File 'lib/inspec/objects/attribute.rb', line 18 def initialize(name, = {}) @name = name @opts = @value = nil end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/inspec/objects/attribute.rb', line 5 def name @name end |
#value ⇒ Object
implicit call is done by inspec to determine the value of an attribute
25 26 27 |
# File 'lib/inspec/objects/attribute.rb', line 25 def value @value.nil? ? default : @value end |
Instance Method Details
#default ⇒ Object
29 30 31 |
# File 'lib/inspec/objects/attribute.rb', line 29 def default @opts[:default] || DEFAULT_ATTRIBUTE.new end |
#description ⇒ Object
37 38 39 |
# File 'lib/inspec/objects/attribute.rb', line 37 def description @opts[:description] end |
#ruby_var_identifier ⇒ Object
41 42 43 |
# File 'lib/inspec/objects/attribute.rb', line 41 def ruby_var_identifier 'attr_' + @name.downcase.strip.gsub(/\s+/, '-').gsub(/[^\w-]/, '') end |
#title ⇒ Object
33 34 35 |
# File 'lib/inspec/objects/attribute.rb', line 33 def title @opts[:title] end |
#to_hash ⇒ Object
45 46 47 48 49 50 |
# File 'lib/inspec/objects/attribute.rb', line 45 def to_hash { name: @name, options: @opts, } end |
#to_ruby ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/inspec/objects/attribute.rb', line 52 def to_ruby res = ["#{ruby_var_identifier} = attribute('#{@name}',{"] res.push " title: '#{title}'," unless title.to_s.empty? res.push " default: '#{default}'," unless default.to_s.empty? res.push " description: '#{description}'," unless description.to_s.empty? res.push '})' res.join("\n") end |
#to_s ⇒ Object
61 62 63 |
# File 'lib/inspec/objects/attribute.rb', line 61 def to_s "Attribute #{@name} with #{@value}" end |