class Avatar::Source::Wrapper::StringSubstitutionSourceWrapper

Wraps a Source using Rails’ AssetTagHelper#image_path, which can turn path URLs (e.g. ‘/images/my_avatar.png’) into absolute URLs( e.g. ‘’).

Attributes

default_options[RW]

Public Instance Methods

apply_substitution(string, options) click to toggle source

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}…’. Note the single quotes. Double quotes will cause the variables to be substituted before this method is run, which is almost certainly <strong>not</strong> what you want.

# File lib/avatar/source/wrapper/string_substitution_source_wrapper.rb, line 40
def apply_substitution(string, options)
  returning(string.dup) do |result|
    options.each do |k,v|
      result.gsub!(Regexp.new('#\{' + "#{k}" + '\}'), "#{v}")
    end
  end
end
default_options=(opts) click to toggle source
# File lib/avatar/source/wrapper/string_substitution_source_wrapper.rb, line 29
def default_options=(opts)
  @default_options = opts || {}
end
substitution_needed?(string) click to toggle source
# File lib/avatar/source/wrapper/string_substitution_source_wrapper.rb, line 48
def substitution_needed?(string)
  string =~ /#\{.*\}/
end
wrap(url, person, options = {}) click to toggle source

Passes url to AssetTagHelper#image_path. Raises an error if it cannot generate a fully-qualified URI. Try setting ActionController::Base.asset_host to avoid this error.

# File lib/avatar/source/wrapper/string_substitution_source_wrapper.rb, line 23
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

Public Class Methods

new(source, default_options = {}) click to toggle source
# File lib/avatar/source/wrapper/string_substitution_source_wrapper.rb, line 14
def initialize(source, default_options = {})
  super(source)
  self.default_options = default_options || {}
end