Module: Maximus::Helper

Included in:
Config, GitControl, Lint, Statistic
Defined in:
lib/maximus/helper.rb

Overview

Methods used in more than one place

Since:

  • 0.1.0

Instance Method Summary collapse

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

Parameters:

  • root (String) (defaults to: @config.working_dir)

    base directory

  • folder (String) (defaults to: '')

    nested folder to search for for Rails or Middleman

  • extension (String) (defaults to: '')

    file glob type to search for if neither

Returns:

  • (String)

    path to desired files

Since:

  • 0.1.6.1



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

Parameters:

  • yaml_location (String)

    YAML absolute file path

Since:

  • 0.1.0



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

Parameters:

  • path (String)

    path to folders

  • ext (String) (defaults to: 'scss')

    file extension to search for

Returns:

  • (Integer)

    number of files matched by the path

Since:

  • 0.1.0



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

Parameters:

  • path (String)

    path to folders

  • ext (String) (defaults to: 'scss')

    file extension to search for

Returns:

  • (Array<String>)

    list of file paths

Since:

  • 0.1.0



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

Returns:

  • (Boolean)

Since:

  • 0.1.6.1



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

Returns:

  • (Boolean)

Since:

  • 0.1.0



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

Parameters:

  • command (String)

    command to check

  • install_instructions (String) (defaults to: 'npm install -g')

    how to install the missing command

Since:

  • 0.1.0



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

Parameters:

  • path (String, Array) (defaults to: @path)

    path to files can be directory or glob

Returns:

  • (Boolean)

Since:

  • 0.1.0



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

Parameters:

  • args (Array<String>)

    prompts to request

Returns:

  • (String)

    user input to use elsewhere

Since:

  • 0.1.0



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

Parameters:

  • filename (String)

Returns:

  • (String)

    absolute path to the reporter file

Since:

  • 0.1.0



50
51
52
# File 'lib/maximus/helper.rb', line 50

def reporter_path(filename)
  File.join(File.dirname(__FILE__), 'reporter', filename)
end

#root_dirString

Get root directory of file being called

Returns:

  • (String)

    absolute path to root directory

Since:

  • 0.1.0



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

Parameters:

  • str (String)

    the string to evaluate

Returns:

  • (Boolean)

    whether or not the string is true

Since:

  • 0.1.0



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