Class: Toadie::Author

Inherits:
Object
  • Object
show all
Defined in:
lib/toadie/author.rb,
lib/toadie/errors.rb

Defined Under Namespace

Classes: MissingEmail

Constant Summary collapse

@@authors =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Author

Returns a new instance of Author.



33
34
35
# File 'lib/toadie/author.rb', line 33

def initialize(params = {})
  params.each { |key, value| __send__("#{key}=", value) }
end

Instance Attribute Details

#emailsObject

Returns the value of attribute emails.



5
6
7
# File 'lib/toadie/author.rb', line 5

def emails
  @emails
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/toadie/author.rb', line 5

def name
  @name
end

#nicknamesObject

Returns the value of attribute nicknames.



5
6
7
# File 'lib/toadie/author.rb', line 5

def nicknames
  @nicknames
end

Class Method Details

.allObject



25
26
27
# File 'lib/toadie/author.rb', line 25

def self.all
  @@authors
end

.create(opts = {}) ⇒ Object

Raises:



15
16
17
18
19
20
21
22
23
# File 'lib/toadie/author.rb', line 15

def self.create(opts = {})
  opts.keys.each do |key|
    key.is_a?(String) and opts[key.to_sym] = opts.delete(key)
  end
  raise MissingEmail if opts[:emails].nil? || opts[:emails].empty?
  opts[:nicknames] ||= []
  @@authors << author = Author.new(opts)
  author
end

.destroy_allObject



29
30
31
# File 'lib/toadie/author.rb', line 29

def self.destroy_all
  @@authors.clear
end

.find_by_email(email) ⇒ Object



11
12
13
# File 'lib/toadie/author.rb', line 11

def self.find_by_email(email)
  @@authors.find { |author| author.emails.include?(email) }
end

.find_or_create(email, opts = {}) ⇒ Object



7
8
9
# File 'lib/toadie/author.rb', line 7

def self.find_or_create(email, opts = {})
  find_by_email(email) || create(opts.merge(emails: [email]))
end

Instance Method Details

#identifierObject



37
38
39
# File 'lib/toadie/author.rb', line 37

def identifier
  @identifier ||= emails.first.downcase.gsub(/\W/, '')
end

#to_sObject



41
42
43
# File 'lib/toadie/author.rb', line 41

def to_s
  name || nicknames.first || emails.first
end