Sha256: c9417280132c2c063877328c352817a0f1999fa3e56c03964f2cbb0953189864

Contents?: true

Size: 2 KB

Versions: 5

Compression:

Stored size: 2 KB

Contents

require 'avatar/source/wrapper/abstract_source_wrapper'
require 'action_view/helpers/asset_tag_helper'

module Avatar # :nodoc:
  module Source # :nodoc:
    module Wrapper
      # Wraps a Source using Rails' <code>AssetTagHelper#image_path</code>,
      # which can turn path URLs (e.g. '/images/my_avatar.png')
      # into absolute URLs( e.g. 'http://assets.mysite.com/images/my_avatar.png').
      class StringSubstitutionSourceWrapper < AbstractSourceWrapper
      
        attr_accessor :default_options
      
        def initialize(source, default_options = {})
          super(source)
          self.default_options = default_options || {}
        end
      
        # Passes +url+ to <code>AssetTagHelper#image_path</code>.  Raises
        # an error if it cannot generate a fully-qualified URI.  Try
        # setting <code>ActionController::Base.asset_host</code> to
        # avoid this error.
        def wrap(url, person, options = {})
          # url will never be nil b/c of guarantee in AbstractSourceWrapper
          result = apply_substitution(url, self.default_options.merge(options))
          substitution_needed?(result) ? nil : result
        end

        def default_options=(opts)
          @default_options = opts || {}
        end
        
        # For each key in +options+ replaces '#{key}' in +string+ with the
        # corresponding value in +options+.
        # +string+ should
        # be of the form '...#{variable_a}...#{variable_b}...'.  <em>Note the
        # single quotes</em>.  Double quotes will cause the variables to be
        # substituted before this method is run, which is almost
        # certainly <strong>not</strong> what you want.
        def apply_substitution(string, options)
          returning(string.dup) do |result|
            options.each do |k,v|
              result.gsub!(Regexp.new('#\{' + "#{k}" + '\}'), "#{v}")
            end
          end
        end

        def substitution_needed?(string)
          string =~ /#\{.*\}/
        end
      
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
gcnovus-avatar-0.0.7 lib/avatar/source/wrapper/string_substitution_source_wrapper.rb
gcnovus-avatar-0.2.0 lib/avatar/source/wrapper/string_substitution_source_wrapper.rb
krasivotokak-avatar-0.0.7 lib/avatar/source/wrapper/string_substitution_source_wrapper.rb
avatar-0.2.0 lib/avatar/source/wrapper/string_substitution_source_wrapper.rb
avatar-0.0.5 lib/avatar/source/wrapper/string_substitution_source_wrapper.rb