Class: Inspec::Attribute

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Attribute

Returns a new instance of Attribute.



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

def initialize(name, options)
  @name = name
  @opts = options
  @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

#defaultObject



20
21
22
# File 'lib/inspec/objects/attribute.rb', line 20

def default
  @opts[:default]
end

#descriptionObject



28
29
30
# File 'lib/inspec/objects/attribute.rb', line 28

def description
  @opts[:description]
end

#ruby_var_identifierObject



32
33
34
# File 'lib/inspec/objects/attribute.rb', line 32

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

#titleObject



24
25
26
# File 'lib/inspec/objects/attribute.rb', line 24

def title
  @opts[:title]
end

#to_hashObject



36
37
38
39
40
41
# File 'lib/inspec/objects/attribute.rb', line 36

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

#to_rubyObject



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

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_sObject



52
53
54
# File 'lib/inspec/objects/attribute.rb', line 52

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

#value(newvalue = nil) ⇒ Object

implicit call is done by inspec to determine the value of an attribute



13
14
15
16
17
18
# File 'lib/inspec/objects/attribute.rb', line 13

def value(newvalue = nil)
  unless newvalue.nil?
    @value = newvalue
  end
  @value || default
end