Project

liquefied

0.0
No commit activity in last 3 years
No release in over 3 years
Monadic wrapper for values that lets you set up a default finalizer.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.11
~> 5.0
~> 10.0
 Project Readme

Liquefied

Keep your value objects from setting into their final output format.

When formatting values in decorator objects we often want to have a default format for their string output, but we also want to access the original values to do arithmetic or other operations. Liquefied lets you have it both ways:

price = Liquefied.new(12.999) { |val| "$%.2f USD" % val }
price.zero? # false
price > 10  # true 
price + 1   # 13.999

price.to_s  # "$12.99 USD"
puts price  # $12.99 USD 

The method that invokes the block is a finalizer, which unwraps the liquefied value to its output format. The default finalizer is to_s, which makes this pattern helpful for implicit formatting in templates, so <%= price %> outputs the formatted value.

The finalizer can be set to any other method you want.

Casting to the same type as the original will unwrap the original object:

count = Liquefied.new(1234) { "1,234" }
count.to_i  # 1234

Use with ActiveSupport Core Extensions

ActiveSupport extends core Ruby classes with a format option on to_s, which enables a simplified usage pattern instead of specifying a block:

require 'active_support/core_ext'

date = Date.new(2016, 1, 1)

date.to_s         # "2016-01-01"
date.to_s(:long)  # "January 1, 2016"

date = Liquefied.new(date, :long)

date.to_s         # "January 1, 2016"
date.to_s(:short) # "Jan 1, 2016"

Installation

Add this line to your application's Gemfile:

gem 'liquefied'

And then execute:

$ bundle

Or install it yourself as:

$ gem install liquefied

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/avit/liquefied.

License

The gem is available as open source under the terms of the MIT License.