Class: Aspisec::Module::Location

Inherits:
Object
  • Object
show all
Defined in:
lib-ruby/aspisec/module.rb

Overview

Object easing the manipulation of locations.
Helpers to get the path, check if this feature/file/directory is enabled, etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tool_conf, feature_name) ⇒ Location

Returns a new instance of Location.

Parameters:

  • tool_conf (Hash)

    Tool configuration as returned by Aspisec::Module#conf.

  • feature_name (String)

    Name of the feature/file/directory to clean.
    Must be equal to the configuration key.



100
101
102
103
104
105
# File 'lib-ruby/aspisec/module.rb', line 100

def initialize(tool_conf, feature_name)
  @name = feature_name
  @path = Pathname.new(tool_conf.dig('location', @name, 'path'))
  @enabled = tool_conf.dig('location', @name).fetch('enabled', true)
  @description = tool_conf.dig('location', @name).fetch('description', '')
end

Instance Attribute Details

#descriptionString (readonly)

Explanation of what the location (file / directory) is containing, to give an idea of how sensitive it is.

Returns:

  • (String)

    description



95
96
97
# File 'lib-ruby/aspisec/module.rb', line 95

def description
  @description
end

#nameString (readonly)

Name of the feature, file or directory of the tool.

Returns:

  • (String)


87
88
89
# File 'lib-ruby/aspisec/module.rb', line 87

def name
  @name
end

#pathPathname (readonly)

File path of the file or directory location to clean.

Returns:

  • (Pathname)

    absolute path



91
92
93
# File 'lib-ruby/aspisec/module.rb', line 91

def path
  @path
end

Instance Method Details

#enabled?true|false

Is this location enabled?

Returns:

  • (true|false)


109
110
111
# File 'lib-ruby/aspisec/module.rb', line 109

def enabled?
  @enabled
end

#exist?true|false

Check if the location exist (weither it's a file, directory or a path contaning globbing so
multiple files / directories).
loc.path.exist? will return false when a path contains globbing as it's not expended,
that's the main reason for creating the loc.exist? helper.

Returns:

  • (true|false)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib-ruby/aspisec/module.rb', line 118

def exist?
  return true if path.exist?

  # this case is needed to support globbing
  candidates = Dir[path].map { |path| Pathname.new(path).exist? }
  # rubocop:disable Lint/DuplicateBranch
  # false positive in rubocop rule
  if candidates.empty? # necessary because [].all? always return true whatever the condition is
    # this is preventing doing a simple one-liner like
    # self.path.exist? || Dir[self.path].map { |path| Pathname.new(path).exist? }.all? { |bool| bool == true }
    false
  elsif candidates.all? { |bool| bool == true }
    true
  else
    false
  end
  # rubocop:enable Lint/DuplicateBranch
end