Class: Gitty::Hook

Inherits:
Object show all
Includes:
FileUtils, Helpers
Defined in:
lib/gitty/hook.rb

Defined Under Namespace

Modules: AvailableHookStrategy, InstalledHookStrategy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#cmd, #existing_directory!, #file_with_existing_directory!, #search_for_file_in_paths, #with_env_var

Constructor Details

#initialize(options = {}) ⇒ Hook

Returns a new instance of Hook.



6
7
8
9
10
11
12
13
14
15
# File 'lib/gitty/hook.rb', line 6

def initialize(options = {})
  options.each do |k,v|
    send("#{k}=", v)
  end
  if installed?
    extend Gitty::Hook::InstalledHookStrategy
  else
    extend Gitty::Hook::AvailableHookStrategy
  end
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/gitty/hook.rb', line 4

def path
  @path
end

Class Method Details

.available_hooks_search_pathsObject



17
18
19
# File 'lib/gitty/hook.rb', line 17

def self.available_hooks_search_paths
  Gitty.asset_paths.map { |ap| File.join(ap, "hooks") }
end

.extract_meta_data(string_or_io) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gitty/hook.rb', line 60

def self.(string_or_io)
  io = string_or_io.respond_to?(:readline) ? string_or_io : StringIO.new(string_or_io)
  meta_yaml = ""
  begin
    while line = io.readline
      next unless line.match(/^# (description.+)/)
      meta_yaml = "#{$1}\n"
      break
    end

    while line = io.readline
      break unless line.match(/^#()$/) || line.match(/^# (.*?)$/)
      meta_yaml << "#{$1}\n"
    end
  rescue EOFError
  end
  meta_yaml.empty? ? nil : YAML.load(meta_yaml)
end

.filter_candidates(candidates, filters = {}) ⇒ Object



43
44
45
46
47
48
# File 'lib/gitty/hook.rb', line 43

def self.filter_candidates(candidates, filters = {})
  filters.each do |field, value|
    candidates = candidates.select { |c| c.send(field) == value }
  end
  candidates
end

.find(name, options = {}) ⇒ Object



50
51
52
# File 'lib/gitty/hook.rb', line 50

def self.find(name, options = {})
  find_all(options.merge(:name => name)).first
end

.find_all(filters = {}) ⇒ Object



37
38
39
40
41
# File 'lib/gitty/hook.rb', line 37

def self.find_all(filters = {})
  paths = installed_hooks_search_paths + available_hooks_search_paths
  candidates = find_all_in_paths(paths)
  filter_candidates(candidates, filters)
end

.find_all_in_paths(paths) ⇒ Object



54
55
56
57
58
# File 'lib/gitty/hook.rb', line 54

def self.find_all_in_paths(paths)
  paths.map { |hook_path| Dir.glob(File.join(hook_path, "*")) }.flatten.map do |path|
    Gitty::Hook.new(:path => path)
  end
end

.installed_helpers_path(which = :local) ⇒ Object



33
34
35
# File 'lib/gitty/hook.rb', line 33

def self.installed_helpers_path(which = :local)
  installed_path(which) + "helpers"
end

.installed_hooks_path(which = :local) ⇒ Object



29
30
31
# File 'lib/gitty/hook.rb', line 29

def self.installed_hooks_path(which = :local)
  installed_path(which) + "hooks"
end

.installed_hooks_search_pathsObject



21
22
23
# File 'lib/gitty/hook.rb', line 21

def self.installed_hooks_search_paths
  [installed_hooks_path(:local), installed_hooks_path(:shared)]
end

.installed_path(which = :local) ⇒ Object



25
26
27
# File 'lib/gitty/hook.rb', line 25

def self.installed_path(which = :local)
  Pathname.new(".git/hooks/#{which}")
end

Instance Method Details

#<=>(other) ⇒ Object



95
96
97
# File 'lib/gitty/hook.rb', line 95

def <=>(other)
  path <=> other.path
end

#install_kindObject



84
85
86
87
88
89
# File 'lib/gitty/hook.rb', line 84

def install_kind
  case
  when path.include?(self.class.installed_hooks_path(:local).to_s) then :local
  when path.include?(self.class.installed_hooks_path(:shared).to_s) then :shared
  end
end

#installed?Boolean Also known as: installed

Returns:

  • (Boolean)


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

def installed?
  install_kind ? true : false
end

#meta_dataObject



99
100
101
# File 'lib/gitty/hook.rb', line 99

def 
  @meta_data ||= self.class.(File.read(path))
end

#nameObject



91
92
93
# File 'lib/gitty/hook.rb', line 91

def name
  File.basename(path)
end