Class: CC::Engine::Configuration

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cc/engine/configuration.rb

Overview

The Code Climate configuration to pass to the engine

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ CC::Engine::Configuration

Instantiates a new engine configuration

Examples:

CC::Engine::Configuration.new({
  "checks" => {
    "AltText" => {
      "enabled" => false
    }
  },
  "config" => "my_haml_lint.yml",
  "enabled" => true,
  "exclude_paths" => [],
  "include_paths" => ["./"]
})

Parameters:

  • attributes (Hash) (defaults to: {})

    the attributes for the engine configuration


46
47
48
# File 'lib/cc/engine/configuration.rb', line 46

def initialize(attributes = {})
  @attributes = attributes
end

Class Method Details

.from_file(filename = '/config.json') ⇒ CC::Engine::Configuration

Loads a configuration based on a file

Examples:

CC::Engine::Configuration.from_file("/config.json")

Parameters:

  • filename (String) (defaults to: '/config.json')

    the absolute path to the configuration file

Returns:


20
21
22
23
24
25
26
# File 'lib/cc/engine/configuration.rb', line 20

def self.from_file(filename = '/config.json')
  if File.exist?(filename)
    new(JSON.parse(File.read(filename)))
  else
    new
  end
end

Instance Method Details

#checksHash

Configuration for the individual linters

Examples:

config = CC::Engine::Configuration.new({
  "checks" => {
    "AltText" => {
      "enabled" => false
    }
  },
  "config" => "my_haml_lint.yml",
  "enabled" => true,
  "exclude_paths" => [],
  "include_paths" => ["./"]
})
config.checks  #=> {"AltText" => {"enabled" => false}}

Returns:

  • (Hash)

68
69
70
# File 'lib/cc/engine/configuration.rb', line 68

def checks
  attribute_with_default('checks', {})
end

#configString, NilClass

The name of the HamlLint configuration file to use

Examples:

config = CC::Engine::Configuration.new({
  "checks" => {
    "AltText" => {
      "enabled" => false
    }
  },
  "config" => "my_haml_lint.yml",
  "enabled" => true,
  "exclude_paths" => [],
  "include_paths" => ["./"]
})
config.config  #=> "my_haml_lint.yml"

Returns:

  • (String, NilClass)

90
91
92
# File 'lib/cc/engine/configuration.rb', line 90

def config
  attribute_with_default('config', nil)
end

#enabled?Boolean

Checks whether the engine is configured or not

Examples:

config = CC::Engine::Configuration.new({
  "checks" => {
    "AltText" => {
      "enabled" => false
    }
  },
  "config" => "my_haml_lint.yml",
  "enabled" => true,
  "exclude_paths" => [],
  "include_paths" => ["./"]
})
config.enabled?  #=> true

Returns:

  • (Boolean)

112
113
114
# File 'lib/cc/engine/configuration.rb', line 112

def enabled?
  attribute_with_default('enabled', true)
end

#exclude_pathsArray<String>

The paths to exclude from analysis

Examples:

config = CC::Engine::Configuration.new({
  "checks" => {
    "AltText" => {
      "enabled" => false
    }
  },
  "config" => "my_haml_lint.yml",
  "enabled" => true,
  "exclude_paths" => [],
  "include_paths" => ["./"]
})
config.exclude_paths  #=> []

Returns:

  • (Array<String>)

134
135
136
# File 'lib/cc/engine/configuration.rb', line 134

def exclude_paths
  attribute_with_default('exclude_paths', [])
end

#include_pathsArray<String>

The paths to include in analysis

Examples:

config = CC::Engine::Configuration.new({
  "checks" => {
    "AltText" => {
      "enabled" => false
    }
  },
  "config" => "my_haml_lint.yml",
  "enabled" => true,
  "exclude_paths" => [],
  "include_paths" => ["./"]
})
config.include_paths  #=> ["./"]

Returns:

  • (Array<String>)

156
157
158
# File 'lib/cc/engine/configuration.rb', line 156

def include_paths
  attribute_with_default('include_paths', %w[./])
end

#to_linter_configHamlLint::Configuration

Converts the engine configuration to HamlLint configuration

Examples:

config = CC::Engine::Configuration.new({
  "checks" => {
    "AltText" => {
      "enabled" => false
    }
  },
  "config" => "my_haml_lint.yml",
  "enabled" => true,
  "exclude_paths" => [],
  "include_paths" => ["./"]
})
config.to_linter_config

Returns:

  • (HamlLint::Configuration)

178
179
180
181
182
183
184
185
186
# File 'lib/cc/engine/configuration.rb', line 178

def to_linter_config
  ::HamlLint::Configuration.new(
    { 'exclude' => exclude_paths, 'linters' => {} }.tap do |config|
      checks.each do |linter, attrs|
        config['linters'][linter.sub(%r{^HamlLint/}, '')] = attrs
      end
    end
  )
end