Module: SitemapGenerator::Utilities
Instance Method Summary (collapse)
-
- (Object) assert_valid_keys(hash, *valid_keys)
Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch.
-
- (Boolean) blank?(object)
An object is blank if it's false, empty, or a whitespace string.
-
- (Object) clean_files
Clean sitemap files in output directory.
- - (Boolean) falsy?(value)
-
- (Object) install_sitemap_rb(verbose = false)
Copy templates/sitemap.rb to config if not there yet.
-
- (Boolean) present?(object)
An object is present if it's not blank.
-
- (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.
-
- (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.
-
- (Object) round(float, precision = nil)
Rounds the float with the specified precision.
-
- (Object) symbolize_keys(hash)
Return a new hash with all keys converted to symbols, as long as they respond to to_sym.
-
- (Object) symbolize_keys!(hash)
Destructively convert all keys to symbols, as long as they respond to to_sym.
- - (Object) titleize(string)
- - (Boolean) truthy?(value)
-
- (Object) uninstall_sitemap_rb
Remove config/sitemap.rb if exists.
-
- (Object) with_warnings(flag)
Sets $VERBOSE for the duration of the block and back to its original value afterwards.
Instance Method Details
- (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.
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?
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/sitemap_generator/utilities.rb', line 102 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) 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 |
- (Boolean) falsy?(value)
139 140 141 |
# File 'lib/sitemap_generator/utilities.rb', line 139 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.
118 119 120 |
# File 'lib/sitemap_generator/utilities.rb', line 118 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( = {})
.reverse_merge! :size => 25, :velocity => 10
end
Using merge, the above example would look as follows:
def setup( = {})
{ :size => 25, :velocity => 10 }.merge()
end
The default :size and :velocity are only set if the options hash passed in doesn't already have the respective key.
82 83 84 |
# File 'lib/sitemap_generator/utilities.rb', line 82 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.
88 89 90 |
# File 'lib/sitemap_generator/utilities.rb', line 88 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
58 59 60 61 62 63 64 65 |
# File 'lib/sitemap_generator/utilities.rb', line 58 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)
130 131 132 133 |
# File 'lib/sitemap_generator/utilities.rb', line 130 def titleize(string) string.gsub!(/_/, ' ') string.split(/(\W)/).map(&:capitalize).join end |
- (Boolean) truthy?(value)
135 136 137 |
# File 'lib/sitemap_generator/utilities.rb', line 135 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.
123 124 125 126 127 128 |
# File 'lib/sitemap_generator/utilities.rb', line 123 def with_warnings(flag) old_verbose, $VERBOSE = $VERBOSE, flag yield ensure $VERBOSE = old_verbose end |