Sha256: 5f14f0d1a7f6c258a0ada9f40e16843f160c93d12eace52b0689a08465a63b35

Contents?: true

Size: 992 Bytes

Versions: 7

Compression:

Stored size: 992 Bytes

Contents

module Toadie
  class Author
    @@authors = []

    attr_accessor :name, :nicknames, :emails

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

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

    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

    def self.all
      @@authors
    end

    def self.destroy_all
      @@authors.clear
    end

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

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

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
toadie-0.0.8 lib/toadie/author.rb
toadie-0.0.7 lib/toadie/author.rb
toadie-0.0.6 lib/toadie/author.rb
toadie-0.0.5 lib/toadie/author.rb
toadie-0.0.4 lib/toadie/author.rb
toadie-0.0.3 lib/toadie/author.rb
toadie-0.0.2 lib/toadie/author.rb