Class: Harmonize::Strategies::BasicCrudStrategy::Modifier
- Inherits:
-
Object
- Object
- Harmonize::Strategies::BasicCrudStrategy::Modifier
- Defined in:
- lib/harmonize/strategies/basic_crud_strategy.rb
Class Method Summary collapse
Instance Method Summary collapse
- #create_log_entry ⇒ Object
- #destroy ⇒ Object
- #find_or_initialize_target ⇒ Object
-
#initialize(source, target, strategy) ⇒ Modifier
constructor
A new instance of Modifier.
- #log ⇒ Object
- #model_errors ⇒ Object
- #modify ⇒ Object
- #save_target ⇒ Object
- #update_target_attributes ⇒ Object
Constructor Details
#initialize(source, target, strategy) ⇒ Modifier
Returns a new instance of Modifier.
51 52 53 54 55 56 57 58 59 |
# File 'lib/harmonize/strategies/basic_crud_strategy.rb', line 51 def initialize(source, target, strategy) @source = source @target = target @error = nil # any error that occured during modification @type = nil # [ create, update, destroy ] @targets = strategy.targets @log = strategy.harmonize_log @key = strategy.harmonizer.key end |
Class Method Details
.destroy(target, strategy) ⇒ Object
47 48 49 |
# File 'lib/harmonize/strategies/basic_crud_strategy.rb', line 47 def self.destroy(target, strategy) new(nil, target, strategy).destroy end |
.modify(source, strategy) ⇒ Object
43 44 45 |
# File 'lib/harmonize/strategies/basic_crud_strategy.rb', line 43 def self.modify(source, strategy) new(source, nil, strategy).modify end |
Instance Method Details
#create_log_entry ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/harmonize/strategies/basic_crud_strategy.rb', line 113 def create_log_entry @log.modifications.create( :instance_id => @target.id, :modification_type => @type, :instance_errors => @error, :before_time => @before, :after_time => @after ) end |
#destroy ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/harmonize/strategies/basic_crud_strategy.rb', line 69 def destroy log do @type = 'destroy' @target.destroy @modified = true end end |
#find_or_initialize_target ⇒ Object
77 78 79 80 81 |
# File 'lib/harmonize/strategies/basic_crud_strategy.rb', line 77 def find_or_initialize_target target_relation = @targets.where( @key => @source[@key] ) @target = target_relation.first || target_relation.build @type = @target.new_record? ? 'create' : 'update' end |
#log ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/harmonize/strategies/basic_crud_strategy.rb', line 94 def log @before = DateTime.now yield @after = DateTime.now rescue *model_errors => e @error = e. ensure if @error @type = 'error' @after = nil end create_log_entry if @modified || @error end |
#model_errors ⇒ Object
108 109 110 111 |
# File 'lib/harmonize/strategies/basic_crud_strategy.rb', line 108 def model_errors return [ ActiveRecord::UnknownAttributeError, ActiveRecord::ActiveRecordError ] if defined?(::ActiveRecord) return [ Mongoid::MongoidError ] if defined?(::ActiveRecord) end |
#modify ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/harmonize/strategies/basic_crud_strategy.rb', line 61 def modify log do find_or_initialize_target update_target_attributes save_target end end |
#save_target ⇒ Object
88 89 90 91 92 |
# File 'lib/harmonize/strategies/basic_crud_strategy.rb', line 88 def save_target unless @target.save @error = @target.errors. end end |
#update_target_attributes ⇒ Object
83 84 85 86 |
# File 'lib/harmonize/strategies/basic_crud_strategy.rb', line 83 def update_target_attributes @target.attributes = @source @modified = @target.changed? end |