module SitemapGenerator::Utilities

Public Instance Methods

append_slash(path) click to toggle source

Append a slash to ‘path` if it does not already end in a slash. Returns a string. Expects a string or Pathname object.

# File lib/sitemap_generator/utilities.rb, line 157
def append_slash(path)
  strpath = path.to_s
  if strpath[-1] != nil && strpath[-1].chr != '/'
    strpath + '/'
  else
    strpath
  end
end
as_array(value) click to toggle source

Make a list of ‘value` if it is not a list already. If `value` is nil, an empty list is returned. If `value` is already a list, return it unchanged.

# File lib/sitemap_generator/utilities.rb, line 54
def as_array(value)
  if value.nil?
    []
  elsif value.is_a?(Array)
    value
  else
    [value]
  end
end
assert_valid_keys(hash, *valid_keys) click to toggle source

Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch. Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols as keys, this will fail.

# File lib/sitemap_generator/utilities.rb, line 32
def assert_valid_keys(hash, *valid_keys)
  unknown_keys = hash.keys - [valid_keys].flatten
  raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
end
blank?(object) click to toggle source

An object is blank if it’s false, empty, or a whitespace string. For example, “”, “ ”, nil, [], and {} are blank.

This simplifies:

if !address.nil? && !address.empty?

…to:

if !address.blank?
# File lib/sitemap_generator/utilities.rb, line 114
def blank?(object)
  case object
  when NilClass, FalseClass
    true
  when TrueClass, Numeric
    false
  when String
    object !~ /\S/
  when Hash, Array
    object.empty?
  when Object
    object.respond_to?(:empty?) ? object.empty? : !object
  end
end
bytesize(string) click to toggle source

Return the bytesize length of the string. Ruby 1.8.6 compatible.

# File lib/sitemap_generator/utilities.rb, line 177
def bytesize(string)
  string.respond_to?(:bytesize) ? string.bytesize : string.length
end
clean_files() click to toggle source

Clean sitemap files in output directory.

# File lib/sitemap_generator/utilities.rb, line 25
def clean_files
  FileUtils.rm(Dir[SitemapGenerator.app.root + 'public/sitemap*.xml.gz'])
end
ellipsis(string, max) click to toggle source

Replace the last 3 characters of string with … if the string is as big or bigger than max.

# File lib/sitemap_generator/utilities.rb, line 168
def ellipsis(string, max)
  if string.size > max
    (string[0, max - 3] || '') + '...'
  else
    string
  end
end
falsy?(value) click to toggle source
# File lib/sitemap_generator/utilities.rb, line 151
def falsy?(value)
  ['0', 0, 'f', 'false', false].include?(value)
end
install_sitemap_rb(verbose=false) click to toggle source

Copy templates/sitemap.rb to config if not there yet.

# File lib/sitemap_generator/utilities.rb, line 6
def install_sitemap_rb(verbose=false)
  if File.exist?(SitemapGenerator.app.root + 'config/sitemap.rb')
    puts "already exists: config/sitemap.rb, file not copied" if verbose
  else
    FileUtils.cp(
      SitemapGenerator.templates.template_path(:sitemap_sample),
      SitemapGenerator.app.root + 'config/sitemap.rb')
    puts "created: config/sitemap.rb" if verbose
  end
end
present?(object) click to toggle source

An object is present if it’s not blank.

# File lib/sitemap_generator/utilities.rb, line 130
def present?(object)
  !blank?(object)
end
reverse_merge(hash, other_hash) click to toggle source

Allows for reverse merging two hashes where the keys in the calling hash take precedence over those in the other_hash. This is particularly useful for initializing an option hash with default values:

def setup(options = {})
  options.reverse_merge! :size => 25, :velocity => 10
end

Using merge, the above example would look as follows:

def setup(options = {})
  { :size => 25, :velocity => 10 }.merge(options)
end

The default :size and :velocity are only set if the options hash passed in doesn’t already have the respective key.

# File lib/sitemap_generator/utilities.rb, line 94
def reverse_merge(hash, other_hash)
  other_hash.merge(hash)
end
reverse_merge!(hash, other_hash) click to toggle source

Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second. Modifies the receiver in place.

# File lib/sitemap_generator/utilities.rb, line 100
def reverse_merge!(hash, other_hash)
  hash.merge!( other_hash ){|k,o,n| o }
end
round(float, precision = nil) click to toggle source

Rounds the float with the specified precision.

x = 1.337
x.round    # => 1
x.round(1) # => 1.3
x.round(2) # => 1.34
# File lib/sitemap_generator/utilities.rb, line 70
def round(float, precision = nil)
  if precision
    magnitude = 10.0 ** precision
    (float * magnitude).round / magnitude
  else
    float.round
  end
end
symbolize_keys(hash) click to toggle source

Return a new hash with all keys converted to symbols, as long as they respond to to_sym.

# File lib/sitemap_generator/utilities.rb, line 39
def symbolize_keys(hash)
  symbolize_keys!(hash.dup)
end
symbolize_keys!(hash) click to toggle source

Destructively convert all keys to symbols, as long as they respond to to_sym.

# File lib/sitemap_generator/utilities.rb, line 45
def symbolize_keys!(hash)
  hash.keys.each do |key|
    hash[(key.to_sym rescue key) || key] = hash.delete(key)
  end
  hash
end
titleize(string) click to toggle source
# File lib/sitemap_generator/utilities.rb, line 142
def titleize(string)
  string.gsub!(/_/, ' ')
  string.split(/(\W)/).map(&:capitalize).join
end
truthy?(value) click to toggle source
# File lib/sitemap_generator/utilities.rb, line 147
def truthy?(value)
  ['1', 1, 't', 'true', true].include?(value)
end
uninstall_sitemap_rb() click to toggle source

Remove config/sitemap.rb if exists.

# File lib/sitemap_generator/utilities.rb, line 18
def uninstall_sitemap_rb
  if File.exist?(SitemapGenerator.app.root + 'config/sitemap.rb')
    File.rm(SitemapGenerator.app.root + 'config/sitemap.rb')
  end
end
with_warnings(flag) { || ... } click to toggle source

Sets $VERBOSE for the duration of the block and back to its original value afterwards.

# File lib/sitemap_generator/utilities.rb, line 135
def with_warnings(flag)
  old_verbose, $VERBOSE = $VERBOSE, flag
  yield
ensure
  $VERBOSE = old_verbose
end