Class: GitHooks::Repository::File
- Inherits:
-
DiffIndexEntryDelegateClass
- Object
- GitHooks::Repository::File
- Defined in:
- lib/githooks/repository/file.rb
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#attribute_value(attribute) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity.
- #contains?(string_or_regexp) ⇒ Boolean
- #contents ⇒ Object
- #eql?(other) ⇒ Boolean
- #fd ⇒ Object
- #full_path ⇒ Object
- #grep(regexp) ⇒ Object
- #hash ⇒ Object
-
#initialize(repo, entry) ⇒ File
constructor
A new instance of File.
- #inspect ⇒ Object
- #lines(strip_newlines = false) ⇒ Object
- #match(type, selector) ⇒ Object
-
#match_callable(type, selector) ⇒ Object
rubocop:disable ElseAlignment, IndentationWidth.
-
#match_type(type, selector) ⇒ Object
rubocop:enable ElseAlignment, IndentationWidth.
- #name ⇒ Object
- #path ⇒ Object
- #realpath ⇒ Object
Constructor Details
#initialize(repo, entry) ⇒ File
Returns a new instance of File.
37 38 39 40 |
# File 'lib/githooks/repository/file.rb', line 37 def initialize(repo, entry) @repo = repo @file = entry end |
Instance Method Details
#<=>(other) ⇒ Object
158 159 160 |
# File 'lib/githooks/repository/file.rb', line 158 def <=>(other) path.to_s <=> other.path.to_s end |
#==(other) ⇒ Object
162 163 164 |
# File 'lib/githooks/repository/file.rb', line 162 def ==(other) path.to_s == other.path.to_s end |
#attribute_value(attribute) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity
61 62 63 64 65 66 67 68 69 70 71 72 |
# 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) ⇒ Boolean
124 125 126 127 128 129 130 |
# 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 ⇒ Object
140 141 142 143 |
# File 'lib/githooks/repository/file.rb', line 140 def contents return unless fd fd.read end |
#eql?(other) ⇒ Boolean
150 151 152 |
# File 'lib/githooks/repository/file.rb', line 150 def eql?(other) path.to_s == other.path.to_s end |
#fd ⇒ Object
110 111 112 113 114 115 |
# 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 ⇒ Object
53 54 55 |
# File 'lib/githooks/repository/file.rb', line 53 def full_path repo.path.join(path) end |
#grep(regexp) ⇒ Object
132 133 134 135 136 137 138 |
# 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 ⇒ Object
154 155 156 |
# File 'lib/githooks/repository/file.rb', line 154 def hash path.to_s.hash end |
#inspect ⇒ Object
42 43 44 45 46 47 |
# 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) ⇒ Object
145 146 147 148 |
# 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) ⇒ Object
74 75 76 77 78 79 80 |
# 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) ⇒ Object
rubocop:disable ElseAlignment, IndentationWidth
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# 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) ⇒ Object
rubocop:enable ElseAlignment, IndentationWidth
98 99 100 101 102 103 104 105 106 107 108 |
# 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 ⇒ Object
57 58 59 |
# File 'lib/githooks/repository/file.rb', line 57 def name path.basename.to_s end |
#path ⇒ Object
49 50 51 |
# File 'lib/githooks/repository/file.rb', line 49 def path to.path || from.path end |
#realpath ⇒ Object
117 118 119 120 121 122 |
# File 'lib/githooks/repository/file.rb', line 117 def realpath case type when :deleted, :deletion then path else path.realpath end end |