Class: GitHooks::Hook

Inherits:
Object show all
Defined in:
lib/githooks/hook.rb

Defined Under Namespace

Classes: Manifest

Constant Summary collapse

VALID_PHASES =
%w{ pre-commit commit-msg pre-push }.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(phase) ⇒ Hook

Returns a new instance of Hook.



67
68
69
70
71
72
73
74
75
76
77
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



96
97
98
99
# 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

Class Attribute Details

.__phases__Object (readonly) Also known as: phases

Returns the value of attribute __phases__.



31
32
33
# File 'lib/githooks/hook.rb', line 31

def __phases__
  @__phases__
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



65
66
67
# File 'lib/githooks/hook.rb', line 65

def args
  @args
end

#limitersObject (readonly)

Returns the value of attribute limiters.



64
65
66
# File 'lib/githooks/hook.rb', line 64

def limiters
  @limiters
end

#phaseObject (readonly)

Returns the value of attribute phase.



64
65
66
# File 'lib/githooks/hook.rb', line 64

def phase
  @phase
end

#repositoryObject (readonly)

Returns the value of attribute repository.



64
65
66
# File 'lib/githooks/hook.rb', line 64

def repository
  @repository
end

#repository_pathObject

Returns the value of attribute repository_path.



64
65
66
# File 'lib/githooks/hook.rb', line 64

def repository_path
  @repository_path
end

#sectionsObject (readonly)

Returns the value of attribute sections.



64
65
66
# File 'lib/githooks/hook.rb', line 64

def sections
  @sections
end

#stagedObject

Returns the value of attribute staged.



65
66
67
# File 'lib/githooks/hook.rb', line 65

def staged
  @staged
end

#trackedObject

Returns the value of attribute tracked.



65
66
67
# File 'lib/githooks/hook.rb', line 65

def tracked
  @tracked
end

#untrackedObject

Returns the value of attribute untracked.



65
66
67
# File 'lib/githooks/hook.rb', line 65

def untracked
  @untracked
end

Class Method Details

.instance(phase = 'pre-commit') ⇒ Object Also known as: []

rubocop:disable AbcSize



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

.method_missing(method, *args, &block) ⇒ Object



53
54
55
56
# 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

.register(phase, &block) ⇒ Object



58
59
60
61
# 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

Instance Method Details

#[](name) ⇒ Object



79
80
81
# File 'lib/githooks/hook.rb', line 79

def [](name)
  @sections[name]
end

#command(name, options = {}) ⇒ Object



146
147
148
# File 'lib/githooks/hook.rb', line 146

def command(name, options = {})
  setup_command name, options
end

#commands(*names) ⇒ Object



150
151
152
153
# 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) ⇒ Object



127
128
129
# File 'lib/githooks/hook.rb', line 127

def config_file(*path_components)
  config_path.join(*path_components)
end

#config_pathObject

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



123
124
125
# File 'lib/githooks/hook.rb', line 123

def config_path
  GitHooks.hooks_root.join('configs')
end

#find_command(name) ⇒ Object



110
111
112
# File 'lib/githooks/hook.rb', line 110

def find_command(name)
  @commands.find { |command| command.name == name.to_s }
end

#lib_file(*path_components) ⇒ Object



135
136
137
# File 'lib/githooks/hook.rb', line 135

def lib_file(*path_components)
  lib_path.join(*path_components)
end

#lib_pathObject



131
132
133
# File 'lib/githooks/hook.rb', line 131

def lib_path
  GitHooks.hooks_root.join('lib')
end

#limit(type) ⇒ Object



139
140
141
142
143
144
# File 'lib/githooks/hook.rb', line 139

def limit(type)
  unless @limiters.include? type
    @limiters[type] ||= Repository::Limiter.new(type)
  end
  @limiters[type]
end

#manifestObject



87
88
89
# File 'lib/githooks/hook.rb', line 87

def manifest
  @manifest ||= Manifest.new(self)
end

#runObject



91
92
93
94
# 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) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
# 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