Module: SitemapGenerator::Utilities
- Defined in:
- lib/sitemap_generator/utilities.rb
Overview
Internal utility functions: hash merging, string manipulation, byte calculation, and warning suppression. Not part of the public API.
Class Method Summary collapse
-
.append_slash(path) ⇒ Object
Append a slash to
pathif it does not already end in a slash. -
.as_array(value) ⇒ Object
Make a list of
valueif it is not a list already. -
.assert_valid_keys(hash, *valid_keys) ⇒ Object
Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch.
-
.blank?(object) ⇒ Boolean
An object is blank if it's false, empty, or a whitespace string.
-
.bytesize(string) ⇒ Object
Return the bytesize length of the string.
-
.clean_files ⇒ Object
Clean sitemap files in output directory.
-
.current_time ⇒ Object
Return the current time, using Time.zone if available (Rails), otherwise Time.now.
-
.ellipsis(string, max) ⇒ Object
Replace the last 3 characters of string with ...
- .falsy?(value) ⇒ Boolean
-
.install_sitemap_rb(verbose = false) ⇒ Object
Copy templates/sitemap.rb to config if not there yet.
-
.present?(object) ⇒ Boolean
An object is present if it's not blank.
-
.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.
-
.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.
-
.round(float, precision = nil) ⇒ Object
Rounds the float with the specified precision.
-
.symbolize_keys(hash) ⇒ Object
Return a new hash with all keys converted to symbols, as long as they respond to
to_sym. -
.symbolize_keys!(hash) ⇒ Object
Destructively convert all keys to symbols, as long as they respond to
to_sym. - .titleize(string) ⇒ Object
- .truthy?(value) ⇒ Boolean
-
.uninstall_sitemap_rb ⇒ Object
Remove config/sitemap.rb if exists.
-
.with_warnings(flag) ⇒ Object
Sets $VERBOSE for the duration of the block and back to its original value afterwards.
Class 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.
173 174 175 176 177 178 179 180 |
# File 'lib/sitemap_generator/utilities.rb', line 173 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.
68 69 70 71 72 73 74 75 76 |
# File 'lib/sitemap_generator/utilities.rb', line 68 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.
42 43 44 45 |
# File 'lib/sitemap_generator/utilities.rb', line 42 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?
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/sitemap_generator/utilities.rb', line 128 def blank?(object) # rubocop:disable Metrics/MethodLength 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.
199 200 201 |
# File 'lib/sitemap_generator/utilities.rb', line 199 def bytesize(string) string.respond_to?(:bytesize) ? string.bytesize : string.length end |
.clean_files ⇒ Object
Clean sitemap files in output directory.
30 31 32 33 34 35 36 37 |
# File 'lib/sitemap_generator/utilities.rb', line 30 def clean_files location = SitemapGenerator::Sitemap.sitemap_location dir = location.directory base = SitemapGenerator::Sitemap.filename.to_s return if base.empty? FileUtils.rm(Dir[File.join(dir, "#{base}*.xml.gz"), File.join(dir, "#{base}*.xml")]) end |
.current_time ⇒ Object
Return the current time, using Time.zone if available (Rails), otherwise Time.now.
193 194 195 196 |
# File 'lib/sitemap_generator/utilities.rb', line 193 def current_time zone = defined?(Time.zone) && Time.zone zone ? zone.now : Time.now end |
.ellipsis(string, max) ⇒ Object
Replace the last 3 characters of string with ... if the string is as big or bigger than max.
184 185 186 187 188 189 190 |
# File 'lib/sitemap_generator/utilities.rb', line 184 def ellipsis(string, max) if string.size > max "#{string[0, max - 3] || ''}..." else string end end |
.falsy?(value) ⇒ Boolean
167 168 169 |
# File 'lib/sitemap_generator/utilities.rb', line 167 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.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/sitemap_generator/utilities.rb', line 10 def install_sitemap_rb(verbose = false) # rubocop:disable Style/OptionalBooleanParameter if File.exist?(SitemapGenerator.app.root + 'config/sitemap.rb') # rubocop:disable Style/StringConcatenation 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' # rubocop:disable Style/StringConcatenation ) puts 'created: config/sitemap.rb' if verbose end end |
.present?(object) ⇒ Boolean
An object is present if it's not blank.
144 145 146 |
# File 'lib/sitemap_generator/utilities.rb', line 144 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( = {})
.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.
108 109 110 |
# File 'lib/sitemap_generator/utilities.rb', line 108 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.
114 115 116 |
# File 'lib/sitemap_generator/utilities.rb', line 114 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
84 85 86 87 88 89 90 91 |
# File 'lib/sitemap_generator/utilities.rb', line 84 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.
49 50 51 |
# File 'lib/sitemap_generator/utilities.rb', line 49 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.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/sitemap_generator/utilities.rb', line 55 def symbolize_keys!(hash) hash.keys.each do |key| hash[begin key.to_sym rescue StandardError key end || key] = hash.delete(key) end hash end |
.titleize(string) ⇒ Object
157 158 159 160 161 |
# File 'lib/sitemap_generator/utilities.rb', line 157 def titleize(string) result = string.tr('_', ' ') result.gsub!(/\b\w/, &:upcase) result end |
.truthy?(value) ⇒ Boolean
163 164 165 |
# File 'lib/sitemap_generator/utilities.rb', line 163 def truthy?(value) ['1', 1, 't', 'true', true].include?(value) end |
.uninstall_sitemap_rb ⇒ Object
Remove config/sitemap.rb if exists.
23 24 25 26 27 |
# File 'lib/sitemap_generator/utilities.rb', line 23 def uninstall_sitemap_rb return unless File.exist?(SitemapGenerator.app.root + 'config/sitemap.rb') # rubocop:disable Style/StringConcatenation File.rm(SitemapGenerator.app.root + 'config/sitemap.rb') # rubocop:disable Style/StringConcatenation end |
.with_warnings(flag) ⇒ Object
Sets $VERBOSE for the duration of the block and back to its original value afterwards.
149 150 151 152 153 154 155 |
# File 'lib/sitemap_generator/utilities.rb', line 149 def with_warnings(flag) old_verbose = $VERBOSE $VERBOSE = flag yield ensure $VERBOSE = old_verbose end |