WebUtils
WebUtils is basically a collection of useful helper
methods that are quite common to have in a web project.
It is organized like Rack::Utils and actually extends it.
Some of the methods are similar to methods you would have
in Active::Support but without monkey patching. Although it
is only a coincidence. The purpose is not to build an alternative.
Here is how you would use it with Sinatra:
require 'sinatra/base'
require 'web_utils'
class Main < Sinatra::Base
# Your frontend code...
helpers do
include WebUtils
# Your other helpers...
end
end
Some methods are also useful on the model side.
Here is a list of the available methods and what they do:
blank?(string)
Just tells you if the string is blank or not.
pluralize(string)
Pluralize simple words. Just override when necessary.
singularize(string)
Singularize simple words. Just override when necessary.
dasherize_class_name(string)
Dasherize class names. Module separator is a double dash.
So BlogArticle::Comment becomes blog-article--comment.
Also for simplicity, it does not gather accronyms. e.g. net--f-t-p.
This is useful for urls or creating CSS class names or IDs.
undasherize_class_name(string)
Basically the opposite.
resolve_class_name(string, context=Kernel)
It takes the class name as a string and returns the class.
You can pass a class name with modules as well (e.g. "Net::FTP").
This is actually the main reason why there is a context
argument, because it uses recursion to do this.
But context is still useful otherwise.
resolve_dasherized_class_name(string)
Same except that it takes the dasherized version of the class name as an argument and returns the class itself (not a string). Useful for resolving a class from a URL param.
guess_related_class_name(parent_class, string)
It is mainly used for guessing the class name of a
children class with a plural name.
So guess_related_class_name(BlogArticle, :comments)
will return 'BlogArticle::Comment'.
get_value(value, target=Kernel)
It is used for example for getting a default value for something and it is passed either as:
- A direct value (e.g.
"John Doe") - A proc (e.g.
proc{ Time.now }) - A symbol (e.g.
:get_last_value)
In the case of a symbol, the message is called on the target. Therefore you would always give the target just in case it is a symbol.
If the value can only be direct or a Proc, you can ignore the second argument (target).
deep_copy(object)
This makes a deeper copy of an object, since dup does not
duplicate nested objects in a hash for example. It uses a simple
marshal/unmarshal mechanism.
It is a bit of a hack and not that web-specific, but useful if you want to avoid some nasty bugs.
ensure_key!(hash, key, default_value)
If the hash does not have the key, it sets it with the default value. And this value is also returned by the method.
ensure_key(hash, key, default_value)
Same as ensure_key! except that it does not change the original
hash. It returns a new one.
slugify(string, force_lowercase=true)
This makes the strings ready to be used as a slug in a URL.
It removes the accents, replaces a lot of separators with
dashes and escapes it. By default it forces the output to
be lowercase, but if you pass false as a second argument,
it will not change the case of letters.
label_for_field(string_or_symbol)
Returns a human readable version of a field name.
It says field, but it could be any kind of symbol I guess.
each_stub(nested_object) {|object,key_or_index,value| ... }
It is used to run something on all the nested stubs of an array or a hash. The second argument of the block is either a key if the object is a hash, or an index if the object is an array.
automatic_typecast(string, casted=[:bool, :nil, :int, :float])
It tries to change a string value received by an HTML form
or a CSV file into an object when it can. So far it recognizes
simple things like true, false, integers and floats.
And an empty string is always nil.
The second argument is the list of things you want to typecast.
By default there is everything, but you only want to typecast
integers and floats, you can pass [:int, :float].
generate_random_id(size)
Like the name suggests, it generates a random string of only letters and numbers. If you don't provide a size, it defaults to 16.
nl2br(string, br="<br>")
The classic nl2br which makes sure return lines are
turned into <br> tags. You can use the second argument if
you want to specify what the replacement tag should be.
Just in case you want self-closing tags.
complete_link(string)
This just makes sure that a link is complete. Very often
people tend to enter a URL like www.google.com which is a
controversial href for some browsers, so it changes it to
//www.google.com. Already seemingly complete links are not
affected by the method.
external_link?(string)
This tells you if a link is pointing to the current site or
an external one. This is useful when you want to create a link
tag and want to decide if target is '_blank' or '_self'.
automatic_html(string, br="<br>")
This automatically does nl2br and links recognizable things
like email addresses and URLs. Not as good as markdown, but it
is quite useful, should it be only for turning an email into a link.
truncate(string, size=320, ellipsis="...")
It truncates a string like what you have in blog summaries.
It automatically removes tags and line breaks. The length is
320 by default. When the original string was longer, it puts
an ellipsis at the end which can be replaced by whatever you put
as a 3rd argument. e.g. '...and more'.
display_price(int)
It changes a price in cents/pence into a formated string
like 49,425.40 when you pass 4942540.
parse_price(string)
It does the opposite of display_price and parses a string in
order to return a price in cents/pence.
branded_filename(path, brand="WebUtils")
It takes the path to a file and add the brand/prefix and a dash
before the file name (really the file name, not the path).
By default, the brand/prefix is WebUtils.
filename_variation(path, variation, ext)
For example you have a file /path/to/image.jpg and you want
to create its thumbnail in png, you can create the thumnail
path with filename_variation(path, :thumbnail, :png) and it
will return /path/to/image.thumbnail.png.
initial_request?(request)
You basically pass the Request object to the method and it
looks at the referrer and returns true if it was not on the same
domain. Essentially tells you if the visitor just arrived.