Class: Kamaze::Project::Tools::Git::Status::File

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(path, flags, base_dir = Dir.pwd) ⇒ File

Returns a new instance of File.

Parameters:

  • path (Pathname|String)
  • flags (Hash)
  • base_dir (Pathname|String) (defaults to: Dir.pwd)


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_dirPathname (readonly)

Returns:

  • (Pathname)


20
21
22
# File 'lib/kamaze/project/tools/git/status/file.rb', line 20

def base_dir
  @base_dir
end

#flagsString (readonly)

Returns:

  • (String)


26
27
28
# File 'lib/kamaze/project/tools/git/status/file.rb', line 26

def flags
  @flags
end

#pathPathname (readonly)

Returns:

  • (Pathname)


23
24
25
# File 'lib/kamaze/project/tools/git/status/file.rb', line 23

def path
  @path
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (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_pathPathname

Get absolute path

Returns:

  • (Pathname)


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

Parameters:

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


114
115
116
# File 'lib/kamaze/project/tools/git/status/file.rb', line 114

def new?
  flags.values.include?(:new)
end

#statusString

Get a status string, composed of two chars

Returns:

  • (String)

See Also:



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_sString

Returns:

  • (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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


163
164
165
# File 'lib/kamaze/project/tools/git/status/file.rb', line 163

def worktree_new?
  worktree? and new?
end