fiveruns / brigit

Git utilities for multiple repositories & submodules

This URL has Read+Write access

brigit / lib / brigit / listable.rb
100644 43 lines (32 sloc) 0.821 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module Brigit
  
  module Listable
    
    def self.included(base)
      base.extend(ClassMethods)
    end
    
    module ClassMethods
      
      def [](name)
        matching = list.select { |klass| klass.name =~ /^#{Regexp.quote name}/io }
        return false unless matching.any?
        if matching.size > 1
          HighLine.say %|<%= color "Too many matches for `#{name}'", :red %>|
return false
end
matching.first
end
 
def inherited(klass)
list << klass
end
 
def list
@list ||= []
end
def listing
list.map do |l|
          block_given? ? yield(l.name) : l.name
        end.join(', ')
      end
 
      def name
        to_s.sub(/^(?:.+::)?(.+?)[A-Z][a-z]+$/, '\1').downcase
      end
      
    end
    
  end
  
end