Class: Kamaze::Project::Tools::Git::Status::File
- Defined in:
- lib/kamaze/project/tools/git/status/file.rb
Overview
Status file
Describe a file as seen in status, file is described by path and flags, and is immutable by design.
Instance Attribute Summary collapse
- #base_dir ⇒ Pathname readonly
- #flags ⇒ String readonly
- #path ⇒ Pathname readonly
Instance Method Summary collapse
- #==(other) ⇒ Boolean
-
#absolute_path ⇒ Pathname
Get absolute path.
-
#comparable_to?(other) ⇒ Boolean
Denote instance is comparable with another object.
-
#deleted? ⇒ Boolean
Denote deleted.
-
#ignored? ⇒ Boolean
Denote ignored.
-
#index? ⇒ Boolean
Denote index.
-
#index_deleted? ⇒ Boolean
Denote deleted in index.
-
#index_modified? ⇒ Boolean
Denote modified in index.
-
#index_new? ⇒ Boolean
Denote new in index.
-
#initialize(path, flags, base_dir = Dir.pwd) ⇒ File
constructor
A new instance of File.
-
#modified? ⇒ Boolean
Denote modified.
-
#new? ⇒ Boolean
Denote new.
-
#status ⇒ String
Get a status string, composed of two chars.
- #to_s ⇒ String
-
#untracked? ⇒ Boolean
Denote untracked.
-
#worktree? ⇒ Boolean
Denote worktree.
-
#worktree_deleted? ⇒ Boolean
Denote deleted in worktree.
-
#worktree_modified? ⇒ Boolean
Denote modified in worktree.
-
#worktree_new? ⇒ Boolean
Denote new in worktree.
Constructor Details
#initialize(path, flags, base_dir = Dir.pwd) ⇒ File
Returns a new instance of File.
31 32 33 34 35 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 31 def initialize(path, flags, base_dir = Dir.pwd) @base_dir = ::Pathname.new(base_dir).freeze @path = ::Pathname.new(path).freeze @flags = flags.freeze end |
Instance Attribute Details
#base_dir ⇒ Pathname (readonly)
20 21 22 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 20 def base_dir @base_dir end |
#flags ⇒ String (readonly)
26 27 28 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 26 def flags @flags end |
#path ⇒ Pathname (readonly)
23 24 25 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 23 def path @path end |
Instance Method Details
#==(other) ⇒ Boolean
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 63 def ==(other) return false unless comparable_with?(other) [[self.path.to_s, other.path.to_s], [self.base_dir.to_s, other.base_dir.to_s], [self.flags.sort, other.flags.sort]].each do |c| return false unless c[0] == c[1] end true end |
#absolute_path ⇒ Pathname
Get absolute path
87 88 89 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 87 def absolute_path base_dir.join(path) end |
#comparable_to?(other) ⇒ Boolean
Denote instance is comparable with another object
79 80 81 82 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 79 def comparable_to?(other) [:flags, :path, :base_dir] .map { |m| other.respond_to?(m) }.uniq == [true] end |
#deleted? ⇒ Boolean
Denote deleted
129 130 131 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 129 def deleted? flags.values.include?(:deleted) end |
#ignored? ⇒ Boolean
Denote ignored
94 95 96 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 94 def ignored? flags.keys.include?(:ignored) end |
#index? ⇒ Boolean
Denote index
108 109 110 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 108 def index? flags.keys.include?(:index) end |
#index_deleted? ⇒ Boolean
Denote deleted in index
157 158 159 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 157 def index_deleted? index? and deleted? end |
#index_modified? ⇒ Boolean
Denote modified in index
150 151 152 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 150 def index_modified? index? and modified? end |
#index_new? ⇒ Boolean
Denote new in index
143 144 145 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 143 def index_new? index? and new? end |
#modified? ⇒ Boolean
Denote modified
122 123 124 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 122 def modified? flags.values.include?(:modified) end |
#new? ⇒ Boolean
Denote new
115 116 117 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 115 def new? flags.values.include?(:new) end |
#status ⇒ String
Get a status string, composed of two chars
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 46 def status return '??' if untracked? states = [' ', ' '] mapping = { new: 'A', modified: 'M', deleted: 'D' } { index: 0, worktree: 1 }.each do |from, index| next unless self.public_send("#{from}?") mapping.each do |flag, s| states[index] = s if self.public_send("#{from}_#{flag}?") end end states.join end |
#to_s ⇒ String
38 39 40 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 38 def to_s path.to_s end |
#untracked? ⇒ Boolean
Denote untracked
136 137 138 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 136 def untracked? worktree? and new? and !index? end |
#worktree? ⇒ Boolean
Denote worktree
101 102 103 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 101 def worktree? flags.keys.include?(:worktree) end |
#worktree_deleted? ⇒ Boolean
Denote deleted in worktree
178 179 180 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 178 def worktree_deleted? worktree? and deleted? end |
#worktree_modified? ⇒ Boolean
Denote modified in worktree
171 172 173 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 171 def worktree_modified? worktree? and modified? end |
#worktree_new? ⇒ Boolean
Denote new in worktree
164 165 166 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 164 def worktree_new? worktree? and new? end |