Class: Hiiro::Git::Branches
- Inherits:
-
Object
- Object
- Hiiro::Git::Branches
- Includes:
- Enumerable
- Defined in:
- lib/hiiro/git/branches.rb
Instance Attribute Summary collapse
-
#branches ⇒ Object
readonly
Returns the value of attribute branches.
Class Method Summary collapse
- .all(sort_by: nil, ignore_case: false) ⇒ Object
- .fetch(sort_by: nil, ignore_case: false, remote: false) ⇒ Object
- .from_names(names) ⇒ Object
- .local(sort_by: nil, ignore_case: false) ⇒ Object
- .remote(sort_by: nil, ignore_case: false) ⇒ Object
Instance Method Summary collapse
- #containing(substring) ⇒ Object
- #current ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #find_by_name(name) ⇒ Object
-
#initialize(branches) ⇒ Branches
constructor
A new instance of Branches.
- #matching(prefix) ⇒ Object
- #names ⇒ Object
- #size ⇒ Object (also: #count, #length)
- #to_a ⇒ Object
Constructor Details
#initialize(branches) ⇒ Branches
Returns a new instance of Branches.
36 37 38 |
# File 'lib/hiiro/git/branches.rb', line 36 def initialize(branches) @branches = branches end |
Instance Attribute Details
#branches ⇒ Object (readonly)
Returns the value of attribute branches.
34 35 36 |
# File 'lib/hiiro/git/branches.rb', line 34 def branches @branches end |
Class Method Details
.all(sort_by: nil, ignore_case: false) ⇒ Object
30 31 32 |
# File 'lib/hiiro/git/branches.rb', line 30 def self.all(sort_by: nil, ignore_case: false) fetch(sort_by: sort_by, ignore_case: ignore_case, remote: :all) end |
.fetch(sort_by: nil, ignore_case: false, remote: false) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/hiiro/git/branches.rb', line 6 def self.fetch(sort_by: nil, ignore_case: false, remote: false) args = ['git', 'branch', '--format=%(refname:short)'] args << '-i' if ignore_case args << '-r' if remote args << "-a" if remote == :all args << "--sort=#{sort_by}" if sort_by output = `#{args.shelljoin} 2>/dev/null` from_names(output.split("\n").map(&:strip)) end |
.from_names(names) ⇒ Object
17 18 19 20 |
# File 'lib/hiiro/git/branches.rb', line 17 def self.from_names(names) branches = names.map { |name| Branch.new(name: name) } new(branches) end |
.local(sort_by: nil, ignore_case: false) ⇒ Object
22 23 24 |
# File 'lib/hiiro/git/branches.rb', line 22 def self.local(sort_by: nil, ignore_case: false) fetch(sort_by: sort_by, ignore_case: ignore_case, remote: false) end |
.remote(sort_by: nil, ignore_case: false) ⇒ Object
26 27 28 |
# File 'lib/hiiro/git/branches.rb', line 26 def self.remote(sort_by: nil, ignore_case: false) fetch(sort_by: sort_by, ignore_case: ignore_case, remote: true) end |
Instance Method Details
#containing(substring) ⇒ Object
52 53 54 |
# File 'lib/hiiro/git/branches.rb', line 52 def containing(substring) self.class.new(branches.select { |b| b.name.include?(substring) }) end |
#current ⇒ Object
60 61 62 |
# File 'lib/hiiro/git/branches.rb', line 60 def current branches.find(&:current?) end |
#each(&block) ⇒ Object
40 41 42 |
# File 'lib/hiiro/git/branches.rb', line 40 def each(&block) branches.each(&block) end |
#empty? ⇒ Boolean
64 65 66 |
# File 'lib/hiiro/git/branches.rb', line 64 def empty? branches.empty? end |
#find_by_name(name) ⇒ Object
44 45 46 |
# File 'lib/hiiro/git/branches.rb', line 44 def find_by_name(name) branches.find { |b| b.name == name } end |
#matching(prefix) ⇒ Object
48 49 50 |
# File 'lib/hiiro/git/branches.rb', line 48 def matching(prefix) self.class.new(branches.select { |b| b.name.start_with?(prefix) }) end |
#names ⇒ Object
56 57 58 |
# File 'lib/hiiro/git/branches.rb', line 56 def names branches.map(&:name) end |
#size ⇒ Object Also known as: count, length
68 69 70 |
# File 'lib/hiiro/git/branches.rb', line 68 def size branches.size end |
#to_a ⇒ Object
74 75 76 |
# File 'lib/hiiro/git/branches.rb', line 74 def to_a branches.dup end |