Sha256: ec20cb5ef4fb59c1dfd68009a05041eafc1de5d9017bf55085dfa391b28f778b

Contents?: true

Size: 1.67 KB

Versions: 5

Compression:

Stored size: 1.67 KB

Contents

# Enables +gravatarify+ support in any plain old ruby object, ActiveRecord, DataMapper or wherever you like :)
#
# Provides the {ClassMethods#gravatarify} method to handle the creation of
# a +gravatar_url+ method for a ruby object.
#
# An ActiveRecord example:
# 
#    class User < ActiveRecord::Base
#      gravatarify
#    end
#    @user.gravatar_url # that's it!
#
# A DataMapper example:
#
#    class User
#      include DataMapper::Resource
#      property ...
#      property :author_email, String
#    end
#    @user.gravatar_url :author_email # that's it, using the specified field!
#
# And finally, using a plain old ruby object:
#
#    class SimpleUser
#      include Gravatarify::ObjectSupport
#      attr_accessor :email
#      gravatarify
#    end
#    @user.gravatar_url # that's it!!!
#
# If more fine grained controller is required, feel free to use {Gravatarify::Base#build_gravatar_url}
# directly.
module Gravatarify::ObjectSupport
  include Gravatarify::Base
  
  def self.included(base) #:nodoc:
    base.send :extend, ClassMethods
  end
  
  module ClassMethods    
    def gravatarify(*args)
      options = args.last.is_a?(Hash) ? args.pop : {}
      source = args.shift
      define_method(:gravatar_url) do |*params|
        source = :email if !source and respond_to?(:email)
        source = :mail if !source and respond_to?(:mail)
        build_gravatar_url send(source || :email), options.merge(params.first || {})
      end
      # has more
      args.each do |src|
        method = "#{src}_gravatar_url".sub(/_e?mail/, '')
        define_method(method) do |*params|
          build_gravatar_url send(src), options.merge(params.first || {})
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gravatarify-1.2.0 lib/gravatarify/object_support.rb
gravatarify-1.1.0 lib/gravatarify/object_support.rb
gravatarify-1.0.0 lib/gravatarify/object_support.rb
gravatarify-0.7.0 lib/gravatarify/object_support.rb
gravatarify-0.6.0 lib/gravatarify/object_support.rb