Module: Maximus::Helper

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

Overview

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#check_default_config_path(file) ⇒ String

Look for a file in the config directory

Parameters:

  • file (String)

    filename with extension to search for

Returns:

  • (String)

    path to default config file or file in user's directory

Since:

  • 0.1.0


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

def check_default_config_path(file)
  File.exist?(file) ? file : File.join(File.dirname(__FILE__), "config/#{file}")
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


96
97
98
99
100
# File 'lib/maximus/helper.rb', line 96

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


79
80
81
# File 'lib/maximus/helper.rb', line 79

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


67
68
69
70
71
72
# File 'lib/maximus/helper.rb', line 67

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

Returns:

  • (Boolean)

Since:

  • 0.1.0


16
17
18
# File 'lib/maximus/helper.rb', line 16

def is_rails?
  defined?(Rails)
end

#lines_added_to_range(file) ⇒ Hash

TODO:

I'm sure there's a better way of doing this

TODO:

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

Examples:

typical output

lines_added = {'filename' => ['0..10', '11..14']}
lines_added_to_range(lines_added)
# output
{
  'filename': {
    {
      [0,1,2,3,4,5,6,7,8,9,10],
      [11,12,13,14]
    }
  }
}

Returns:

  • (Hash)

    changes_array of spelled-out arrays of integers

See Also:

  • GitControl#lines_added
  • Lint#relevant_lint

Since:

  • 0.1.0


132
133
134
135
# File 'lib/maximus/helper.rb', line 132

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

Parameters:

  • command (String)

    command to check

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

    how to install the missing command

Since:

  • 0.1.0


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/maximus/helper.rb', line 32

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}".color(:red)
    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


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/maximus/helper.rb', line 141

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

Parameters:

  • args (Array<String>)

    prompts to request

Returns:

  • (String)

    user input to use elsewhere

Since:

  • 0.1.0


106
107
108
109
# File 'lib/maximus/helper.rb', line 106

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


58
59
60
# File 'lib/maximus/helper.rb', line 58

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


23
24
25
# File 'lib/maximus/helper.rb', line 23

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


87
88
89
90
# File 'lib/maximus/helper.rb', line 87

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