class GitHooks::Repository::File

Attributes

__getobj__[R]
file[R]
repo[R]

Public Class Methods

new(repo, entry) click to toggle source
# File lib/githooks/repository/file.rb, line 37
def initialize(repo, entry)
  @repo = repo
  @file = entry
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/githooks/repository/file.rb, line 158
def <=>(other)
  path.to_s <=> other.path.to_s
end
==(other) click to toggle source
# File lib/githooks/repository/file.rb, line 162
def ==(other)
  path.to_s == other.path.to_s
end
attribute_value(attribute) click to toggle source
# File lib/githooks/repository/file.rb, line 61
def attribute_value(attribute) # rubocop:disable Metrics/CyclomaticComplexity
  case attribute
    when :name then name
    when :path then path.to_s
    when :type then type
    when :mode then to.mode
    when :sha then to.sha
    when :score then score
    else fail ArgumentError,
              "Invalid attribute type '#{attribute}' - expected: :name, :path, :type, :mode, :sha, or :score"
  end
end
contains?(string_or_regexp) click to toggle source
# File lib/githooks/repository/file.rb, line 124
def contains?(string_or_regexp)
  if string_or_regexp.is_a?(Regexp)
    contents =~ string_or_regexp
  else
    contents.include? string_or_regexp
  end
end
contents() click to toggle source
# File lib/githooks/repository/file.rb, line 140
def contents
  return unless fd
  fd.read
end
eql?(other) click to toggle source
# File lib/githooks/repository/file.rb, line 150
def eql?(other)
  path.to_s == other.path.to_s
end
fd() click to toggle source
# File lib/githooks/repository/file.rb, line 110
def fd
  case type
    when :deleted, :deletion then nil
    else full_path.open
  end
end
full_path() click to toggle source
# File lib/githooks/repository/file.rb, line 53
def full_path
  repo.path.join(path)
end
grep(regexp) click to toggle source
# File lib/githooks/repository/file.rb, line 132
def grep(regexp)
  lines(true).select_with_index { |line|
    line =~ regexp
  }.collect { |num, line|
    [num + 1, line] # line numbers start from 1, not 0
  }
end
hash() click to toggle source
# File lib/githooks/repository/file.rb, line 154
def hash
  path.to_s.hash
end
inspect() click to toggle source
# File lib/githooks/repository/file.rb, line 42
def inspect
  attributes = [:name, :path, :type, :mode, :sha, :score].collect do |name|
    "#{name}=#{attribute_value(name).inspect}"
  end
  "#<#{self.class.name} #{attributes.join(' ')} >"
end
lines(strip_newlines = false) click to toggle source
# File lib/githooks/repository/file.rb, line 145
def lines(strip_newlines = false)
  return [] unless fd
  strip_newlines ? fd.readlines.collect(&:chomp!) : fd.readlines
end
match(type, selector) click to toggle source
# File lib/githooks/repository/file.rb, line 74
def match(type, selector)
  if selector.respond_to? :call
    match_callable(type, selector)
  else
    match_type(type, selector)
  end
end
match_callable(type, selector) click to toggle source

rubocop:disable ElseAlignment, IndentationWidth

# File lib/githooks/repository/file.rb, line 83
def match_callable(type, selector)
  value = attribute_value(type)

  case (arity = selector.arity)
    when 0 then fail ArgumentError, 'limiter recieves no parameters'
    when -4..-1, 3 then selector.call(value, type, self)
    when 1 then selector.call(value)
    when 2 then selector.call(value, type)
  else
    fail ArgumentError, 'expected limiter to receive at most 3 parameters, ' \
                         "but it receives #{arity}"
  end
end
match_type(type, selector) click to toggle source

rubocop:enable ElseAlignment, IndentationWidth

# File lib/githooks/repository/file.rb, line 98
def match_type(type, selector) # rubocop:disable AbcSize,CyclomaticComplexity
  value = attribute_value(type)
  case type
    when :name  then selector.is_a?(Regexp) ? value =~ selector : value == selector
    when :path  then selector.is_a?(Regexp) ? value =~ selector : value == selector
    when :type  then [*selector].include?(:any) ? true : [*selector].include?(value)
    when :mode  then selector & value == selector
    when :sha   then selector == value
    when :score then selector == value
  end
end
name() click to toggle source
# File lib/githooks/repository/file.rb, line 57
def name
  path.basename.to_s
end
path() click to toggle source
# File lib/githooks/repository/file.rb, line 49
def path
  to.path || from.path
end
realpath() click to toggle source
# File lib/githooks/repository/file.rb, line 117
def realpath
  case type
    when :deleted, :deletion then path
    else path.realpath
  end
end