Class: Toadie::Author
- Inherits:
-
Object
- Object
- Toadie::Author
- Defined in:
- lib/toadie/author.rb,
lib/toadie/errors.rb
Defined Under Namespace
Classes: MissingEmail
Constant Summary collapse
[]
Instance Attribute Summary collapse
-
#emails ⇒ Object
Returns the value of attribute emails.
-
#name ⇒ Object
Returns the value of attribute name.
-
#nicknames ⇒ Object
Returns the value of attribute nicknames.
Class Method Summary collapse
- .all ⇒ Object
- .create(opts = {}) ⇒ Object
- .destroy_all ⇒ Object
- .find_by_email(email) ⇒ Object
- .find_or_create(email, opts = {}) ⇒ Object
Instance Method Summary collapse
- #identifier ⇒ Object
-
#initialize(params = {}) ⇒ Author
constructor
A new instance of Author.
- #to_s ⇒ Object
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
#emails ⇒ Object
Returns the value of attribute emails.
5 6 7 |
# File 'lib/toadie/author.rb', line 5 def emails @emails end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/toadie/author.rb', line 5 def name @name end |
#nicknames ⇒ Object
Returns the value of attribute nicknames.
5 6 7 |
# File 'lib/toadie/author.rb', line 5 def nicknames @nicknames end |
Class Method Details
.all ⇒ Object
25 26 27 |
# File 'lib/toadie/author.rb', line 25 def self.all @@authors end |
.create(opts = {}) ⇒ Object
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.new(opts) end |
.destroy_all ⇒ Object
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 { || .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
#identifier ⇒ Object
37 38 39 |
# File 'lib/toadie/author.rb', line 37 def identifier @identifier ||= emails.first.downcase.gsub(/\W/, '') end |
#to_s ⇒ Object
41 42 43 |
# File 'lib/toadie/author.rb', line 41 def to_s name || nicknames.first || emails.first end |