Module: SitemapGenerator::Utilities

Extended by:
Utilities
Included in:
Utilities
Defined in:
lib/sitemap_generator/utilities.rb

Instance Method Summary collapse

Instance Method Details

#append_slash(path) ⇒ Object

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



160
161
162
163
164
165
166
167
# File 'lib/sitemap_generator/utilities.rb', line 160

def append_slash(path)
  strpath = path.to_s
  if strpath[-1] != nil && strpath[-1].chr != '/'
    strpath + '/'
  else
    strpath
  end
end

#as_array(value) ⇒ Object

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.



56
57
58
59
60
61
62
63
64
# File 'lib/sitemap_generator/utilities.rb', line 56

def as_array(value)
  if value.nil?
    []
  elsif value.is_a?(Array)
    value
  else
    [value]
  end
end

#assert_valid_keys(hash, *valid_keys) ⇒ Object

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.

Raises:

  • (ArgumentError)


34
35
36
37
# File 'lib/sitemap_generator/utilities.rb', line 34

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) ⇒ Boolean

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?

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/sitemap_generator/utilities.rb', line 116

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) ⇒ Object

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



180
181
182
# File 'lib/sitemap_generator/utilities.rb', line 180

def bytesize(string)
  string.respond_to?(:bytesize) ? string.bytesize : string.length
end

#clean_filesObject

Clean sitemap files in output directory.



27
28
29
# File 'lib/sitemap_generator/utilities.rb', line 27

def clean_files
  FileUtils.rm(Dir[SitemapGenerator.app.root + 'public/sitemap*.xml.gz'])
end

#ellipsis(string, max) ⇒ Object

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



171
172
173
174
175
176
177
# File 'lib/sitemap_generator/utilities.rb', line 171

def ellipsis(string, max)
  if string.size > max
    (string[0, max - 3] || '') + '...'
  else
    string
  end
end

#falsy?(value) ⇒ Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/sitemap_generator/utilities.rb', line 154

def falsy?(value)
  ['0', 0, 'f', 'false', false].include?(value)
end

#install_sitemap_rb(verbose = false) ⇒ Object

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



8
9
10
11
12
13
14
15
16
17
# File 'lib/sitemap_generator/utilities.rb', line 8

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) ⇒ Boolean

An object is present if it’s not blank.

Returns:

  • (Boolean)


132
133
134
# File 'lib/sitemap_generator/utilities.rb', line 132

def present?(object)
  !blank?(object)
end

#reverse_merge(hash, other_hash) ⇒ Object

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.



96
97
98
# File 'lib/sitemap_generator/utilities.rb', line 96

def reverse_merge(hash, other_hash)
  other_hash.merge(hash)
end

#reverse_merge!(hash, other_hash) ⇒ Object

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



102
103
104
# File 'lib/sitemap_generator/utilities.rb', line 102

def reverse_merge!(hash, other_hash)
  hash.merge!( other_hash ){|k,o,n| o }
end

#round(float, precision = nil) ⇒ Object

Rounds the float with the specified precision.

x = 1.337
x.round    # => 1
x.round(1) # => 1.3
x.round(2) # => 1.34


72
73
74
75
76
77
78
79
# File 'lib/sitemap_generator/utilities.rb', line 72

def round(float, precision = nil)
  if precision
    magnitude = 10.0 ** precision
    (float * magnitude).round / magnitude
  else
    float.round
  end
end

#symbolize_keys(hash) ⇒ Object

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



41
42
43
# File 'lib/sitemap_generator/utilities.rb', line 41

def symbolize_keys(hash)
  symbolize_keys!(hash.dup)
end

#symbolize_keys!(hash) ⇒ Object

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



47
48
49
50
51
52
# File 'lib/sitemap_generator/utilities.rb', line 47

def symbolize_keys!(hash)
  hash.keys.each do |key|
    hash[(key.to_sym rescue key) || key] = hash.delete(key)
  end
  hash
end

#titleize(string) ⇒ Object



144
145
146
147
148
# File 'lib/sitemap_generator/utilities.rb', line 144

def titleize(string)
  string = string.dup if string.frozen?
  string.gsub!(/_/, ' ')
  string.split(/(\W)/).map(&:capitalize).join
end

#truthy?(value) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/sitemap_generator/utilities.rb', line 150

def truthy?(value)
  ['1', 1, 't', 'true', true].include?(value)
end

#uninstall_sitemap_rbObject

Remove config/sitemap.rb if exists.



20
21
22
23
24
# File 'lib/sitemap_generator/utilities.rb', line 20

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) ⇒ Object

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



137
138
139
140
141
142
# File 'lib/sitemap_generator/utilities.rb', line 137

def with_warnings(flag)
  old_verbose, $VERBOSE = $VERBOSE, flag
  yield
ensure
  $VERBOSE = old_verbose
end