Jeweler::Utils module split-off into a Gem for modularity, easy testing, and the ability to use it in my other projects / share it with others. This is a utility package of various Ruby / Rails Core Extensions, helpers extracted from Jeweler and code to implement Ruby Code-Design Patterns that we use for Jeweler.
GitHub Repo
Usage
Service Object Pattern
- Returns
result
method which is set to the value of the result of the Call
method
- Returns any errors in the
@errors
variable with initialized as an empty Array. Place error messages here.
- These can optionally be logged as a warning.
- Fatal errors can be stored in
@fatals
Array (initialized empty).
- This automatically enters the generic warning message
There was an error. Contact Support.
into @errors
- These automatically get logged as errors.
- Succesful message is stored in
@result_message
, initialized as an empty String ''
Example:
class UpdateTask < Jeweler::Service
def initialize(task)
@task = task
end
def call
if task.update(hours: 4)
Note.create_event(task.sprint.project, 'task_deleted', 'Deleted: ' + task.description)
else
task.errors.full_messages.map { |e| fatals << 'Error Destroying Task: ' + e }
end
task
end
private
attr_reader :task
end
Usage:
@service_object = DestroyTask.call(@task)
@service_object.errors # Combined array of fatals and errors, logged automatically.
@service_object.result # Task<id: 7, hours: 3...>
Errors and Fatals example:
class ServiceObject1 < Jeweler::Service
def call
errors << 'error 1'
errors << 'error 2'
fatals << 'fatal 1'
end
end
so = ServiceObject1.call
ServiceObject1.errors = ['error 1', 'error 2', 'There was an error. Contact Support.']
String
true?
- returns true if the string is explicitly equal to 'true'
'true'.true? # returns true ONLY
'truue'.true? # returns false
.to_b
- converts the string 'true' and 'false' explicitly / respectively to TRUE / FALSE, otherwise it will throw an exception.
'true'.to_b # TRUE
'false'.to_b # FALSE
'faalse'.to_b # Throws an Exception
String, BigDecimal, Decimal, Float, Integer
.money
- will convert this to a formatted USD money String, rounding it to 0 decimal places if it is a whole number
(500.84).money # '$500.84'
(500.00).money # '$500'
BigDecimal, Decimal, Float, Integer, Double
(Double does not have a rate)
.prettify
- Rounds the number to 0 decimal places if it is a whole number anyway
(500.00).prettify # 500
(500.87).prettify # 500.87
.rate
- Converts a number to an hourly-rate in USD ($/hr) (String)
(55.50) # '$55.50/hr'
.hours
- Appends hours or hour to the end of a number, converts it to String
(5).hours # '5 hrs'
(1).hours # '1 hr'
NilClass
These, as well as the methods above are for displaying these numbers in the UI. I prefer these cleaner methods to the decorator pattern (in these few use-cases), because it is really really clean.
(nil).prettify
- returns 0 (Integer)
(nil).hours
- returns '0 hr.' (String)
(nil).money
- returns '$0' (String)
(nil).true?
- returns False (Boolean)
(nil).to_b
- returns False (Boolean)
Array
.log_errors(prefix_msg = nil)
- Logs all of these errors, as errors, prefixed with the param (optionally)
sprint.errors.full_messages.log_errors('Error Saving Sprint')
# log.warn - Error Saving Sprint: the validation error while saving sprint
sprint.errors.full_messages.log_errors
# log.warn - the validation error while saving sprint
Installation
Add this line to your application's Gemfile:
gem 'jeweler-utils', source: 'git@github.com:vaskaloidis/jeweler-utils.git'
And then execute:
$ bundle
Or install it yourself as:
$ gem install jeweler-utils
Contributing
Just submit a pull request with your modifications and I will take a look.
License
The gem is available as open source under the terms of the MIT License.