Class ActiveRecord::ConnectionAdapters::TriggerDefinition
In: lib/rails_pg_procs.rb
Parent: Object

Methods

Constants

CLEAN = 0b0
ROW = 0b00001
BEFORE = 0b00010
INSERT = 0b00100
DELETE = 0b01000
UPDATE = 0b10000

Attributes

binary_type  [R] 
id  [RW] 
name  [RW] 
procedure_name  [RW] 
table  [RW] 

Public Class methods

[Source]

     # File lib/rails_pg_procs.rb, line 147
147:       def initialize(id, table, name=nil, binary_type=[], procedure_name=nil)
148:         puts "id #{id.inspect} table #{table.inspect} name #{name.inspect} binary_type #{binary_type.inspect} procedure_name #{procedure_name.inspect}" if DEBUG
149:         @id                 = id
150:         @table              = table
151:         self.binary_type    = binary_type
152:         self.name           = (name || triggerized_name)
153:         self.procedure_name = (procedure_name || name || triggerized_name)
154:         puts "id #{self.id.inspect} table #{self.table.inspect} name #{self.name.inspect} binary_type #{self.binary_type.inspect} procedure_name #{self.procedure_name.inspect}" if DEBUG
155:       end

Public Instance methods

[Source]

     # File lib/rails_pg_procs.rb, line 203
203:           def before?
204:             calc(BEFORE)
205:           end

[Source]

     # File lib/rails_pg_procs.rb, line 168
168:       def binary_type=(*types)
169: #        print "types #{types.inspect} types[0] #{types[0].inspect} " if DEBUG
170:         case types[0]
171:           when Fixnum, Array
172:             @binary_type = bin_typ(types[0])
173:           else
174:             @binary_type = bin_typ(types)
175:         end
176:       end

[Source]

     # File lib/rails_pg_procs.rb, line 207
207:           def row?
208:             calc(ROW)
209:                 end

that‘s to_r(uby)d(efinition)l(anguage)

[Source]

     # File lib/rails_pg_procs.rb, line 158
158:       def to_rdl() 
159:         "  add_trigger(#{Inflector.symbolize(table)}" <<
160:         ", [" + events.join(", ") + "]" <<
161:         (      before? ? ", :before => true"  : "") <<
162:         (         row? ? ", :row => true"     : "") <<
163:         (!triggerized? ? ", :name => #{Inflector.symbolize(name)}" : "") <<
164:         (!triggerized?(procedure_name) ? ", :function => #{Inflector.symbolize(procedure_name)}" : "") <<
165:         ")"
166:       end

TODO —

[Source]

     # File lib/rails_pg_procs.rb, line 179
179:           def to_sql_create()
180:                   result = "CREATE TRIGGER "          << 
181:         name.to_sql_name                  << 
182:         (before? ? " BEFORE" : " AFTER")  <<
183:         " "                               << 
184:         (
185:           events.collect {|event| 
186:             event.to_s.upcase.gsub(/^:/, '') }.join(" OR ")
187:         ) <<
188:         " ON "                            << 
189:         table.to_sql_name                 << 
190:         " FOR EACH "                      << 
191:         (row? ? "ROW" : "STATEMENT")      << 
192:         " EXECUTE PROCEDURE "             << 
193:         procedure_name.to_s               << 
194:         "();"
195:       result
196:           end

[Source]

     # File lib/rails_pg_procs.rb, line 198
198:     def triggerized?(nam=nil)
199:       nam ||= self.name
200:       triggerized_name == nam
201:     end

Private Instance methods

[Source]

     # File lib/rails_pg_procs.rb, line 229
229:       def bin_typ(typs)
230: #        puts "typs #{typs.inspect} typs.class #{typs.class}" if DEBUG
231:         case typs
232:           when Fixnum
233:             return typs
234:           when Symbol
235:             return bin_typ(typs.to_s)
236:           when String
237:             return typs.to_i if typs =~ /^\d+$/
238:             return self.class.const_get(typs.upcase.to_sym)
239:           when Array
240:             ctype = 0
241:             typs.each {|typ| 
242:               ctype += bin_typ(typ)
243:             }
244:         end
245:         ctype
246:       end

[Source]

     # File lib/rails_pg_procs.rb, line 225
225:       def calc(bin)
226:         eval(sprintf("0b%0.8b", self.binary_type())) & bin > 0
227:       end

[Source]

     # File lib/rails_pg_procs.rb, line 217
217:       def events
218:         events = []
219:         events.push(":insert") if calc(INSERT)
220:         events.push(":update") if calc(UPDATE)
221:         events.push(":delete") if calc(DELETE)
222:         events
223:       end

[Source]

     # File lib/rails_pg_procs.rb, line 213
213:       def triggerized_name
214:         Inflector.triggerize(table, events, calc(BEFORE))
215:       end

[Validate]