Project

simplifier

0.0
No commit activity in last 3 years
No release in over 3 years
Reduce objects to a simpler representation.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

 Project Readme

Simplifier

A Ruby library for simplifying object graphs.

require "simplifier"

Simplifier.new.simplify({ :foo => [:bar, "baz", Time.now] })
# => {"foo"=>["bar", "baz", "2013-06-11T00:03:27Z"]}

By default

  • nil, true, and false are themselves.
  • Symbols are simplified into interned, frozen Strings.
  • Strings are UTF-8 with \n newlines.
  • Strings in the BINARY encoding are themselves.
  • Arrays and Sets are simplified into Arrays of simplified elements.
  • Hashes are simplified into Hashes of simplified keys and values.
  • Dates become an ISO 8601 "yyyy-mm-dd" String.
  • DateTimes and Times become an ISO 8601 "yyyy-mm-ddThh:mm:ssZ" String.
  • Any other types raise Simplifier::Unknown.

Extending

require "simplifier"

class APISimplifier < Simplifier
  def simplify(object)
    case object
    when MyApp::User
      simplify :id => object.id, :name => object.name

    else
      super
    end
  end
end

The constructor for Simplifier takes an optional configuration Hash and assigns it to the options attribute. This can be useful if you need to change the simplification of a type depending on embedding or other factors.

Maintainer

@jbarnette