Module: Maximus::Helper
- Included in:
- Config, GitControl, Lint, Statistic
- Defined in:
- lib/maximus/helper.rb
Overview
Methods used in more than one place
Instance Method Summary collapse
-
#check_default_config_path(file) ⇒ String
Look for a file in the config directory.
-
#edit_yaml(yaml_location, &block) ⇒ void
Edit and save a YAML file.
-
#file_count(path, ext = 'scss') ⇒ Integer
Count how many files were linted.
-
#file_list(path, ext = 'scss', remover = '') ⇒ Array<String>
Find all files that were linted by extension.
-
#is_rails? ⇒ Boolean
See if project linted is a Rails app This will usually be stored as a class variable in the inherited class.
-
#lines_added_to_range(file) ⇒ Hash
Convert the array from lines_added into spelled-out ranges This is a GitControl helper but it’s used in Lint.
-
#node_module_exists(command, install_instructions = 'npm install -g') ⇒ void
Verify that command is available on the box before continuing.
-
#path_exists?(path = @path) ⇒ Boolean
Ensure path exists.
-
#prompt(*args) ⇒ String
Request user input.
-
#reporter_path(filename) ⇒ String
Grab the absolute path of the reporter file.
-
#root_dir ⇒ String
Get root directory of file being called.
-
#truthy?(str) ⇒ Boolean
Convert string to boolean.
Instance Method Details
#check_default_config_path(file) ⇒ String
Look for a file in the config directory
45 46 47 |
# File 'lib/maximus/helper.rb', line 45 def check_default_config_path(file) File.exist?(file) ? file : File.join(File.dirname(__FILE__), file) end |
#edit_yaml(yaml_location, &block) ⇒ void
This method returns an undefined value.
Edit and save a YAML file
88 89 90 91 92 |
# File 'lib/maximus/helper.rb', line 88 def edit_yaml(yaml_location, &block) d = YAML.load_file(yaml_location) block.call(d) File.open(yaml_location, 'w') {|f| f.write d.to_yaml } end |
#file_count(path, ext = 'scss') ⇒ Integer
Count how many files were linted
73 74 75 |
# File 'lib/maximus/helper.rb', line 73 def file_count(path, ext = 'scss') file_list(path, ext).length end |
#file_list(path, ext = 'scss', remover = '') ⇒ Array<String>
Find all files that were linted by extension
61 62 63 64 65 66 |
# File 'lib/maximus/helper.rb', line 61 def file_list(path, ext = 'scss', remover = '') # Necessary so that directories aren't counted collect_path = path.include?("*") ? path : "#{path}/**/*.#{ext}" # Remove first slash from path if present. probably a better way to do this. Dir[collect_path].collect { |file| file.gsub(remover, '').gsub(/^\/app\//, 'app/') if File.file?(file) } end |
#is_rails? ⇒ Boolean
See if project linted is a Rails app This will usually be stored as a class variable in the inherited class
16 17 18 |
# File 'lib/maximus/helper.rb', line 16 def is_rails? defined?(Rails) end |
#lines_added_to_range(file) ⇒ Hash
I’m sure there’s a better way of doing this
figure out a better place to put this than in Helper
Convert the array from lines_added into spelled-out ranges This is a GitControl helper but it’s used in Lint
116 117 118 119 |
# File 'lib/maximus/helper.rb', line 116 def lines_added_to_range(file) changes_array = file[:changes].map { |ch| ch.split("..").map(&:to_i) } changes_array.map { |e| (e[0]..e[1]).to_a }.flatten! end |
#node_module_exists(command, install_instructions = 'npm install -g') ⇒ void
This method returns an undefined value.
Verify that command is available on the box before continuing
31 32 33 34 35 36 37 38 |
# File 'lib/maximus/helper.rb', line 31 def node_module_exists(command, install_instructions = 'npm install -g') cmd = `if hash #{command} 2>/dev/null; then echo "true"; else echo "false"; fi` if cmd.include? "false" command_msg = "Missing command #{command}" abort "#{command_msg}: Please run `#{install_instructions} #{command}` And try again\n" exit 1 end end |
#path_exists?(path = @path) ⇒ Boolean
Ensure path exists
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/maximus/helper.rb', line 124 def path_exists?(path = @path) path = path.split(' ') if path.is_a?(String) && path.include?(' ') if path.is_a?(Array) path.each do |p| unless File.exist?(p) puts "#{p} does not exist" return false end end else if File.exist?(path) return true else puts "#{path} does not exist" return false end end end |
#prompt(*args) ⇒ String
Request user input
97 98 99 100 |
# File 'lib/maximus/helper.rb', line 97 def prompt(*args) print(*args) STDIN.gets end |
#reporter_path(filename) ⇒ String
Grab the absolute path of the reporter file
52 53 54 |
# File 'lib/maximus/helper.rb', line 52 def reporter_path(filename) File.join(File.dirname(__FILE__), "reporter/#{filename}") end |
#root_dir ⇒ String
Get root directory of file being called
22 23 24 |
# File 'lib/maximus/helper.rb', line 22 def root_dir is_rails? ? Rails.root.to_s : Dir.pwd.to_s end |
#truthy?(str) ⇒ Boolean
Convert string to boolean
80 81 82 83 |
# File 'lib/maximus/helper.rb', line 80 def truthy?(str) return true if str == true || str =~ (/^(true|t|yes|y|1)$/i) return false if str == false || str.blank? || str =~ (/^(false|f|no|n|0)$/i) end |