Class: Maximus::Config

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/maximus/config.rb

Overview

Global options and configuration

Since:

  • 0.1.3

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#check_default_config_path, #edit_yaml, #file_count, #file_list, #is_rails?, #lines_added_to_range, #node_module_exists, #path_exists?, #prompt, #reporter_path, #root_dir, #truthy?

Constructor Details

#initialize(opts = {}) ⇒ #load_config_file #group_families #evaluate_settings

Global options for all of maximus

Parameters:

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

    options passed directly to config

Options Hash (opts):

  • :is_dev (Boolean) — default: false

    whether or not the class was initialized from the command line

  • :log (String, Boolean, nil) — default: 'log/maximus_git.log'

    path to log file If not set, logger outputs to STDOUT

  • :git_log (String, Boolean) — default: false

    path to log file or don’t log The git gem is very noisey

  • :root_dir (String)

    base directory

  • :domain (String) — default: 'http://localhost:3000'

    the host - used for Statistics

  • :port (String, Integer) — default: ''

    port number - used for Statistics and appended to domain. If blank (false, empty string, etc.), will not append to domain

  • :file_paths (String, Array) — default: ''

    path to files. Accepts glob notation

  • :paths (Hash) — default: {home: '/'}

    labeled relative path to URLs. Statistics only

  • :commit (String)

    accepts sha, “working”, “last”, or “master”.

  • :config_file (String) — default: 'maximus.yml'

    path to config file

  • :compile_assets (Boolean) — default: true

    compile and destroy assets automagically

Since:

  • 0.1.3



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/maximus/config.rb', line 35

def initialize(opts = {})

  default_options = {
    root_dir: root_dir,
    domain: 'http://localhost',
    port: is_rails? ? 3000 : '',
    paths: { 'home' => '/' },
    is_dev: false,
    phantomas: false,
    stylestats: false,
    wraith: false
  }

  yaml = default_options.merge load_config_file(opts[:config_file])
  @settings = yaml.merge opts

  # Only set log file if it's set to true.
  #   Otherwise, allow it to be nil or a path
  @settings[:log] = false if @settings[:log].nil?
  @settings[:log] ||= 'log/maximus.log' if @settings[:log].is_a?(TrueClass)

  @settings[:git_log] = false if @settings[:git_log].nil?
  @settings[:git_log] ||= 'log/maximus_git.log' if @settings[:git_log].is_a?(TrueClass)

  @settings[:compile_assets] = true if @settings[:compile_assets].nil?

  @settings[:paths] = split_paths(@settings[:paths]) if @settings[:paths].is_a?(Array)

  group_families

  # Instance variables for Config class only
  @temp_files = {}
  # Override options with any defined in a discovered config file
  evaluate_settings
end

Instance Attribute Details

#settingsHash (readonly)

all the options

Returns:

  • (Hash)

    the current value of settings

Since:

  • 0.1.3



10
11
12
# File 'lib/maximus/config.rb', line 10

def settings
  @settings
end

#temp_filesHash (readonly)

Filename without extension => path to temp file

Returns:

  • (Hash)

    the current value of temp_files

Since:

  • 0.1.3



10
11
12
# File 'lib/maximus/config.rb', line 10

def temp_files
  @temp_files
end

Instance Method Details

#destroy_temp(filename = nil) ⇒ Object

Remove all or one created temporary config file

Parameters:

  • filename (String) (defaults to: nil)

    (nil) file to destroy If nil, destroy all temp files

See Also:

  • #temp_it

Since:

  • 0.1.3



155
156
157
158
159
160
161
162
163
164
# File 'lib/maximus/config.rb', line 155

def destroy_temp(filename = nil)
  if filename.nil?
    @temp_files.each { |filename, file| file.unlink }
    @temp_files = {}
  else
    return if @temp_files[filename.to_sym].blank?
    @temp_files[filename.to_sym].unlink
    @temp_files.delete(filename.to_sym)
  end
end

#domainString

Combine domain with port if necessary

Returns:

  • (String)

    complete domain/host address

Since:

  • 0.1.3



168
169
170
# File 'lib/maximus/config.rb', line 168

def domain
  (!@settings[:port].blank?) ? "#{@settings[:domain]}:#{@settings[:port]}" : @settings[:domain]
end

#evaluate_settings(settings_data = @settings) ⇒ Hash

Set global options or generate appropriate config files for lints or statistics

Parameters:

  • settings_data (Hash) (defaults to: @settings)

    (@settings) loaded data from the discovered maximus config file

Returns:

  • (Hash)

    paths to temp config files and static options These should be deleted with destroy_temp after read and loaded

Since:

  • 0.1.3



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/maximus/config.rb', line 75

def evaluate_settings(settings_data = @settings)
  settings_data.each do |key, value|
    next if value.is_a?(FalseClass)
    value = {} if value.is_a?(TrueClass)

    case key

      when :jshint, :JSHint, :JShint

        # @todo DRY this load_config up, but can't call it at the start because of the
        #   global config variables (last when statement in this switch)
        value = load_config(value)

        if settings_data[key].is_a?(Hash) && settings_data[key].has_key?('jshintignore')
          jshintignore_file = []
          settings_data[key]['jshintignore'].each { |i| jshintignore_file << "#{i}\n" }
          @settings[:jshintignore] = temp_it('jshintignore.json', jshintignore_file)
        end
        @settings[:jshint] = temp_it('jshint.json', value.to_json)

      when :scsslint, :SCSSlint
        value = load_config(value)

        @settings[:scsslint] = temp_it('scsslint.yml', value.to_yaml)

      when :rubocop, :Rubocop, :RuboCop
        value = load_config(value)

        @settings[:rubocop] = temp_it('rubocop.yml', value.to_yaml)

      when :brakeman
        @settings[:brakeman] = settings_data[key]

      when :rails_best_practice, :railsbp
        @settings[:railsbp] = settings_data[key]

      when :stylestats, :Stylestats
        value = load_config(value)
        @settings[:stylestats] = temp_it('stylestats.json', value.to_json)

      when :phantomas, :Phantomas
        value = load_config(value)
        @settings[:phantomas] = temp_it('phantomas.json', value.to_json)

      when :wraith, :Wraith
        value = load_config(value)
        evaluate_for_wraith(value)

      # Configuration important to all of maximus
      when :is_dev, :log, :root_dir, :domain, :port, :paths, :commit
        @settings[key] = settings_data[key]
    end
  end

  # Finally, we're done
  @settings
end

#is_dev?Boolean

If output should be returned to console

in a pretty display

Returns:

  • (Boolean)

Since:

  • 0.1.3



136
137
138
# File 'lib/maximus/config.rb', line 136

def is_dev?
  @settings[:is_dev]
end

#logLogger

Defines base logger

Parameters:

  • out (String, STDOUT)

    location for logging Accepts file path

Returns:

  • (Logger)

    self.log

Since:

  • 0.1.3



144
145
146
147
148
149
# File 'lib/maximus/config.rb', line 144

def log
  out = @settings[:log] || STDOUT
  @log ||= Logger.new(out)
  @log.level ||= Logger::INFO
  @log
end