Module: Sequel::Plugins::Composition

Defined in:
lib/sequel/plugins/composition.rb

Overview

The composition plugin allows you to easily define getter and setter instance methods for a class where the backing data is composed of other getters and decomposed to other setters.

A simple example of this is when you have a database table with separate columns for year, month, and day, but where you want to deal with Date objects in your ruby code. This can be handled with:

Album.plugin :composition
Album.composition :date, :mapping=>[:year, :month, :day]

The :mapping option is optional, but you can define custom composition and decomposition procs via the :composer and :decomposer options.

Note that when using the composition object, you should not modify the underlying columns if you are also instantiating the composition, as otherwise the composition object values will override any underlying columns when the object is saved.

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.apply(model) ⇒ Object

Define the necessary class instance variables.



25
26
27
# File 'lib/sequel/plugins/composition.rb', line 25

def self.apply(model)
  model.instance_eval{@compositions = {}}
end