lib/materialist/materializer.rb in materialist-2.2.0 vs lib/materialist/materializer.rb in materialist-2.3.0

- old
+ new

@@ -83,17 +83,25 @@ def source_key(key, &url_parser_block) __materialist_options[:source_key] = key __materialist_options[:url_parser] = url_parser_block end + def before_upsert(*method_array) + __materialist_options[:before_upsert] = method_array + end + def after_upsert(*method_array) __materialist_options[:after_upsert] = method_array end def after_destroy(*method_array) __materialist_options[:after_destroy] = method_array end + + def before_destroy(*method_array) + __materialist_options[:before_destroy] = method_array + end end class Materializer def initialize(url, klass) @@ -124,10 +132,11 @@ end def destroy return unless materialize_self? model_class.find_by(source_lookup(url)).tap do |entity| + send_messages(before_destroy, entity) unless before_destroy.nil? entity.destroy!.tap do |entity| send_messages(after_destroy, entity) unless after_destroy.nil? end if entity end end @@ -140,10 +149,11 @@ options.include? :model_class end def upsert_record model_class.find_or_initialize_by(source_lookup(url)).tap do |entity| + send_messages(before_upsert, entity) unless before_upsert.nil? entity.update_attributes attributes entity.save! end end @@ -167,11 +177,19 @@ def mapping options.fetch :mapping end + def before_upsert + options[:before_upsert] + end + def after_upsert options[:after_upsert] + end + + def before_destroy + options[:before_destroy] end def after_destroy options[:after_destroy] end