Module: Datamappify::Repository::QueryMethod::Callbacks

Defined in:
lib/datamappify/repository/query_method/callbacks.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/datamappify/repository/query_method/callbacks.rb', line 7

def self.included(klass)
  klass.class_eval do
    include Hooks

    define_hooks :before_create,  :after_create,
                 :before_update,  :after_update,
                 :before_save,    :after_save,
                 :before_destroy, :after_destroy,
                 :halts_on_falsey => true
  end
end

Instance Method Details

#hook_for(type, filter) ⇒ Symbol (private)

Parameters:

  • type (Symbol)

    e.g. :create, :update, :save or :destroy

  • filter (Symbol)

    e.g. :before or :after

Returns:

  • (Symbol)


58
59
60
# File 'lib/datamappify/repository/query_method/callbacks.rb', line 58

def hook_for(type, filter)
  :"#{filter}_#{type}"
end

#run_callbacks(entity, *types) { ... } ⇒ void

This method returns an undefined value.

Parameters:

  • entity (Entity)
  • types (Symbol)

    e.g. :create, :update, :save or :destroy

Yields:

  • callback



27
28
29
30
31
32
# File 'lib/datamappify/repository/query_method/callbacks.rb', line 27

def run_callbacks(entity, *types, &block)
  run_hooks(types, :before, entity) &&
    (yield_value = block.call) &&
    run_hooks(types.reverse, :after, entity) &&
    yield_value
end

#run_hooks(types, filter, entity) ⇒ Boolean (private)

Parameters:

  • types (Array<Symbol] an array of types (e.g. :create, :update, :save or :destroy))

    ypes [Array<Symbol] an array of types (e.g. :create, :update, :save or :destroy)

  • filter (Symbol)

    e.g. :before or :after

  • entity (Entity)

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/datamappify/repository/query_method/callbacks.rb', line 45

def run_hooks(types, filter, entity)
  types.take_while do |type|
    run_hook(hook_for(type, filter), entity).not_halted?
  end.length == types.length
end