Module | Sequel::Plugins::Composition::ClassMethods
In: | lib/sequel/plugins/composition.rb

### Methods

composition define_composition_accessor inherited

### Attributes

composition_module | [R] | A module included in the class holding the composition getter and setter methods.
compositions | [R] | A hash with composition name keys and composition reflection hash values.

### Public Instance methods

composition(name, opts={})

Define a composition for this model, with name being the name of the composition. You must provide either a :mapping option or both the :composer and :decomposer options.

Options:

- :class - if using the :mapping option, the class to use, as a Class, String or Symbol.
- :composer - A proc that is instance evaled when the composition getter method is called to create the composition.
- :decomposer - A proc that is instance evaled before saving the model object, if the composition object exists, which sets the columns in the model object based on the value of the composition object.
- :mapping - An array where each element is either a symbol or an array of two symbols. A symbol is treated like an array of two symbols where both symbols are the same. The first symbol represents the getter method in the model, and the second symbol represents the getter method in the composition object. Example:

        # Uses columns year, month, and day in the current model
        # Uses year, month, and day methods in the composition object
        :mapping=>[:year, :month, :day]
        # Uses columns year, month, and day in the current model
        # Uses y, m, and d methods in the composition object where
        # for example y in the composition object represents year
        # in the model object.
        :mapping=>[[:year, :y], [:month, :m], [:day, :d]]

[Source]

        # File lib/sequel/plugins/composition.rb, line 60
    60:         def composition(name, opts={})
    61:           opts = opts.dup
    62:           compositions[name] = opts
    63:           if mapping = opts[:mapping]
    64:             keys = mapping.map{|k| k.is_a?(Array) ? k.first : k}
    65:             if !opts[:composer]
    66:               late_binding_class_option(opts, name)
    67:               klass = opts[:class]
    68:               class_proc = proc{klass || constantize(opts[:class_name])}
    69:               opts[:composer] = proc do
    70:                 if values = keys.map{|k| send(k)} and values.any?{|v| !v.nil?}
    71:                   class_proc.call.new(*values)
    72:                 else
    73:                   nil
    74:                 end
    75:               end
    76:             end
    77:             if !opts[:decomposer]
    78:               setter_meths = keys.map{|k| "#{k}=""#{k}="}
    79:               cov_methods = mapping.map{|k| k.is_a?(Array) ? k.last : k}
    80:               setters = setter_meths.zip(cov_methods)
    81:               opts[:decomposer] = proc do
    82:                 if (o = compositions[name]).nil?
    83:                   setter_meths.each{|sm| send(sm, nil)}
    84:                 else
    85:                   setters.each{|sm, cm| send(sm, o.send(cm))}
    86:                 end
    87:               end
    88:             end
    89:           end
    90:           raise(Error, "Must provide :composer and :decomposer options, or :mapping option") unless opts[:composer] && opts[:decomposer]
    91:           define_composition_accessor(name, opts)
    92:         end

define_composition_accessor(name, opts={})

Define getter and setter methods for the composition object.

[Source]

         # File lib/sequel/plugins/composition.rb, line 102
    102:         def define_composition_accessor(name, opts={})
    103:           include(@composition_module ||= Module.new) unless composition_module
    104:           composer = opts[:composer]
    105:           composition_module.class_eval do
    106:             define_method(name) do
    107:               compositions.include?(name) ? compositions[name] : (compositions[name] = instance_eval(&composer))
    108:             end
    109:             define_method("#{name}=") do |v|
    110:               modified!
    111:               compositions[name] = v
    112:             end
    113:           end
    114:         end

inherited(subclass)

Copy the necessary class instance variables to the subclass.

[Source]

        # File lib/sequel/plugins/composition.rb, line 95
    95:         def inherited(subclass)
    96:           super
    97:           c = compositions.dup
    98:           subclass.instance_eval{@compositions = c}
    99:         end

[Validate]