Class: GitPair::Author

Inherits:
Object
  • Object
show all
Defined in:
lib/git-pair/author.rb

Defined Under Namespace

Classes: InvalidAuthorString

Constant Summary collapse

ValidAuthorStringRegex =
/^\s*([^<]+)<([^>]+)>\s*$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Author

Returns a new instance of Author.



41
42
43
44
45
46
47
48
49
# File 'lib/git-pair/author.rb', line 41

def initialize(string)
  unless Author.valid_string?(string)
    raise(InvalidAuthorString, "\"#{string}\" is not a valid name and email")
  end

  string =~ ValidAuthorStringRegex
  @name = $1.to_s.strip
  @email = $2.to_s.strip
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.



39
40
41
# File 'lib/git-pair/author.rb', line 39

def email
  @email
end

#nameObject (readonly)

Returns the value of attribute name.



39
40
41
# File 'lib/git-pair/author.rb', line 39

def name
  @name
end

Class Method Details

.allObject



8
9
10
# File 'lib/git-pair/author.rb', line 8

def self.all
  Config.all_author_strings.map { |string| new(string) }
end

.email(authors) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/git-pair/author.rb', line 22

def self.email(authors)
  if authors.length == 1
    authors.first.email
  else
    initials_string = '+' + authors.map { |a| a.initials }.join('+')
    Config.default_email.sub("@", "#{initials_string}@")
  end
end

.exists?(author) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/git-pair/author.rb', line 31

def self.exists?(author)
  self.all.find { |a| a.name == author.name }
end

.find(abbr) ⇒ Object



17
18
19
20
# File 'lib/git-pair/author.rb', line 17

def self.find(abbr)
  all.find { |author| author.match?(abbr) } || 
    raise(NoMatchingAuthorsError, "no authors matched #{abbr}")
end

.find_all(abbrs) ⇒ Object



12
13
14
15
# File 'lib/git-pair/author.rb', line 12

def self.find_all(abbrs)
  raise MissingConfigurationError, "Please add some authors first" if all.empty?
  abbrs.map { |abbr| self.find(abbr) }
end

.valid_string?(author_string) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/git-pair/author.rb', line 35

def self.valid_string?(author_string)
  author_string =~ ValidAuthorStringRegex
end

Instance Method Details

#<=>(other) ⇒ Object



51
52
53
# File 'lib/git-pair/author.rb', line 51

def <=>(other)
  name.split.last <=> other.name.split.last
end

#initialsObject



55
56
57
# File 'lib/git-pair/author.rb', line 55

def initials
  name.split.map { |word| word[0].chr }.join.downcase
end

#match?(abbr) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/git-pair/author.rb', line 59

def match?(abbr)
  abbr.downcase == initials
end