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.
-
.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.
168 169 170 171 172 173 174 175 |
# File 'lib/sitemap_generator/utilities.rb', line 168 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.
63 64 65 66 67 68 69 70 71 |
# File 'lib/sitemap_generator/utilities.rb', line 63 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.
37 38 39 40 |
# File 'lib/sitemap_generator/utilities.rb', line 37 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?
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/sitemap_generator/utilities.rb', line 123 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.
188 189 190 |
# File 'lib/sitemap_generator/utilities.rb', line 188 def bytesize(string) string.respond_to?(:bytesize) ? string.bytesize : string.length end |
.clean_files ⇒ Object
Clean sitemap files in output directory.
30 31 32 |
# File 'lib/sitemap_generator/utilities.rb', line 30 def clean_files FileUtils.rm(Dir[SitemapGenerator.app.root + 'public/sitemap*.xml.gz']) # rubocop:disable Style/StringConcatenation end |
.ellipsis(string, max) ⇒ Object
Replace the last 3 characters of string with ... if the string is as big or bigger than max.
179 180 181 182 183 184 185 |
# File 'lib/sitemap_generator/utilities.rb', line 179 def ellipsis(string, max) if string.size > max "#{string[0, max - 3] || ''}..." else string end end |
.falsy?(value) ⇒ Boolean
162 163 164 |
# File 'lib/sitemap_generator/utilities.rb', line 162 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.
139 140 141 |
# File 'lib/sitemap_generator/utilities.rb', line 139 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.
103 104 105 |
# File 'lib/sitemap_generator/utilities.rb', line 103 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.
109 110 111 |
# File 'lib/sitemap_generator/utilities.rb', line 109 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
79 80 81 82 83 84 85 86 |
# File 'lib/sitemap_generator/utilities.rb', line 79 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.
44 45 46 |
# File 'lib/sitemap_generator/utilities.rb', line 44 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.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sitemap_generator/utilities.rb', line 50 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
152 153 154 155 156 |
# File 'lib/sitemap_generator/utilities.rb', line 152 def titleize(string) result = string.tr('_', ' ') result.gsub!(/\b\w/, &:upcase) result end |
.truthy?(value) ⇒ Boolean
158 159 160 |
# File 'lib/sitemap_generator/utilities.rb', line 158 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.
144 145 146 147 148 149 150 |
# File 'lib/sitemap_generator/utilities.rb', line 144 def with_warnings(flag) old_verbose = $VERBOSE $VERBOSE = flag yield ensure $VERBOSE = old_verbose end |