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
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 62 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
86 87 88 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 86 def absolute_path base_dir.join(path) end |
#comparable_to?(other) ⇒ Boolean
Denote instance is comparable with another object
78 79 80 81 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 78 def comparable_to?(other) [:flags, :path, :base_dir] .map { |m| other.respond_to?(m) }.uniq == [true] end |
#deleted? ⇒ Boolean
Denote deleted
128 129 130 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 128 def deleted? flags.values.include?(:deleted) end |
#ignored? ⇒ Boolean
Denote ignored
93 94 95 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 93 def ignored? flags.keys.include?(:ignored) end |
#index? ⇒ Boolean
Denote index
107 108 109 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 107 def index? flags.keys.include?(:index) end |
#index_deleted? ⇒ Boolean
Denote deleted in index
156 157 158 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 156 def index_deleted? index? and deleted? end |
#index_modified? ⇒ Boolean
Denote modified in index
149 150 151 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 149 def index_modified? index? and modified? end |
#index_new? ⇒ Boolean
Denote new in index
142 143 144 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 142 def index_new? index? and new? end |
#modified? ⇒ Boolean
Denote modified
121 122 123 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 121 def modified? flags.values.include?(:modified) end |
#new? ⇒ Boolean
Denote new
114 115 116 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 114 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 |
# 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
135 136 137 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 135 def untracked? worktree? and new? and !index? end |
#worktree? ⇒ Boolean
Denote worktree
100 101 102 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 100 def worktree? flags.keys.include?(:worktree) end |
#worktree_deleted? ⇒ Boolean
Denote deleted in worktree
177 178 179 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 177 def worktree_deleted? worktree? and deleted? end |
#worktree_modified? ⇒ Boolean
Denote modified in worktree
170 171 172 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 170 def worktree_modified? worktree? and modified? end |
#worktree_new? ⇒ Boolean
Denote new in worktree
163 164 165 |
# File 'lib/kamaze/project/tools/git/status/file.rb', line 163 def worktree_new? worktree? and new? end |