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.



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

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.



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

def email
  @email
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.allObject



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

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

.email(authors) ⇒ Object



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

def self.email(authors)
  if authors.length == 1
    authors.first.email
  else
    Config.default_email
  end
end

.exists?(author) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/git-pair/author.rb', line 29

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

.find(abbr) ⇒ Object



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

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

.find_all(abbrs) ⇒ Object



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

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)


33
34
35
# File 'lib/git-pair/author.rb', line 33

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

Instance Method Details

#<=>(other) ⇒ Object



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

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

#initialsObject



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

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

#match?(abbr) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/git-pair/author.rb', line 57

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