Class: Maximus::Config

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

Overview

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 = {}) ⇒ void

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

Since:

  • 0.1.3


29
30
31
32
33
34
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
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/maximus/config.rb', line 29

def initialize(opts = {})
  opts[:is_dev] ||= false

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

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

  # @see Helper#root_dir
  opts[:root_dir] ||= root_dir
  opts[:domain] ||= 'http://localhost'
  opts[:port] ||= is_rails? ? 3000 : ''
  opts[:paths] ||= { 'home' => '/' }

  # Accounting for space-separated command line arrays
  if opts[:paths].is_a?(Array)
    new_paths = {}
    opts[:paths].each do |p|
      if p.split('/').length > 1
        new_paths[p.split('/').last.to_s] = p
      else
        new_paths['home'] = '/'
      end
    end
    opts[:paths] = new_paths
  end

  # What we're really interested in
  @settings = opts

  # Instance variables for Config class only
  @temp_files = {}

  conf_location = (opts[:config_file] && File.exist?(opts[:config_file])) ? opts[:config] : find_config

  @yaml = YAML.load_file(conf_location)

  # Match defaults
  @yaml['domain'] ||= @settings[:domain]
  @yaml['paths'] ||= @settings[:paths]
  @yaml['port'] ||= @settings[:port]
  set_families('lints', ['jshint', 'scsslint', 'rubocop', 'brakeman', 'railsbp'])
  set_families('frontend', ['jshint', 'scsslint', 'phantomas', 'stylestats', 'wraith'])
  set_families('backend', ['rubocop', 'brakeman', 'railsbp'])
  set_families('ruby', ['rubocop', 'brakeman', 'railsbp'])
  set_families('statistics', ['phantomas', 'stylestats', 'wraith'])
  set_families('all', ['lints', 'statistics'])

  # Override options with any defined in a discovered config file
  evaluate_yaml
end

Instance Attribute Details

#settingsHash (readonly)

all the options

Returns:

  • (Hash)

    the current value of settings

Since:

  • 0.1.3


5
6
7
# File 'lib/maximus/config.rb', line 5

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


5
6
7
# File 'lib/maximus/config.rb', line 5

def temp_files
  @temp_files
end

Instance Method Details

#destroy_temp(filename = nil) ⇒ void

This method returns an undefined value.

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
  • #yaml_evaluate

Since:

  • 0.1.3


203
204
205
206
207
208
209
210
211
212
# File 'lib/maximus/config.rb', line 203

def destroy_temp(filename = nil)
  return if @temp_files[filename.to_sym].blank?
  if filename.nil?
    @temp_files.each { |filename, file| file.unlink }
    @temp_files = {}
  else
    @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


217
218
219
# File 'lib/maximus/config.rb', line 217

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

#evaluate_yaml(yaml_data = @yaml) ⇒ Hash

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

Parameters:

  • yaml_data (Hash) (defaults to: @yaml)

    (@yaml) 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


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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/maximus/config.rb', line 88

def evaluate_yaml(yaml_data = @yaml)
  yaml_data.each do |key, value|
    unless value.is_a?(FalseClass)
      value = {} if value.is_a?(TrueClass)

      case key

        when 'jshint', 'JSHint', 'JShint'

          # @todo DRY this 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 yaml_data[key].is_a?(Hash) && yaml_data[key].has_key?['ignore']
            jshintignore_file = []
            yaml_data[key]['ignore'].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', 'scss-lint', '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] = yaml_data[key]

        when 'rails_best_practice', 'railsbp'
          @settings[:railsbp] = yaml_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)

          @settings[:wraith] = {}
          if value.include?('browser')
            value['browser'].each do |browser, browser_value|
              unless browser_value.is_a?(FalseClass)
                new_data = {}
                new_data['browser'] = []
                new_data['browser'] << { browser.to_s => browser.to_s }

                # Regardless of what's in the config, override with maximus,
                #   predictable namespacing
                new_data['directory'] = "maximus_wraith_#{browser}"
                new_data['history_dir'] = "maximus_wraith_history_#{browser}"

                # @todo a snap file cannot be set in the config
                snap_file = case browser
                  when 'casperjs' then 'casper'
                  when 'nojs' then 'nojs'
                  else 'snap'
                end
                new_data['snap_file'] = File.join(File.dirname(__FILE__), "config/wraith/#{snap_file}.js")

                @settings[:wraith][browser.to_sym] = wraith_setup(new_data, "wraith_#{browser}")
              end
            end
          else
            value['browser'] = { 'phantomjs' => 'phantomjs' }
            value['directory'] = 'maximus_wraith_phantomjs'
            value['history_dir'] = 'maximus_wraith_history_phantomjs'
            value['snap_file'] = File.join(File.dirname(__FILE__), "config/wraith/snap.js")
            @settings[:wraith][:phantomjs] = wraith_setup(value)
          end

        # Configuration important to all of maximus
        when 'is_dev', 'log', 'root_dir', 'domain', 'port', 'paths', 'commit'
          @settings[key.to_sym] = yaml_data[key]
      end
    end
  end

  # Finally, we're done
  @settings
end

#is_dev?Boolean

Returns:

  • (Boolean)

Since:

  • 0.1.3


179
180
181
# File 'lib/maximus/config.rb', line 179

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


188
189
190
191
192
193
# File 'lib/maximus/config.rb', line 188

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