Class: CassandraMapper::Support::SupportCallbacks::CallbackChain

Inherits:
Array
  • Object
show all
Defined in:
lib/cassandra_mapper/support/support_callbacks.rb

Overview

An Array with a compile method

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#extract_options!, wrap

Constructor Details

#initialize(name, config) ⇒ CallbackChain

Returns a new instance of CallbackChain.



338
339
340
341
342
343
344
345
# File 'lib/cassandra_mapper/support/support_callbacks.rb', line 338

def initialize(name, config)
  @name = name
  @config = {
    :terminator => "false",
    :rescuable => false,
    :scope => [ :kind ]
  }.merge(config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



336
337
338
# File 'lib/cassandra_mapper/support/support_callbacks.rb', line 336

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



336
337
338
# File 'lib/cassandra_mapper/support/support_callbacks.rb', line 336

def name
  @name
end

Instance Method Details

#compile(key = nil, object = nil) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/cassandra_mapper/support/support_callbacks.rb', line 347

def compile(key=nil, object=nil)
  method = []
  method << "value = nil"
  method << "halted = false"

  each do |callback|
    method << callback.start(key, object)
  end

  if config[:rescuable]
    method << "rescued_error = nil"
    method << "begin"
  end

  method << "value = yield if block_given? && !halted"

  if config[:rescuable]
    method << "rescue Exception => e"
    method << "rescued_error = e"
    method << "end"
  end

  reverse_each do |callback|
    method << callback.end(key, object)
  end

  method << "raise rescued_error if rescued_error" if config[:rescuable]
  method << "halted ? false : (block_given? ? value : true)"
  method.compact.join("\n")
end