Skip to content

Commit

Permalink
introduce BranchCollection class (#78)
Browse files Browse the repository at this point in the history
This will encapsulate more logic for finding branches
  • Loading branch information
mockdeep authored Jun 13, 2024
1 parent 422ecfe commit 8cebce6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/baes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ class Baes::Error < StandardError; end

require_relative "baes/actions"
require_relative "baes/branch"
require_relative "baes/branch_collection"
require_relative "baes/git"
require_relative "baes/version"
6 changes: 4 additions & 2 deletions lib/baes/actions/build_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ def find_root_branch(branches)
end

def generate_branches
git.branch_names.map do |branch_name|
Baes::Branch.new(branch_name)
branches = Baes::BranchCollection.new
git.branch_names.each do |branch_name|
branches << Baes::Branch.new(branch_name)
end
branches
end

def prune(branch)
Expand Down
40 changes: 40 additions & 0 deletions lib/baes/branch_collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

# class to encapsulate a collection of branches
class Baes::BranchCollection
attr_accessor :branches

def initialize
self.branches = []
end

# add a branch
def <<(branch)
branches << branch
end

# find a branch
def find(&block)
branches.find(&block)
end

# iterate over each branch with object
def each_with_object(obj, &block)
branches.each_with_object(obj, &block)
end

# iterate over each branch
def each(&block)
branches.each(&block)
end

# return the first branch
def first
branches.first
end

# return a branch by index
def [](index)
branches[index]
end
end

0 comments on commit 8cebce6

Please sign in to comment.