Sha256: b3183e393368de324351af098cb1509cc9867d66a28874a1d00ec919857d1061

Contents?: true

Size: 1.67 KB

Versions: 46

Compression:

Stored size: 1.67 KB

Contents

#!/usr/bin/env ruby

require 'utils'
require 'tins/go'
include Tins::GO
require 'tins/xt/string'
require 'term/ansicolor'

def path_shifter(string, separator: ?/, n: nil)
  n or return string
  n, path = n.to_i, string.split(separator)
  if n < 0
    path = path.slice(n..-1)
  else
    path.slice!(0...n)
  end
  path * separator
end

def underscore(string)
  string = path_shifter(string, n: $opts[?n], separator: '::')
  string = Tins::StringUnderscore.instance_method(:underscore).bind(string).()
  $opts[?s] and string << '.rb'
  string
end

def parameterize(string, separator)
  underscore(string).gsub(?/, separator) # quick and dirty
end

def camelize(string)
  string = path_shifter(string, n: $opts[?n])
  string = string.gsub(/#{Regexp.quote(File.extname(string))}\Z/, '')
  string.camelize
end

def camelcase?(string)
  string[0, 1] =~ /[A-Z]/
end

def compute_shift(config, string)
  string = underscore(string)
  result = config.classify.shift_path_by_default
  for prefix in config.classify.shift_path_for_prefix
    if string.start_with? prefix
      return prefix.count(?/) + 1
    end
  end
  result
end

$opts = go 'bdtsp:n:'


string = ARGV.shift or fail "need a class/filepath/filename"
string = Term::ANSIColor.uncolor string

config = Utils::ConfigFile.new
config.configure_from_paths

$opts[?n] ||= compute_shift(config, string)

$opts[?b] and $opts[?n] = '-1'
print case
when $opts[?t]
  if camelcase?(string)
    if separator = $opts[?p]
      parameterize string, separator
    else
      underscore string
    end
  else
    camelize string
  end
when $opts[?d]
  if separator = $opts[?p]
    parameterize string, separator
  else
    underscore string
  end
else
  camelize string
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
utils-0.38.0 bin/classify
utils-0.37.1 bin/classify
utils-0.37.0 bin/classify
utils-0.36.0 bin/classify
utils-0.35.0 bin/classify
utils-0.34.0 bin/classify
utils-0.33.1 bin/classify
utils-0.33.0 bin/classify
utils-0.32.0 bin/classify
utils-0.31.1 bin/classify
utils-0.31.0 bin/classify
utils-0.30.2 bin/classify
utils-0.30.1 bin/classify
utils-0.30.0 bin/classify
utils-0.29.0 bin/classify
utils-0.28.0 bin/classify
utils-0.27.0 bin/classify
utils-0.26.1 bin/classify
utils-0.26.0 bin/classify
utils-0.25.0 bin/classify