Class: Tailor::Configuration
- Inherits:
-
Object
- Object
- Tailor::Configuration
- Includes:
- Logger::Mixin
- Defined in:
- lib/tailor/configuration.rb,
lib/tailor/configuration/style.rb,
lib/tailor/configuration/file_set.rb
Overview
Pulls in any configuration from the places configuration can be set:
1. ~/.tailorrc
2. CLI options
3. Default options
It then basically represents a list of “file sets” and the rulers that should be applied against each file set.
If a file list is given from the CLI and a configuration file is given/found, tailor uses the style settings for the default file set and only checks the default file set.
Defined Under Namespace
Constant Summary collapse
- DEFAULT_RC_FILE =
Dir.home + '/.tailorrc'
- DEFAULT_PROJECT_CONFIG =
Dir.pwd + '/.tailor'
Instance Attribute Summary collapse
-
#file_sets ⇒ Object
readonly
Returns the value of attribute file_sets.
-
#formatters(*new_formatters) ⇒ Array
readonly
The list of formatters.
Class Method Summary collapse
Instance Method Summary collapse
-
#config_file ⇒ String
Name of the config file to use.
-
#file_set(file_expression = 'lib/**/*.rb', label = :default) {|new_style| ... } ⇒ Object
Adds a file set to the list of file sets in the Configuration object.
-
#get_file_sets_from_cli_opts ⇒ Object
If any files are given from the CLI, this gets that list of files and replaces those in any :default file set.
- #get_file_sets_from_config_file ⇒ Object
- #get_formatters_from_cli_opts ⇒ Object
- #get_formatters_from_config_file ⇒ Object
- #get_style_from_cli_opts ⇒ Object
-
#initialize(runtime_file_list = nil, options = nil) ⇒ Configuration
constructor
A new instance of Configuration.
-
#load! ⇒ Object
Call this to load settings from the config file and from CLI options.
-
#load_from_config_file(config_file) ⇒ Object
Tries to open the file at the path given at
config_file
and read in the configuration given there. -
#recursive_file_set(file_expression, label = :default) ⇒ Object
A helper to #file_set that allows you to specify ‘*.rb’ to get all files ending with
.rb
in your current path and deeper. -
#show ⇒ Object
Displays the current configuration as a text table.
Methods included from Logger::Mixin
Constructor Details
#initialize(runtime_file_list = nil, options = nil) ⇒ Configuration
Returns a new instance of Configuration.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/tailor/configuration.rb', line 39 def initialize(runtime_file_list=nil, =nil) @formatters = ['text'] @file_sets = {} @runtime_file_list = runtime_file_list log "Got runtime file list: #{@runtime_file_list}" @options = log "Got options: #{@options}" unless @options.nil? @config_file = @options.config_file unless @options.config_file.empty? end end |
Instance Attribute Details
#file_sets ⇒ Object (readonly)
Returns the value of attribute file_sets.
31 32 33 |
# File 'lib/tailor/configuration.rb', line 31 def file_sets @file_sets end |
#formatters(*new_formatters) ⇒ Array (readonly)
Returns The list of formatters.
173 174 175 |
# File 'lib/tailor/configuration.rb', line 173 def formatters @formatters end |
Class Method Details
.default ⇒ Hash
27 28 29 |
# File 'lib/tailor/configuration.rb', line 27 def self.default new end |
Instance Method Details
#config_file ⇒ String
Returns Name of the config file to use.
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/tailor/configuration.rb', line 96 def config_file return @config_file if @config_file if File.exists?(DEFAULT_PROJECT_CONFIG) return @config_file = DEFAULT_PROJECT_CONFIG end if File.exists?(DEFAULT_RC_FILE) return @config_file = DEFAULT_RC_FILE end end |
#file_set(file_expression = 'lib/**/*.rb', label = :default) {|new_style| ... } ⇒ Object
Adds a file set to the list of file sets in the Configuration object.
184 185 186 187 188 189 190 191 192 193 |
# File 'lib/tailor/configuration.rb', line 184 def file_set(file_expression='lib/**/*.rb', label=:default) log "file sets before: #{@file_sets}" log "file set label #{label}" new_style = Style.new yield new_style if block_given? @file_sets[label] = FileSet.new(file_expression, new_style) log "file sets after: #{@file_sets}" end |
#get_file_sets_from_cli_opts ⇒ Object
If any files are given from the CLI, this gets that list of files and replaces those in any :default file set.
151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/tailor/configuration.rb', line 151 def get_file_sets_from_cli_opts return if @runtime_file_list.nil? || @runtime_file_list.empty? # Only use options set for the :default file set because the user gave # a different set of files to measure. @file_sets.delete_if { |k, v| k != :default } if @file_sets.include? :default @file_sets[:default].file_list = @runtime_file_list else @file_sets = { default: FileSet.new(@runtime_file_list) } end end |
#get_file_sets_from_config_file ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/tailor/configuration.rb', line 108 def get_file_sets_from_config_file return if @config_from_file.file_sets.empty? @config_from_file.file_sets.each do |label, file_set| log "label: #{label}" log "file set file list: #{file_set[:file_list]}" log "file set style: #{file_set[:style]}" if @file_sets[label] log "label already exists. Updating..." @file_sets[label].update_file_list(file_set[:file_list]) @file_sets[label].update_style(file_set[:style]) else log "Creating new label..." @file_sets[label] = FileSet.new(file_set[:file_list], file_set[:style]) end end end |
#get_formatters_from_cli_opts ⇒ Object
165 166 167 168 169 170 |
# File 'lib/tailor/configuration.rb', line 165 def get_formatters_from_cli_opts unless @options.nil? || @options.formatters.empty? || @options.formatters.nil? @formatters = @options.formatters log "@formatters is now #{@formatters}" end end |
#get_formatters_from_config_file ⇒ Object
128 129 130 131 132 133 |
# File 'lib/tailor/configuration.rb', line 128 def get_formatters_from_config_file return if @config_from_file.formatters.empty? @formatters = @config_from_file.formatters log "@formatters is now #{@formatters}" end |
#get_style_from_cli_opts ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/tailor/configuration.rb', line 135 def get_style_from_cli_opts return unless @options && @options.style @options.style.each do |property, value| @file_sets.keys.each do |label| if value == :off || value == "off" @file_sets[label].style[property][1] = { level: :off } else @file_sets[label].style[property][0] = value end end end end |
#load! ⇒ Object
Call this to load settings from the config file and from CLI options.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/tailor/configuration.rb', line 54 def load! if config_file load_from_config_file(config_file) if @config_from_file get_formatters_from_config_file #get_file_sets_from_config_file unless @runtime_file_list get_file_sets_from_config_file end else log "Creating default file set..." @file_sets = { default: FileSet.new(@runtime_file_list) } end get_formatters_from_cli_opts get_file_sets_from_cli_opts get_style_from_cli_opts end |
#load_from_config_file(config_file) ⇒ Object
Tries to open the file at the path given at config_file
and read in the configuration given there.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/tailor/configuration.rb', line 77 def load_from_config_file(config_file) user_config_file = File.(config_file) if File.exists? user_config_file log "Loading config from file: #{user_config_file}" begin @config_from_file = instance_eval(File.read(user_config_file), user_config_file) log "Got new config from file: #{user_config_file}" rescue LoadError => ex raise Tailor::RuntimeError, "Couldn't load config file: #{user_config_file}" end else log "No config file found at #{user_config_file}." end end |
#recursive_file_set(file_expression, label = :default) ⇒ Object
A helper to #file_set that allows you to specify ‘*.rb’ to get all files ending with .rb
in your current path and deeper.
200 201 202 |
# File 'lib/tailor/configuration.rb', line 200 def recursive_file_set(file_expression, label=:default) file_set("*/**/#{file_expression}", label) end |
#show ⇒ Object
Displays the current configuration as a text table.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/tailor/configuration.rb', line 205 def show table = Text::Table.new(horizontal_padding: 4) table.head = [{ value: 'Configuration', colspan: 2, align: :center }] table.rows << :separator table.rows << ['Formatters', @formatters] @file_sets.each do |label, file_set| table.rows << :separator table.rows << ['Label', label] table.rows << ['Style', ''] file_set[:style].each do |k, v| table.rows << ['', "#{k}: #{v}"] end table.rows << ['File List', ''] file_set[:file_list].each { |file| table.rows << ['', file] } end puts table end |