ontoportal-lirmm / goo

Graph Oriented Objects (GOO) for Ruby. A RDF/SPARQL based ORM.
http://ncbo.github.io/goo/
Other
0 stars 4 forks source link

Feature: Add after_save and after_destroy hooks to models #53

Closed syphax-bouazzouni closed 9 months ago

syphax-bouazzouni commented 9 months ago

Changes

Added two dsl to the setting of a model, after_save(list_methods) and after_destroy(list_methods) that will be called when a model is saved or deleted, calling and running each of the method listed as argument in the hook declaration

Example

class TestHookModel < Goo::Base::Resource
  model :test_hook, name_with: lambda { |s| RDF::URI.new("http://example.org/test/#{rand(1000)}") }
  after_save :update_count, :update_count_2
  after_destroy :decrease_count_2

  attribute :name, enforce: [:existence, :unique]
  attr_reader :count, :count2

  def update_count
    @count ||= 0
    @count += 1
  end

  def update_count_2
    @count2 ||= 0
    @count2 += 2
  end

  def decrease_count_2
    @count2 -= 2
  end
end

model = TestHookModel.new(name: "test").save # create the model and call update_count, :update_count_2
model.count # => 1
model.count2 # => 2

model.delete # delete from the triple store the model and call :decrease_count_2
model.count # => 1
model.count2 # => 0
codecov[bot] commented 9 months ago

Codecov Report

Attention: 14 lines in your changes are missing coverage. Please review.

Comparison is base (df1a7bb) 86.19% compared to head (51cf285) 85.87%.

Files Patch % Lines
lib/goo/base/settings/hooks.rb 80.00% 6 Missing :warning:
lib/goo/utils/callbacks_utils.rb 45.45% 6 Missing :warning:
lib/goo/base/resource.rb 50.00% 2 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## development #53 +/- ## =============================================== - Coverage 86.19% 85.87% -0.32% =============================================== Files 39 41 +2 Lines 2672 2712 +40 =============================================== + Hits 2303 2329 +26 - Misses 369 383 +14 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.