Class: Plumber
- Inherits:
-
Object
- Object
- Plumber
- Defined in:
- lib/plumber.rb,
lib/plumber/context.rb,
lib/plumber/version.rb
Overview
Provides functionality for creating and updating ActiveRecord model objects. Requires Rails 3+.
Defined Under Namespace
Classes: Context
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Method Summary collapse
- #import {|context| ... } ⇒ Object
-
#initialize(model, logger) ⇒ Plumber
constructor
Create a new instance which will act on the given model and log to the given logger.
Constructor Details
#initialize(model, logger) ⇒ Plumber
Create a new instance which will act on the given model and log to the given logger.
9 10 11 12 |
# File 'lib/plumber.rb', line 9 def initialize(model, logger) @model = model @logger = logger end |
Instance Method Details
#import {|context| ... } ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/plumber.rb', line 14 def import context = Plumber::Context.new(@model.scoped) yield context operation = if context.record.new_record? "Create" else "Update" end changed = context.record.changed begin ActiveRecord::Base.transaction do if context.record.save! if changed @logger.info("#{operation} #{context.record.class} #{context.record.id}.") else @logger.debug("#{context.record.class} #{context.record.id} not changed.") end end end rescue ActiveRecord::RecordInvalid => e prefix = "#{e.record.class} #{e.record.id}".strip @logger.warn("#{prefix}: #{operation} failed - #{e.record.errors.size} validation errors.") context.record.errors..each do || @logger.warn("#{prefix}: #{}") end end context.record end |