Class: Inspec::Attribute

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, 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

#valueObject

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

#defaultObject



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

def default
  @opts.key?(:default) ? @opts[:default] : DEFAULT_ATTRIBUTE.new
end

#descriptionObject



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

def description
  @opts[:description]
end

#ruby_var_identifierObject



41
42
43
# File 'lib/inspec/objects/attribute.rb', line 41

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

#titleObject



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

def title
  @opts[:title]
end

#to_hashObject



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

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

#to_rubyObject



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.inspect}," unless default.to_s.empty?
  res.push "  description: '#{description}'," unless description.to_s.empty?
  res.push '})'
  res.join("\n")
end

#to_sObject



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

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