Class: Chef::Compliance::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/compliance/input.rb

Overview

Chef object that represents a single input file in the compliance segment of a cookbook.

Constant Summary collapse

HIDDEN_IVARS =
[ :@events ].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(events, data, path, cookbook_name) ⇒ Input

Returns a new instance of Input.


48
49
50
51
52
53
54
55
# File 'lib/chef/compliance/input.rb', line 48

def initialize(events, data, path, cookbook_name)
  @events = events
  @data = data
  @cookbook_name = cookbook_name
  @path = path
  @pathname = File.basename(path, File.extname(path)) unless path.nil?
  disable!
end

Instance Attribute Details

#cookbook_nameString (readonly)

Returns The name of the cookbook that the input is in.

Returns:

  • (String)

    The name of the cookbook that the input is in


31
32
33
# File 'lib/chef/compliance/input.rb', line 31

def cookbook_name
  @cookbook_name
end

#dataObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


46
47
48
# File 'lib/chef/compliance/input.rb', line 46

def data
  @data
end

#enabledBoolean (readonly)

Returns if the input has been enabled.

Returns:

  • (Boolean)

    if the input has been enabled


28
29
30
# File 'lib/chef/compliance/input.rb', line 28

def enabled
  @enabled
end

#eventsChef::EventDispatch::Dispatcher (readonly)

Event dispatcher for this run.


43
44
45
# File 'lib/chef/compliance/input.rb', line 43

def events
  @events
end

#pathString (readonly)

Returns The full path on the host to the input yml file.

Returns:

  • (String)

    The full path on the host to the input yml file


34
35
36
# File 'lib/chef/compliance/input.rb', line 34

def path
  @path
end

#pathnameString (readonly)

Returns the pathname in the cookbook.

Returns:

  • (String)

    the pathname in the cookbook


37
38
39
# File 'lib/chef/compliance/input.rb', line 37

def pathname
  @pathname
end

Class Method Details

.from_file(events, filename, cookbook_name = nil) ⇒ Object

Parameters:

  • filename (String)

    full path to the yml file in the cookbook

  • cookbook_name (String) (defaults to: nil)

    cookbook that the input is in


110
111
112
# File 'lib/chef/compliance/input.rb', line 110

def self.from_file(events, filename, cookbook_name = nil)
  from_yaml(events, IO.read(filename), filename, cookbook_name)
end

.from_hash(events, hash, path = nil, cookbook_name = nil) ⇒ Object

Helper to construct a input object from a hash. Since the path and cookbook_name are required this is probably not externally useful.


96
97
98
# File 'lib/chef/compliance/input.rb', line 96

def self.from_hash(events, hash, path = nil, cookbook_name = nil)
  new(events, hash, path, cookbook_name)
end

.from_yaml(events, string, path = nil, cookbook_name = nil) ⇒ Object

Helper to construct a input object from a yaml string. Since the path and cookbook_name are required this is probably not externally useful.


103
104
105
# File 'lib/chef/compliance/input.rb', line 103

def self.from_yaml(events, string, path = nil, cookbook_name = nil)
  from_hash(events, YAML.safe_load(string, permitted_classes: [Date]), path, cookbook_name)
end

Instance Method Details

#disable!Object

Set the input as being disabled


72
73
74
# File 'lib/chef/compliance/input.rb', line 72

def disable!
  @enabled = false
end

#enable!Object

Set the input to being enabled


65
66
67
68
# File 'lib/chef/compliance/input.rb', line 65

def enable!
  events.compliance_input_enabled(self)
  @enabled = true
end

#enabled?Boolean

Returns if the input has been enabled.

Returns:

  • (Boolean)

    if the input has been enabled


59
60
61
# File 'lib/chef/compliance/input.rb', line 59

def enabled?
  !!@enabled
end

#inspec_dataObject

Render the input in a way that it can be consumed by inspec


78
79
80
# File 'lib/chef/compliance/input.rb', line 78

def inspec_data
  data
end

#inspectObject

Omit the event object from error output


86
87
88
89
90
91
# File 'lib/chef/compliance/input.rb', line 86

def inspect
  ivar_string = (instance_variables.map(&:to_sym) - HIDDEN_IVARS).map do |ivar|
    "#{ivar}=#{instance_variable_get(ivar).inspect}"
  end.join(", ")
  "#<#{self.class}:#{object_id} #{ivar_string}>"
end