Class: RightGit::Git::BranchCollection
- Inherits:
-
Object
- Object
- RightGit::Git::BranchCollection
- Defined in:
- lib/right_git/git/branch_collection.rb
Overview
A collection of Git branches. Acts a bit like an Array, allowing it to be mapped, sorted and compared as such.
Instance Method Summary collapse
-
#[](argument) ⇒ Object
Accessor that acts like either a Hash or Array accessor.
-
#initialize(repo, *args) ⇒ BranchCollection
constructor
A new instance of BranchCollection.
-
#local ⇒ BranchCollection
Filters on local branches.
-
#merged(revision) ⇒ BranchCollection
Queries and filters on branches reachable from the given revision, if any.
- #method_missing(meth, *args, &block) ⇒ Object
-
#remote ⇒ BranchCollection
Filters on remote branches.
- #to_s ⇒ Object (also: #inspect)
Constructor Details
#initialize(repo, *args) ⇒ BranchCollection
Returns a new instance of BranchCollection.
33 34 35 36 |
# File 'lib/right_git/git/branch_collection.rb', line 33 def initialize(repo, *args) @repo = repo @branches = args end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/right_git/git/branch_collection.rb', line 96 def method_missing(meth, *args, &block) result = @branches.__send__(meth, *args, &block) if result.is_a?(::Array) BranchCollection.new(@repo, *result) else result end end |
Instance Method Details
#[](argument) ⇒ Object
Accessor that acts like either a Hash or Array accessor
86 87 88 89 90 91 92 93 94 |
# File 'lib/right_git/git/branch_collection.rb', line 86 def [](argument) case argument when String target = Branch.new(@repo, argument) @branches.detect { |b| b == target } else @branches.__send__(:[], argument) end end |
#local ⇒ BranchCollection
Filters on local branches.
46 47 48 49 50 51 52 |
# File 'lib/right_git/git/branch_collection.rb', line 46 def local local = BranchCollection.new(@repo) @branches.each do |branch| local << branch unless branch.remote? end local end |
#merged(revision) ⇒ BranchCollection
Queries and filters on branches reachable from the given revision, if any.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/right_git/git/branch_collection.rb', line 70 def merged(revision) git_args = ['branch', '-r', '--merged', revision] all_merged = @repo.git_output(git_args).lines.map do |line| Branch.new(@repo, line) end merged = BranchCollection.new(@repo) @branches.each do |candidate| # For some reason Set#include? does not play nice with our overridden comparison operators # for branches, so we need to do this the hard way :( merged << candidate if all_merged.detect { |b| candidate == b } end merged end |
#remote ⇒ BranchCollection
Filters on remote branches.
57 58 59 60 61 62 63 |
# File 'lib/right_git/git/branch_collection.rb', line 57 def remote remote = BranchCollection.new(@repo) @branches.each do |branch| remote << branch if branch.remote? end remote end |
#to_s ⇒ Object Also known as: inspect
38 39 40 |
# File 'lib/right_git/git/branch_collection.rb', line 38 def to_s "#{self.class.name}: #{@branches.inspect}" end |