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)


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_pathPathname

Get absolute path

Returns:

  • (Pathname)


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

Parameters:

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

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
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_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)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

def worktree_new?
  worktree? and new?
end