Class: Mj::Git::Branches

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mj/git/branches.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(branches) ⇒ Branches

Returns a new instance of Branches.



40
41
42
# File 'lib/mj/git/branches.rb', line 40

def initialize(branches)
  @branches = branches
end

Class Method Details

.from_branch_names(branch_names) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mj/git/branches.rb', line 13

def self.from_branch_names(branch_names)
  branches = branch_names.map do |branch|
    branch = branch.sub("*", "").strip

    if branch.start_with?("remotes/")
      next RemoteBranch.new(branch)
    end

    next LocalBranch.new(branch)
  end

  new(branches)
end

Instance Method Details

#each(&block) ⇒ Object



44
45
46
# File 'lib/mj/git/branches.rb', line 44

def each(&block)
  @branches.each(&block)
end

#lengthObject



52
53
54
# File 'lib/mj/git/branches.rb', line 52

def length
  @branches.length
end

#matching(pattern) ⇒ Object



56
57
58
# File 'lib/mj/git/branches.rb', line 56

def matching(pattern)
  self.class.new(@branches.select { |branch| branch.name.match?(pattern) })
end

#sort_by(&block) ⇒ Object



48
49
50
# File 'lib/mj/git/branches.rb', line 48

def sort_by(&block)
  self.class.new(@branches.sort_by(&block))
end

#to_localObject



27
28
29
# File 'lib/mj/git/branches.rb', line 27

def to_local
  self.class.new(map(&:to_local))
end

#uniqObject



31
32
33
34
35
36
37
38
# File 'lib/mj/git/branches.rb', line 31

def uniq
  branches = {}
  each do |branch|
    branches[branch.name] ||= branch
  end

  self.class.new(branches.values)
end