module Sequel::Plugins::AssociationMultiAddRemove::ClassMethods

  1. lib/sequel/plugins/association_multi_add_remove.rb

Methods

Public Instance

  1. def_association_instance_methods

Public Instance methods

def_association_instance_methods (opts)

Define the methods use to add/remove/set multiple associated objects in a single method call.

[show source]
   # File lib/sequel/plugins/association_multi_add_remove.rb
44 def def_association_instance_methods(opts)
45   super
46 
47   if opts[:adder]
48     add_method = opts[:add_method]
49     multi_add_method = opts[:multi_add_method] || :"add_#{opts[:name]}"
50     multi_add_method = nil if add_method == multi_add_method
51     if multi_add_method
52       association_module_def(multi_add_method, opts) do |objs, *args|
53         db.transaction(:server=>@server){objs.map{|obj| send(add_method, obj, *args)}.compact}
54       end
55     end
56   end
57 
58   if opts[:remover]
59     remove_method = opts[:remove_method]
60     multi_remove_method = opts[:multi_remove_method] || :"remove_#{opts[:name]}"
61     multi_remove_method = nil if remove_method == multi_remove_method
62     if multi_remove_method
63       association_module_def(multi_remove_method, opts) do |objs, *args|
64         db.transaction(:server=>@server){objs.map{|obj| send(remove_method, obj, *args)}.compact}
65       end
66     end
67   end
68 
69   if multi_add_method && multi_remove_method
70     association_module_def(:"#{opts[:name]}=", opts) do |objs, *args|
71       db.transaction(:server=>@server) do
72         existing_objs = send(opts.association_method)
73         send(multi_remove_method, (existing_objs - objs), *args)
74         send(multi_add_method, (objs - existing_objs), *args)
75         nil
76       end
77     end
78   end
79 end