public
Rubygem
Description: Ruby helpers for avatar display
Clone URL: git://github.com/gcnovus/avatar.git
Search Repo:
first version: gravatar, file_column, source_chain, static_url, 
abstract_view, action_view
gcnovus (author)
Tue Mar 25 16:09:54 -0700 2008
commit  90fc3be83126439a5db9fbd0251026fe934aad3e
tree    50c64a277a5f4c44167365186f0cf62c4e01547a
parent  66e88e9b409043a4762d9265df8229f238aa1f68
...
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
0
@@ -1 +1,12 @@
0
+#ignore all .diff files
0
+/*.diff
0
+
0
+#ignore coverage reports
0
+/coverage
0
+
0
+#ignore logs
0
+/log/*.log
0
+
0
+#ignore packaged local gem:
0
+/pkg
...
 
 
 
 
...
1
2
3
4
0
@@ -1 +1,5 @@
0
+== 0.0.1 2008-03-24
0
+
0
+* 1 major enhancement:
0
+ * Initial release
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
0
@@ -1 +1,21 @@
0
+Copyright (c) 2008 Universal Presence, Inc.
0
+
0
+Permission is hereby granted, free of charge, to any person obtaining
0
+a copy of this software and associated documentation files (the
0
+"Software"), to deal in the Software without restriction, including
0
+without limitation the rights to use, copy, modify, merge, publish,
0
+distribute, sublicense, and/or sell copies of the Software, and to
0
+permit persons to whom the Software is furnished to do so, subject to
0
+the following conditions:
0
+
0
+The above copyright notice and this permission notice shall be
0
+included in all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
0
@@ -1 +1,74 @@
0
+.gitignore
0
+History.txt
0
+License.txt
0
+Manifest.txt
0
+README.txt
0
+Rakefile
0
+config/hoe.rb
0
+config/requirements.rb
0
+lib/avatar.rb
0
+lib/avatar/source.rb
0
+lib/avatar/source/abstract_source.rb
0
+lib/avatar/source/file_column_source.rb
0
+lib/avatar/source/gravatar_source.rb
0
+lib/avatar/source/nil_source.rb
0
+lib/avatar/source/source_chain.rb
0
+lib/avatar/source/static_url_source.rb
0
+lib/avatar/source/string_substitution_source.rb
0
+lib/avatar/version.rb
0
+lib/avatar/view.rb
0
+lib/avatar/view/abstract_view_support.rb
0
+lib/avatar/view/action_view_support.rb
0
+script/destroy
0
+script/generate
0
+script/txt2html
0
+setup.rb
0
+tasks/deployment.rake
0
+tasks/environment.rake
0
+tasks/testing.rake
0
+tasks/website.rake
0
+test/.gitignore
0
+test/lib/database.rb
0
+test/lib/database.yml
0
+test/lib/file_column/CHANGELOG
0
+test/lib/file_column/README
0
+test/lib/file_column/Rakefile
0
+test/lib/file_column/TODO
0
+test/lib/file_column/init.rb
0
+test/lib/file_column/lib/file_column.rb
0
+test/lib/file_column/lib/file_column_helper.rb
0
+test/lib/file_column/lib/file_compat.rb
0
+test/lib/file_column/lib/magick_file_column.rb
0
+test/lib/file_column/lib/rails_file_column.rb
0
+test/lib/file_column/lib/test_case.rb
0
+test/lib/file_column/lib/validations.rb
0
+test/lib/file_column/test/abstract_unit.rb
0
+test/lib/file_column/test/connection.rb
0
+test/lib/file_column/test/file_column_helper_test.rb
0
+test/lib/file_column/test/file_column_test.rb
0
+test/lib/file_column/test/fixtures/entry.rb
0
+test/lib/file_column/test/fixtures/invalid-image.jpg
0
+test/lib/file_column/test/fixtures/kerb.jpg
0
+test/lib/file_column/test/fixtures/mysql.sql
0
+test/lib/file_column/test/fixtures/schema.rb
0
+test/lib/file_column/test/fixtures/skanthak.png
0
+test/lib/file_column/test/magick_test.rb
0
+test/lib/file_column/test/magick_view_only_test.rb
0
+test/lib/schema.rb
0
+test/lib/user_suit.png
0
+test/test_abstract_view_support.rb
0
+test/test_action_view_support.rb
0
+test/test_avatar.rb
0
+test/test_file_column_source.rb
0
+test/test_gravatar_source.rb
0
+test/test_helper.rb
0
+test/test_nil_source.rb
0
+test/test_source_chain.rb
0
+test/test_static_url_source.rb
0
+test/test_string_substitution_source.rb
0
+website/index.html
0
+website/index.txt
0
+website/javascripts/rounded_corners_lite.inc.js
0
+website/stylesheets/screen.css
0
+website/template.rhtml
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
0
@@ -1 +1,66 @@
0
+= avatar
0
+
0
+* http://avatar.rubyforge.org
0
+
0
+== DESCRIPTION:
0
+Avatar is a collection of Ruby utilities for displaying avatars.
0
+
0
+== FEATURES/PROBLEMS:
0
+
0
+Avatar currently supports the following sources:
0
+* Gravatar (see http://www.gravatar.com)
0
+* a constant URL (e.g. http://mysite.com/images/default_icon.png)
0
+* parameterized URLs (e.g. http://mysite.com/images/famfamfam/user_#{color}.png)
0
+* file_column (see http://www.kanthak.net/opensource/file_column/)
0
+* chains of sources (e.g. file_column if exists; otherwise default constant URL)
0
+
0
+Avatar currently supports the following views:
0
+* ActionView (Rails), through avatar_tag
0
+* AbstractView (any framework), through avatar_url_for
0
+
0
+== SYNOPSIS:
0
+
0
+in RAILS_ROOT/app/helpers/people_helper.rb
0
+require 'avatar/view/action_view_support'
0
+class PeopleHelper
0
+ include Avatar::View::ActionViewSupport
0
+end
0
+
0
+in RAILS_ROOT/app/views/people/show.html.erb:
0
+<%= avatar_tag(@current_user, :size => 40) %>
0
+
0
+== REQUIREMENTS:
0
+
0
+* none for basic functionality
0
+* will integrate with ActionView
0
+* will integrate with the file_column Rails plugin
0
+
0
+== INSTALL:
0
+
0
+* sudo gem install avatar
0
+
0
+== LICENSE:
0
+
0
+(The MIT License)
0
+
0
+Copyright (c) 2008 Universal Presenece, Inc.
0
+
0
+Permission is hereby granted, free of charge, to any person obtaining
0
+a copy of this software and associated documentation files (the
0
+'Software'), to deal in the Software without restriction, including
0
+without limitation the rights to use, copy, modify, merge, publish,
0
+distribute, sublicense, and/or sell copies of the Software, and to
0
+permit persons to whom the Software is furnished to do so, subject to
0
+the following conditions:
0
+
0
+The above copyright notice and this permission notice shall be
0
+included in all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
0
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
0
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
0
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
0
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
...
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
0
@@ -1 +1,11 @@
0
+require 'config/requirements'
0
+require 'config/hoe' # setup Hoe + all gem configuration
0
+
0
+Rake::TaskManager.class_eval do
0
+ def remove_task(task_name)
0
+ @tasks.delete(task_name.to_s)
0
+ end
0
+end
0
+
0
+Dir['tasks/**/*.rake'].each { |rake| load rake }
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
0
@@ -1 +1,71 @@
0
+require 'avatar/version'
0
+
0
+AUTHOR = 'James Rosen' # can also be an array of Authors
0
+EMAIL = "james@u-presence.com"
0
+DESCRIPTION = "helpers for displaying avatars"
0
+GEM_NAME = 'avatar' # what ppl will type to install your gem
0
+RUBYFORGE_PROJECT = 'avatar' # The unix name for your project
0
+HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
0
+DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
0
+
0
+@config_file = "~/.rubyforge/user-config.yml"
0
+@config = nil
0
+RUBYFORGE_USERNAME = "unknown"
0
+def rubyforge_username
0
+ unless @config
0
+ begin
0
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
0
+ rescue
0
+ puts <<-EOS
0
+ERROR: No rubyforge config file found: #{@config_file}
0
+Run 'rubyforge setup' to prepare your env for access to Rubyforge
0
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
0
+ EOS
0
+ exit
0
+ end
0
+ end
0
+ RUBYFORGE_USERNAME.replace @config["username"]
0
+end
0
+
0
+
0
+REV = nil
0
+# UNCOMMENT IF REQUIRED:
0
+# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
0
+VERS = Avatar::VERSION::STRING + (REV ? ".#{REV}" : "")
0
+RDOC_OPTS = ['--quiet', '--title', 'avatar documentation',
0
+ "--opname", "index.html",
0
+ "--line-numbers",
0
+ "--main", "README",
0
+ "--inline-source"]
0
+
0
+class Hoe
0
+ def extra_deps
0
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
0
+ @extra_deps
0
+ end
0
+end
0
+
0
+# Generate all the Rake tasks
0
+# Run 'rake -T' to see list of generated tasks (from gem root directory)
0
+hoe = Hoe.new(GEM_NAME, VERS) do |p|
0
+ p.developer(AUTHOR, EMAIL)
0
+ p.description = DESCRIPTION
0
+ p.summary = DESCRIPTION
0
+ p.url = HOMEPATH
0
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
0
+ p.test_globs = ["test/**/test_*.rb"]
0
+ p.clean_globs |= ['coverage', 'test/public', '**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
0
+
0
+ # == Optional
0
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
0
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
0
+
0
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
0
+
0
+end
0
+
0
+CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
0
+PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
0
+hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
0
+hoe.rsync_args = '-av --delete --ignore-errors'
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
0
@@ -1 +1,18 @@
0
+require 'fileutils'
0
+include FileUtils
0
+
0
+require 'rubygems'
0
+%w[rake hoe newgem rubigen].each do |req_gem|
0
+ begin
0
+ require req_gem
0
+ rescue LoadError
0
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
0
+ puts "Installation: gem install #{req_gem} -y"
0
+ exit
0
+ end
0
+end
0
+
0
+$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
0
+
0
+require 'avatar'
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
0
@@ -1 +1,45 @@
0
+$:.unshift File.expand_path(File.dirname(__FILE__))
0
+require 'avatar/source/abstract_source'
0
+require 'avatar/source/gravatar_source'
0
+require 'avatar/source/static_url_source'
0
+require 'avatar/source/nil_source'
0
+
0
+# Helpers for displaying avatars.
0
+# Usage in Rails:
0
+# # in app/helpers/ProfileHelper.rb:
0
+# include Avatar::ActionView::Support
0
+#
0
+# # in app/views/profiles/show.html.erb:
0
+# <%= avatar_for @person =>
0
+#
0
+# By default, Avatar::source is a GravatarSource with
0
+# StaticUrlSource as its default.
0
+# The latter has the url '/images/default_avatar.png'.
0
+module Avatar
0
+
0
+ @@source = Avatar::Source::NilSource.new
0
+ @@default_avatar_options = {}
0
+
0
+ def self.source
0
+ @@source.dup
0
+ end
0
+
0
+ def self.source=(source)
0
+ raise ArgumentError.new("#{source} is not an Avatar::Source::AbstractSource") unless source.kind_of?(Avatar::Source::AbstractSource)
0
+ @@source = source
0
+ end
0
+
0
+ def self.default_avatar_options
0
+ @@default_avatar_options.dup
0
+ end
0
+
0
+ def self.default_avatar_options=(options)
0
+ raise ArgumentError.new("#{options} is not a Hash") unless options.kind_of?(Hash)
0
+ @@options = options
0
+ end
0
+
0
+end
0
+
0
+url_source = Avatar::Source::StaticUrlSource.new('/images/default_avatar.png')
0
+Avatar.source = Avatar::Source::GravatarSource.new(url_source)
...
 
 
 
 
...
1
2
3
4
0
@@ -1 +1,5 @@
0
+module Avatar # :nodoc:
0
+ module Source # :nodoc:
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0
@@ -1 +1,16 @@
0
+module Avatar # :nodoc:
0
+ module Source # :nodoc:
0
+ # To be included by classes that generate avatar URLs from profiles.
0
+ module AbstractSource
0
+
0
+ # Return an avatar URL for the person, or nil if this source cannot generate one.
0
+ # Including classes <em>must</em> override this method. In general, implementations
0
+ # should return nil if +person+ is nil.
0
+ def avatar_url_for(person, options = {})
0
+ raise NotImplementedError.new('including class must define avatar_url_for(person, options = {})')
0
+ end
0
+
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
0
@@ -1 +1,36 @@
0
+require 'avatar/source/abstract_source'
0
+require 'file_column_helper'
0
+
0
+module Avatar # :nodoc:
0
+ module Source # :nodoc:
0
+ # For use with the FileColumn Rails plugin.
0
+ class FileColumnSource
0
+ include AbstractSource
0
+ include FileColumnHelper
0
+
0
+ attr_accessor :default_field
0
+
0
+ def initialize(default_field = :avatar)
0
+ raise ArgumentError.new('default_field cannot be nil') if default_field.nil?
0
+ @default_field = default_field
0
+ end
0
+
0
+ # Returns nil if person is nil; otherwise, returns the (possibly nil) result of
0
+ # <code>url_for_image_column</code>, passing in all of +options+ except :field.
0
+ # Options:
0
+ # * <code>:field</code> - the image file column within +person+; by default, :avatar
0
+ # * <code>:file_column_version</code> - one of the versions of the file_column; no default
0
+ # If :file_column_version is not specified, all other options are passed to
0
+ # <code>url_for_image_column</code> as +options+ (see FileColumnHelper)
0
+ def avatar_url_for(person, options = {})
0
+ return nil if person.nil?
0
+ field = options.delete(:field) || default_field
0
+ return nil if field.nil? || person.send(field).nil?
0
+ options = options[:file_column_version] || options
0
+ url_for_image_column(person, field, options)
0
+ end
0
+
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
0
@@ -1 +1,79 @@
0
+require 'avatar/source/abstract_source'
0
+require 'avatar/source/static_url_source'
0
+require 'avatar/source/nil_source'
0
+require 'digest/md5'
0
+
0
+module Avatar # :nodoc:
0
+ module Source # :nodoc:
0
+ # NOTE: since Gravatar always returns a URL (never a 404), instances of this
0
+ # class should only be placed at the end of a SourceChain.
0
+ # (see link:classes/Avatar/Source/SourceChain.html)
0
+ # Alternatively, use <code>default_source = ...</code> to generate a site-wide
0
+ # default to be passed to Gravatar. (In fact, since <code>default_source</code>
0
+ # is an instance of Avatar::Source::AbstractSource, it can generate a different
0
+ # default for each person.)
0
+ class GravatarSource
0
+ include AbstractSource
0
+
0
+ attr_accessor :default_field
0
+ attr_reader :default_source
0
+
0
+ # 'http://www.gravatar.com/avatar/'
0
+ def self.base_url
0
+ 'http://www.gravatar.com/avatar/'
0
+ end
0
+
0
+ # :nodoc:
0
+ def initialize(default_source = nil, default_field = :email)
0
+ self.default_source = default_source #not @default_source = ... b/c want to use the setter function below
0
+ @default_field = default_field
0
+ raise "There's a bug in the code" if @default_source.nil?
0
+ end
0
+
0
+ # Generates a Gravatar URL. Returns nil if person is nil.
0
+ # Options:
0
+ # * <code>:field (Symbol)</code> - the field to call from person. By default, <code>:email</code>.
0
+ # * <code>:default (String)</code> - override the default generated by <code>default_source</code>.
0
+ # * <code>:size or :s</code> - the size in pixels of the avatar to render.
0
+ # * <code>:rating or :r</code> - the maximum rating; one of ['G', 'PG', 'R', 'X']
0
+ def avatar_url_for(person, options = {})
0
+ return nil if person.nil?
0
+ options[:default] ||= default_avatar_url_for(person, options)
0
+ field = options[:field] || default_field
0
+ raise ArgumentError.new('No field specified; either specify a default field or pass in a value for :field (probably :email)') unless field
0
+ url = self.class.base_url
0
+ url << Digest::MD5::hexdigest(person.send(field)).strip
0
+ [:size, :s, :rating, :r, :default].each do |x|
0
+ next unless options[x]
0
+ url << (url.include?('?') ? '&' : '?')
0
+ url << "#{x}=#{options[x]}"
0
+ end
0
+ url
0
+ end
0
+
0
+ # Set the default source for all people.
0
+ # If +default+ is a String, it will be converted to an instance of Avatar::Source::StaticUrlSource.
0
+ # If +default+ is nil, sets the default to a NilSource.
0
+ def default_source=(default)
0
+ case default
0
+ when String
0
+ @default_source = StaticUrlSource.new(default)
0
+ when AbstractSource
0
+ @default_source = default
0
+ when NilClass
0
+ @default_source = NilSource.new
0
+ else
0
+ raise ArgumentError.new("#{default} must be either a String or an instance of #{AbstractSource}")
0
+ end
0
+ end
0
+
0
+ private
0
+
0
+ def default_avatar_url_for(person, options)
0
+ @default_source.avatar_url_for(person, options)
0
+ end
0
+
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
0
@@ -1 +1,18 @@
0
+require 'avatar/source/abstract_source'
0
+
0
+module Avatar # :nodoc:
0
+ module Source # :nodoc:
0
+ # A really dumb implementation that never returns a URL.
0
+ # Can be helpful for testing. Also used in GravatarSource::default_source.
0
+ class NilSource
0
+ include AbstractSource
0
+
0
+ # Always returns nil.
0
+ def avatar_url_for(person, options = {})
0
+ nil
0
+ end
0
+
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
0
@@ -1 +1,52 @@
0
+require 'avatar/source/abstract_source'
0
+
0
+module Avatar # :nodoc:
0
+ module Source # :nodoc:
0
+ class SourceChain
0
+ include AbstractSource
0
+
0
+ # :nodoc:
0
+ def initialize
0
+ clear!
0
+ end
0
+
0
+ # Clear the chain
0
+ def clear!
0
+ @chain = []
0
+ end
0
+
0
+ # Retrieve the +n+<sup>th</sup> Source.
0
+ def [](n)
0
+ @chain[n]
0
+ end
0
+
0
+ # Add a source to the chain. +source+ must be an instance of (a subclass of) Avatar::Source::AbstractSource.
0
+ def add_source(source)
0
+ raise ArgumentError.new("#{source} is not an Avatar::Source::AbstractSource") unless source.kind_of?(Avatar::Source::AbstractSource)
0
+ @chain << source
0
+ end
0
+
0
+ # Alias for <code>add_source</code>
0
+ def <<(source)
0
+ add_source(source)
0
+ end
0
+
0
+ # True unless a Source has been added.
0
+ def empty?
0
+ @chain.empty?
0
+ end
0
+
0
+ # Iterate through the chain and return the first URL returned.
0
+ # Any error raised will propagate.
0
+ def avatar_url_for(person, options = {})
0
+ @chain.each do |source|
0
+ result = source.avatar_url_for(person, options)
0
+ return result unless result.nil?
0
+ end
0
+ return nil
0
+ end
0
+
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
0
@@ -1 +1,26 @@
0
+require 'avatar/source/abstract_source'
0
+
0
+module Avatar # :nodoc:
0
+ module Source # :nodoc:
0
+ # Source representing a constant URL.
0
+ # Good as a default or last-resort source.
0
+ class StaticUrlSource
0
+ include AbstractSource
0
+
0
+ attr_accessor :url
0
+
0
+ # Create a new source with static url +url+.
0
+ def initialize(url)
0
+ raise ArgumentError.new("URL cannot be nil") if url.nil?
0
+ @url = url.to_s
0
+ end
0
+
0
+ # Returns nil if person is nil; the static url otherwise.
0
+ def avatar_url_for(person, options = {})
0
+ person.nil? ? nil : url
0
+ end
0
+
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
0
@@ -1 +1,49 @@
0
+require 'avatar/source/abstract_source'
0
+
0
+module Avatar # :nodoc:
0
+ module Source # :nodoc:
0
+ # Like a StaticUrlSource, but allows variable replacement within the string.
0
+ # Usage:
0
+ # source = StringSubstitutionSource.new('#{gender}_icon_#{size}.png')
0
+ # url = source.avatar_url_for(@person, :gender => :female, :size => :large)
0
+ # # => 'female_icon_large.png'
0
+ class StringSubstitutionSource
0
+ include AbstractSource
0
+
0
+ attr_accessor :url
0
+
0
+ # Create a new source with static url +url+, which can contain any number
0
+ # of variables to be subsituted through +options+. Strings should
0
+ # be of the form '...#{variable_a}...#{variable_b}...'. <em>note the
0
+ # single quotes</em>; double quotes will cause the variables to be
0
+ # substituted at Source-creation (when #new is called); this is almost
0
+ # certainly <strong>not</strong> what you want.
0
+ def initialize(url)
0
+ raise ArgumentError.new("URL cannot be nil") if url.nil?
0
+ @url = url.to_s
0
+ end
0
+
0
+ # Returns nil if +person+ is nil or if variables in <code>url</code>
0
+ # remain un-bound after substituting +options+; otherwise, returns
0
+ # the result of replacing each variable within <code>url</code>
0
+ # with the value of the corresponding key within +options+.
0
+ def avatar_url_for(person, options = {})
0
+ return nil if person.nil?
0
+ result = apply_replacement(options)
0
+ result =~ /#\{.*\}/ ? nil : result
0
+ end
0
+
0
+ private
0
+
0
+ def apply_replacement(options)
0
+ result = self.url
0
+ options.each do |k,v|
0
+ result = result.gsub(Regexp.new('#\{' + "#{k}" + '\}'), "#{v}")
0
+ end
0
+ result
0
+ end
0
+
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
0
@@ -1 +1,10 @@
0
+module Avatar #:nodoc:
0
+ module VERSION #:nodoc:
0
+ MAJOR = 0
0
+ MINOR = 0
0
+ TINY = 1
0
+
0
+ STRING = [MAJOR, MINOR, TINY].join('.')
0
+ end
0
+end