Module: SitemapGenerator::Utilities

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) append_slash(path)

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



157
158
159
160
161
162
163
164
# 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

- (Object) as_array(value)

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.



54
55
56
57
58
59
60
61
62
# 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

- (Object) assert_valid_keys(hash, *valid_keys)

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)


32
33
34
35
# 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

- (Boolean) blank?(object)

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)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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

- (Object) bytesize(string)

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



177
178
179
# File 'lib/sitemap_generator/utilities.rb', line 177

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

- (Object) clean_files

Clean sitemap files in output directory.



25
26
27
# File 'lib/sitemap_generator/utilities.rb', line 25

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

- (Object) ellipsis(string, max)

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



168
169
170
171
172
173
174
# File 'lib/sitemap_generator/utilities.rb', line 168

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

- (Boolean) falsy?(value)

Returns:

  • (Boolean)


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

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

- (Object) install_sitemap_rb(verbose = false)

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



6
7
8
9
10
11
12
13
14
15
# 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

- (Boolean) present?(object)

An object is present if it's not blank.

Returns:

  • (Boolean)


130
131
132
# File 'lib/sitemap_generator/utilities.rb', line 130

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

- (Object) reverse_merge(hash, other_hash)

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.



94
95
96
# File 'lib/sitemap_generator/utilities.rb', line 94

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

- (Object) reverse_merge!(hash, other_hash)

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



100
101
102
# File 'lib/sitemap_generator/utilities.rb', line 100

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

- (Object) round(float, precision = nil)

Rounds the float with the specified precision.

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


70
71
72
73
74
75
76
77
# 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

- (Object) symbolize_keys(hash)

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



39
40
41
# File 'lib/sitemap_generator/utilities.rb', line 39

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

- (Object) symbolize_keys!(hash)

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



45
46
47
48
49
50
# 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

- (Object) titleize(string)



142
143
144
145
# File 'lib/sitemap_generator/utilities.rb', line 142

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

- (Boolean) truthy?(value)

Returns:

  • (Boolean)


147
148
149
# File 'lib/sitemap_generator/utilities.rb', line 147

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

- (Object) uninstall_sitemap_rb

Remove config/sitemap.rb if exists.



18
19
20
21
22
# 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

- (Object) with_warnings(flag)

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



135
136
137
138
139
140
# File 'lib/sitemap_generator/utilities.rb', line 135

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