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
-
#discover_path(root = @config.working_dir, folder = '', extension = '') ⇒ String
Default paths to check for lints and some stats Note: is_rails? must be defined second-to-last if more frameworks are added.
-
#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_middleman? ⇒ Boolean
See if project is a Middleman app.
-
#is_rails? ⇒ Boolean
See if project linted is a Rails app This will usually be stored as a class variable in the inherited class.
-
#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
#discover_path(root = @config.working_dir, folder = '', extension = '') ⇒ String
Default paths to check for lints and some stats Note: is_rails? must be defined second-to-last if more frameworks are added
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/maximus/helper.rb', line 130 def discover_path(root = @config.working_dir, folder = '', extension = '') return @path unless @path.blank? if is_middleman? File.join(root, 'source', folder) elsif is_rails? File.join(root, 'app', 'assets', folder) else extension.blank? ? File.join(root) : File.join(root, '/**', "/*.#{extension}") end end |
#edit_yaml(yaml_location, &block) ⇒ void
This method returns an undefined value.
Edit and save a YAML file
86 87 88 89 90 |
# File 'lib/maximus/helper.rb', line 86 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
71 72 73 |
# File 'lib/maximus/helper.rb', line 71 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
59 60 61 62 63 64 |
# File 'lib/maximus/helper.rb', line 59 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_middleman? ⇒ Boolean
See if project is a Middleman app
23 24 25 |
# File 'lib/maximus/helper.rb', line 23 def is_middleman? (@config.settings[:framework] == 'middleman' unless @config.blank?) || Gem::Specification::find_all_by_name('middleman').any? 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? (@config.settings[:framework] == 'rails' unless @config.blank?) || defined?(Rails) 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
38 39 40 41 42 43 44 45 |
# File 'lib/maximus/helper.rb', line 38 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
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/maximus/helper.rb', line 103 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 path = path.gsub('/**', '').gsub('/*', '').gsub(/\/\.*/, '') if path.include?('*') if File.exist?(path) return true else puts "#{path} does not exist" return false end end end |
#prompt(*args) ⇒ String
Request user input
95 96 97 98 |
# File 'lib/maximus/helper.rb', line 95 def prompt(*args) print(*args) STDIN.gets end |
#reporter_path(filename) ⇒ String
Grab the absolute path of the reporter file
50 51 52 |
# File 'lib/maximus/helper.rb', line 50 def reporter_path(filename) File.join(File.dirname(__FILE__), 'reporter', filename) end |
#root_dir ⇒ String
Get root directory of file being called
29 30 31 |
# File 'lib/maximus/helper.rb', line 29 def root_dir is_rails? ? Rails.root.to_s : Dir.pwd.to_s end |
#truthy?(str) ⇒ Boolean
Convert string to boolean
78 79 80 81 |
# File 'lib/maximus/helper.rb', line 78 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 |