Class: CustomActiveRecordObserver::DSL
- Inherits:
-
Object
- Object
- CustomActiveRecordObserver::DSL
- Defined in:
- lib/custom_active_record_observer/dsl.rb
Overview
A main methods that are supposed to be used within CustomActiveRecordObserver
Instance Attribute Summary collapse
-
#actions_and_rules ⇒ Object
readonly
Returns the value of attribute actions_and_rules.
Instance Method Summary collapse
-
#initialize(block) ⇒ DSL
constructor
A new instance of DSL.
-
#on_add(*attributes, &block) ⇒ Object
Executes the code inside a block when the specific attributes are updated from
nilto somenot nilvalue. -
#on_change(*attributes, &block) ⇒ Object
Executes the code inside a block when the specific attributes are updated from some
not nilto anothernot nil. -
#on_create(&block) ⇒ Object
Executes the code inside a block after the record is created.
-
#on_destroy(&block) ⇒ Object
Executes the code inside a block after the record is destroyed.
-
#on_remove(*attributes, &block) ⇒ Object
Executes the code inside a block when the specific attributes are updated from some
not nilvalue tonil. -
#on_update(*attributes, &block) ⇒ Object
Launches code inside a block if an object changes any of attributes according to the pattern which is described in the brackets (value before -> value after).
Constructor Details
#initialize(block) ⇒ DSL
Returns a new instance of DSL.
10 11 12 13 14 |
# File 'lib/custom_active_record_observer/dsl.rb', line 10 def initialize(block) @actions_and_rules = [] instance_exec(&block) end |
Instance Attribute Details
#actions_and_rules ⇒ Object (readonly)
Returns the value of attribute actions_and_rules.
7 8 9 |
# File 'lib/custom_active_record_observer/dsl.rb', line 7 def actions_and_rules @actions_and_rules end |
Instance Method Details
#on_add(*attributes, &block) ⇒ Object
Executes the code inside a block when the specific attributes are updated from nil to some not nil value
62 63 64 |
# File 'lib/custom_active_record_observer/dsl.rb', line 62 def on_add(*attributes, &block) on_update(*attributes.push([nil, NotNil]), &block) end |
#on_change(*attributes, &block) ⇒ Object
Executes the code inside a block when the specific attributes are updated from some not nil to another not nil
82 83 84 |
# File 'lib/custom_active_record_observer/dsl.rb', line 82 def on_change(*attributes, &block) on_update(*attributes.push([NotNil, NotNil]), &block) end |
#on_create(&block) ⇒ Object
Executes the code inside a block after the record is created
17 18 19 |
# File 'lib/custom_active_record_observer/dsl.rb', line 17 def on_create(&block) store(:create, Rules::CreateRule.new(block)) end |
#on_destroy(&block) ⇒ Object
Executes the code inside a block after the record is destroyed
22 23 24 |
# File 'lib/custom_active_record_observer/dsl.rb', line 22 def on_destroy(&block) store(:destroy, Rules::DestroyRule.new(block)) end |
#on_remove(*attributes, &block) ⇒ Object
Executes the code inside a block when the specific attributes are updated from some not nil value to nil
72 73 74 |
# File 'lib/custom_active_record_observer/dsl.rb', line 72 def on_remove(*attributes, &block) on_update(*attributes.push([NotNil, nil]), &block) end |
#on_update(*attributes, &block) ⇒ Object
Launches code inside a block if an object changes any of attributes according to the pattern which is described in the brackets (value before -> value after)
This uses ‘===` method to compare values. Will be launched only if user_group_id was changed from `nil` to anything but nil. in the array [] there are two values that are compared with the real changes to detect if they maches.
48 49 50 51 52 53 54 |
# File 'lib/custom_active_record_observer/dsl.rb', line 48 def on_update(*attributes, &block) pattern = attributes.pop if attributes.last.is_a?(Array) attributes.each do |attribute| store(:update, Rules::UpdateRule.new(block, attribute: attribute, pattern: pattern)) end end |