class GitHooks::Hook

Constants

VALID_PHASES

Attributes

__phases__[R]
phases[R]
args[RW]
limiters[R]
phase[R]
repository[R]
repository_path[R]
sections[R]
staged[RW]
tracked[RW]
untracked[RW]

Public Class Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/githooks/hook.rb, line 53
def method_missing(method, *args, &block)
  return super unless instance.public_methods.include? method
  instance.public_send(method, *args, &block)
end
new(phase) click to toggle source
# File lib/githooks/hook.rb, line 67
def initialize(phase)
  @phase      = phase.to_s
  @sections   = {}
  @limiters   = {}
  @commands   = []
  @args       = []
  @staged     = true
  @tracked    = false
  @untracked  = false
  @repository = Repository.new(Dir.getwd)
end
register(phase, &block) click to toggle source
# File lib/githooks/hook.rb, line 58
def register(phase, &block)
  fail ArgumentError, 'expected block, received none' unless block_given?
  instance(phase).instance_eval(&block)
end

Private Class Methods

[](phase = 'pre-commit')
Alias for: instance
instance(phase = 'pre-commit') click to toggle source
# File lib/githooks/hook.rb, line 34
def instance(phase = 'pre-commit') # rubocop:disable AbcSize
  phase = phase.to_s
  unless VALID_PHASES.include? phase
    fail ArgumentError, "Hook phase (#{phase}) must be one of #{VALID_PHASES.join(', ')}"
  end

  unless phases[phase]
    @__mutex__.synchronize {
      return phases[phase] if phases[phase]
      phases[phase] = new(phase)
    }
  end
  phases[phase]
end
Also aliased as: []

Public Instance Methods

[](name) click to toggle source
# File lib/githooks/hook.rb, line 79
def [](name)
  @sections[name]
end
command(name, options = {}) click to toggle source
# File lib/githooks/hook.rb, line 146
def command(name, options = {})
  setup_command name, options
end
commands(*names) click to toggle source
# File lib/githooks/hook.rb, line 150
def commands(*names)
  return @commands if names.empty?
  names.each { |name| command name }
end
config_file(*path_components) click to toggle source
# File lib/githooks/hook.rb, line 127
def config_file(*path_components)
  config_path.join(*path_components)
end
config_path() click to toggle source

FIXME: these should be switched to behaviors that are included into this classs

# File lib/githooks/hook.rb, line 123
def config_path
  GitHooks.hooks_root.join('configs')
end
find_command(name) click to toggle source
# File lib/githooks/hook.rb, line 110
def find_command(name)
  @commands.find { |command| command.name == name.to_s }
end
lib_file(*path_components) click to toggle source
# File lib/githooks/hook.rb, line 135
def lib_file(*path_components)
  lib_path.join(*path_components)
end
lib_path() click to toggle source
# File lib/githooks/hook.rb, line 131
def lib_path
  GitHooks.hooks_root.join('lib')
end
limit(type) click to toggle source
# File lib/githooks/hook.rb, line 139
def limit(type)
  unless @limiters.include? type
    @limiters[type] ||= Repository::Limiter.new(type)
  end
  @limiters[type]
end
manifest() click to toggle source
# File lib/githooks/hook.rb, line 87
def manifest
  @manifest ||= Manifest.new(self)
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/githooks/hook.rb, line 96
def method_missing(method, *args, &block)
  return super unless command = find_command(method) # rubocop:disable AssignmentInCondition
  command.execute(*args, &block)
end
repository_path=(path) click to toggle source
# File lib/githooks/hook.rb, line 83
def repository_path=(path)
  @repository = Repository.new(path)
end
run() click to toggle source
# File lib/githooks/hook.rb, line 91
def run
  # only run sections that have actions matching files in the manifest
  sections.reject { |s| s.actions.empty? }.collect(&:run).all?
end
section(name, &block) click to toggle source
# File lib/githooks/hook.rb, line 155
def section(name, &block)
  key_name = Section.key_from_name(name)
  return @sections[key_name] unless block_given?

  if @sections.include? key_name
    @sections[key_name].instance_eval(&block)
  else
    @sections[key_name] ||= Section.new(name, self, &block)
  end
  self
end

Private Instance Methods

setup_command(name, options = {}) click to toggle source
# File lib/githooks/hook.rb, line 101
def setup_command(name, options = {})
  @commands << SystemUtils::Command.new(
    name.to_sym,
    chdir: options.delete(:chdir),
    bin_path: options.delete(:bin_path)
  )
end