Class: Inspec::Rule

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers
Defined in:
lib/inspec/rule.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, _opts, &block) ⇒ Rule

Returns a new instance of Rule.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/inspec/rule.rb', line 51

def initialize(id, _opts, &block)
  @id = id
  @impact = nil
  @__block = block
  @__code = __get_block_source(&block)
  @title = nil
  @desc = nil
  # not changeable by the user:
  @profile_id = nil
  @checks = []
  # evaluate the given definition
  instance_eval(&block) if block_given?
end

Class Method Details

.full_id(profile_id, rule) ⇒ Object

Get the full id consisting of profile id + rule id for the rule. If the rule’s profile id is empty, the given profile_id will be used instead and also set for the rule.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/inspec/rule.rb', line 123

def self.full_id(profile_id, rule)
  if rule.is_a?(String) or rule.nil?
    rid = rule
  else
    # As the profile context is exclusively pulled with a
    # profile ID, attach it to the rule if necessary.
    rid = rule.instance_variable_get(:@id)
    if rid.nil?
      # TODO: Message about skipping this rule
      # due to missing ID
      return nil
    end
  end
  pid = rule.instance_variable_get(:@profile_id)
  if pid.nil?
    rule.instance_variable_set(:@profile_id, profile_id)
    pid = profile_id
  end

  # if we don't have a profile id, just return the rule's ID
  return rid if pid.nil? or pid.empty?
  # otherwise combine them
  "#{pid}/#{rid}"
end

.merge(dst, src) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/inspec/rule.rb', line 95

def self.merge(dst, src)
  if src.id != dst.id
    # TODO: register an error, this case should not happen
    return
  end
  sp = src.instance_variable_get(:@profile_id)
  dp = dst.instance_variable_get(:@profile_id)
  if sp != dp
    # TODO: register an error, this case should not happen
    return
  end
  # merge all fields
  dst.impact(src.impact) unless src.impact.nil?
  dst.title(src.title)   unless src.title.nil?
  dst.desc(src.desc)     unless src.desc.nil?
  # merge indirect fields
  # checks defined in the source will completely eliminate
  # all checks that were defined in the destination
  sc = src.instance_variable_get(:@checks)
  unless sc.nil? || sc.empty?
    dst.instance_variable_set(:@checks, sc)
  end
end

Instance Method Details

#desc(v = nil) ⇒ Object



80
81
82
83
# File 'lib/inspec/rule.rb', line 80

def desc(v = nil)
  @desc = unindent(v) unless v.nil?
  @desc
end

#describe(value, &block) ⇒ Object



85
86
87
# File 'lib/inspec/rule.rb', line 85

def describe(value, &block)
  @checks.push(['describe', [value], block])
end

#expect(value, &block) ⇒ Object



89
90
91
92
93
# File 'lib/inspec/rule.rb', line 89

def expect(value, &block)
  target = ExpectationTarget.new(value, &block)
  @checks.push(['expect', [value], target])
  target
end

#id(*_) ⇒ Object



65
66
67
68
# File 'lib/inspec/rule.rb', line 65

def id(*_)
  # never overwrite the ID
  @id
end

#impact(v = nil) ⇒ Object



70
71
72
73
# File 'lib/inspec/rule.rb', line 70

def impact(v = nil)
  @impact = v unless v.nil?
  @impact
end

#title(v = nil) ⇒ Object



75
76
77
78
# File 'lib/inspec/rule.rb', line 75

def title(v = nil)
  @title = v unless v.nil?
  @title
end