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.



55
56
57
58
59
60
61
62
63
# File 'lib/git-pair/author.rb', line 55

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

  string =~ ValidAuthorStringRegex
  self.name = $1.to_s.strip
  self.email = EmailAddress.new($2.to_s.strip)
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



53
54
55
# File 'lib/git-pair/author.rb', line 53

def email
  @email
end

#nameObject

Returns the value of attribute name.



53
54
55
# File 'lib/git-pair/author.rb', line 53

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

.domain_for(email_address) ⇒ Object



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

def self.domain_for(email_address)
  return pair_email.domain if pair_email.domain
  return email_address.domain
end

.email_address_for(authors) ⇒ Object



22
23
24
# File 'lib/git-pair/author.rb', line 22

def self.email_address_for(authors)
  "#{local_part_for(authors)}@#{domain_for(authors.first.email)}"
end

.exists?(author) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/git-pair/author.rb', line 41

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

.local_part_for(authors) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/git-pair/author.rb', line 26

def self.local_part_for(authors)
  return authors.first.email.local_part if authors.size == 1

  if pair_email.local_part && pair_email.local_part != ""
    authors.map(&:initials).unshift(pair_email.local_part).join("+")
  else
    authors.map { |author| author.email.local_part }.join("+")
  end
end

.pair_emailObject



49
50
51
# File 'lib/git-pair/author.rb', line 49

def self.pair_email
  EmailAddress.new(Config.pair_email)
end

.valid_string?(author_string) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/git-pair/author.rb', line 45

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

Instance Method Details

#<=>(other) ⇒ Object



65
66
67
# File 'lib/git-pair/author.rb', line 65

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

#initialsObject



69
70
71
# File 'lib/git-pair/author.rb', line 69

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

#match?(abbr) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/git-pair/author.rb', line 73

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