Module: Forgitter

Defined in:
lib/forgitter.rb,
lib/forgitter/cli.rb,
lib/forgitter/runner.rb,
lib/forgitter/options.rb,
lib/forgitter/version.rb,
lib/forgitter/ignorefiles.rb,
lib/forgitter/cli/option_parser.rb

Defined Under Namespace

Modules: CLI Classes: Runner

Constant Summary collapse

DATA_PATH =
File.realpath(File.join(File.dirname(__FILE__), '..', 'data'))
DEFAULT_OPTIONS =
{
  :list => false,
  :tags => []
}
VERSION =
'0.1.4'

Class Method Summary collapse

Class Method Details

.filter(options = {}) ⇒ Array

Filter ignorefiles by tags.

If tags is empty, this will return all ignorefiles.

Parameters:

  • the (Hash)

    hash of options containing tag strings

Returns:

  • (Array)

    the array of filtered ignorefiles



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/forgitter/ignorefiles.rb', line 20

def self.filter(options = {})
  return ignorefiles if options[:tags].empty?

  ignorefiles.select do |ignorefile|
    selected = true
    options[:tags].uniq.each do |tag|
      selected &&= ignorefile[:tags].count(tag) >= options[:tags].count(tag)
    end
    selected
  end
end

.ignorefile(path) ⇒ String

Pull a parameterized ignorefile out of the given path.

Parameters:

  • the (String)

    path to a .gitignore file

Returns:

  • (String)

    the ignorefile



53
54
55
# File 'lib/forgitter/ignorefiles.rb', line 53

def self.ignorefile(path)
  parameterize(File.basename(path).sub('.gitignore', ''))
end

.ignorefilesArray

Fetch all available ignorefiles.

Returns:

  • (Array)

    the array of available ignorefiles



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/forgitter/ignorefiles.rb', line 82

def self.ignorefiles
  unless defined?(@@ignorefiles) && !@@ignorefiles.empty?
    @@ignorefiles = []

    paths.each do |path|
      @@ignorefiles << {
        :path => path,
        :name => ignorefile(path),
        :tags => tags(path)
      }
    end
  end
  @@ignorefiles
end

.list(tags = []) ⇒ Object

Output a list of ignorefile tags along with the relative path to the gitignore, formatted into columns.

Parameters:

  • tags (Array) (defaults to: [])

    The list of tags to filter.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/forgitter/ignorefiles.rb', line 103

def self.list(tags = [])
  ignorefiles = filter({ :tags => tags })
  if ignorefiles.empty?
    puts 'No ignorefiles found!'
  else
    lines = []
    col1size = 0

    ignorefiles.each do |ignorefile|
      id = ignorefile[:tags].join(' ')
      col1size = id.length if id.length > col1size
      lines << [id, ignorefile[:path]]
    end

    lines.sort_by { |line| line[0] }.each do |line|
      printf("%-#{col1size}s\t%s\n", line[0], line[1])
    end
  end
end

.parameterize(str) ⇒ String

Strip unnecessary characters and downcase the given string.

Parameters:

  • any (String)

    string

Returns:

  • (String)

    the “parameterized” string



8
9
10
# File 'lib/forgitter/ignorefiles.rb', line 8

def self.parameterize(str)
  str.gsub(/[^a-z0-9+]+/i, '').downcase
end

.pathsArray

Fetch all available ignorefile paths, relative to the DATA_PATH.

.gitignore files placed directly under DATA_PATH are ignored.

Returns:

  • (Array)

    the array of available ignorefile paths



39
40
41
42
43
44
45
# File 'lib/forgitter/ignorefiles.rb', line 39

def self.paths
  @@paths ||= Dir["#{DATA_PATH}/**/*.gitignore"].map do |path|
    path.sub("#{DATA_PATH}/", '')
  end.select do |path|
    path =~ /\//
  end
end

.tags(path) ⇒ Array

Pull parameterized tags out of the given path.

If path does not contain a /, this just returns the ignorefile name in an array.

Parameters:

  • the (String)

    path to a .gitignore file

Returns:

  • (Array)

    the tags



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/forgitter/ignorefiles.rb', line 65

def self.tags(path)
  tags = []
  if path =~ /\//
    tags = path.sub("/#{File.basename(path)}", '').split('/')
    tags.map! do |tag|
      parameterize(tag)
    end
  end
  tags << ignorefile(path)
  tags
end