No commit activity in last 3 years
No release in over 3 years
This module defines an YAMLColumn coder that returns nil instead of a blank object. Useful when the class you are serializing to/from does not allow uninitialized objects (such as a unit measurement)
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 10.0
~> 2.0

Runtime

 Project Readme

nil-returning-coder

This gem supplies an YAMLColumn coder that returns nil instead of a blank object. Useful when the class you are serializing to/from does not allow uninitialized objects (such as a unit measurement)

Why?

Suppose you have this:

class Sandwich < ActiveRecord::Base
  attr_accessible :length
  serialize :length, Unit
end

This happens:

2.0.0-p247 :002 > Sandwich.new
ArgumentError: Invalid Unit Format

Because ActiveRecord is trying to do this:

2.0.0-p247 :003 > Unit.new
ArgumentError: Invalid Unit Format

So in config/initializers/ruby-units.rb

Unit.instance_eval do
  include NilReturningCoder
end

Now your shit works

2.0.0-p247 :002 > Sandwich.new
 => #<Sandwich id: nil, length: nil>