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]]
# File lib/sequel/plugins/composition.rb, line 93 93: def composition(name, opts={}) 94: opts = opts.dup 95: compositions[name] = opts 96: if mapping = opts[:mapping] 97: keys = mapping.map{|k| k.is_a?(Array) ? k.first : k} 98: if !opts[:composer] 99: late_binding_class_option(opts, name) 100: klass = opts[:class] 101: class_proc = proc{klass || constantize(opts[:class_name])} 102: opts[:composer] = proc do 103: if values = keys.map{|k| send(k)} and values.any?{|v| !v.nil?} 104: class_proc.call.new(*values) 105: else 106: nil 107: end 108: end 109: end 110: if !opts[:decomposer] 111: setter_meths = keys.map{|k| :"#{k}="} 112: cov_methods = mapping.map{|k| k.is_a?(Array) ? k.last : k} 113: setters = setter_meths.zip(cov_methods) 114: opts[:decomposer] = proc do 115: if (o = compositions[name]).nil? 116: setter_meths.each{|sm| send(sm, nil)} 117: else 118: setters.each{|sm, cm| send(sm, o.send(cm))} 119: end 120: end 121: end 122: end 123: raise(Error, "Must provide :composer and :decomposer options, or :mapping option") unless opts[:composer] && opts[:decomposer] 124: define_composition_accessor(name, opts) 125: end
Define getter and setter methods for the composition object.
# File lib/sequel/plugins/composition.rb, line 135 135: def define_composition_accessor(name, opts={}) 136: include(@composition_module ||= Module.new) unless composition_module 137: composer = opts[:composer] 138: composition_module.class_eval do 139: define_method(name) do 140: compositions.include?(name) ? compositions[name] : (compositions[name] = instance_eval(&composer)) 141: end 142: define_method("#{name}=") do |v| 143: modified! 144: compositions[name] = v 145: end 146: end 147: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.