No commit activity in last 3 years
No release in over 3 years
Support for symbol decorations such as where(:field.in => [1,2,3])
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Symbol Decoration

The Symbol Decoration gem provides symbol method extensions to implement DSL such as like where(:field.in => [1,2,3]).

The goal is to allow different ORMs such as Mongoid and NoBrainer to co-exist as both ORMs need such functionality.

Usage

In the following example: where(:field.in => [1,2,3]), the in keyword is a decorator, and decorates the :field symbol.

To register decorators, you may call Symbol::Decoration.register(decorator, ...). For example:

Symbol::Decoration.register(:in)
Symbol::Decoration.register(*%w(in nin eq ne not gt ge gte lt le lte))

Once registered, a decorator can be used with symbol.decorator, which returns a Symbol::Decoration instance.

To support the lowest common denominator, decorators may accept arguments and blocks. For example, :field.gt(5) is valid.

You may retrieve the decoration properties with:

  • decorated_symbol.symbol to get the symbol which is being decorated.
  • decorated_symbol.decorator to get the decoration (for example :gt).
  • decorated_symbol.args to get the decoration arguments.
  • decorated_symbol.blocks to get the decoration block.

Full example:

Symbol::Decoration.register(:gt)

:field.gt.is_a?(Symbol::Decoration) == true
:field.gt.symbol                    == :field
:field.gt.decorator                 == :gt
:field.gt(5).args                   == [5]
:field.gt { 5 }.block.call          == 5

To allow certain decorators to be chainable, you must use the :chainable => true option when registering the decorator. Example:

Symbol::Decoration.register(:any, chainable => true)
Symbol::Decoration.register(:in)

:field.any.in # valid
:field.in.any # invalid

License

Symbol Decoration is MIT Licensed.