@@ -1,10 +1,8 @@ |
||
| 1 | 1 |
module DryRunnable |
| 2 |
- def dry_run! |
|
| 3 |
- readonly! |
|
| 2 |
+ extend ActiveSupport::Concern |
|
| 4 | 3 |
|
| 5 |
- class << self |
|
| 6 |
- prepend Sandbox |
|
| 7 |
- end |
|
| 4 |
+ def dry_run! |
|
| 5 |
+ @dry_run = true |
|
| 8 | 6 |
|
| 9 | 7 |
log = StringIO.new |
| 10 | 8 |
@dry_run_logger = Logger.new(log) |
@@ -14,6 +12,7 @@ module DryRunnable |
||
| 14 | 12 |
|
| 15 | 13 |
begin |
| 16 | 14 |
raise "#{short_type} does not support dry-run" unless can_dry_run?
|
| 15 |
+ readonly! |
|
| 17 | 16 |
check |
| 18 | 17 |
rescue => e |
| 19 | 18 |
error "Exception during dry-run. #{e.message}: #{e.backtrace.join("\n")}"
|
@@ -23,28 +22,38 @@ module DryRunnable |
||
| 23 | 22 |
memory: memory, |
| 24 | 23 |
log: log.string, |
| 25 | 24 |
) |
| 25 |
+ ensure |
|
| 26 |
+ @dry_run = false |
|
| 26 | 27 |
end |
| 27 | 28 |
|
| 28 | 29 |
def dry_run? |
| 29 |
- is_a? Sandbox |
|
| 30 |
+ !!@dry_run |
|
| 31 |
+ end |
|
| 32 |
+ |
|
| 33 |
+ included do |
|
| 34 |
+ prepend Wrapper |
|
| 30 | 35 |
end |
| 31 | 36 |
|
| 32 |
- module Sandbox |
|
| 37 |
+ module Wrapper |
|
| 33 | 38 |
attr_accessor :results |
| 34 | 39 |
|
| 35 | 40 |
def logger |
| 41 |
+ return super unless dry_run? |
|
| 36 | 42 |
@dry_run_logger |
| 37 | 43 |
end |
| 38 | 44 |
|
| 39 |
- def save |
|
| 40 |
- valid? |
|
| 45 |
+ def save(options = {})
|
|
| 46 |
+ return super unless dry_run? |
|
| 47 |
+ perform_validations(options) |
|
| 41 | 48 |
end |
| 42 | 49 |
|
| 43 |
- def save! |
|
| 44 |
- save or raise ActiveRecord::RecordNotSaved |
|
| 50 |
+ def save!(options = {})
|
|
| 51 |
+ return super unless dry_run? |
|
| 52 |
+ save(options) or raise_record_invalid |
|
| 45 | 53 |
end |
| 46 | 54 |
|
| 47 | 55 |
def log(message, options = {})
|
| 56 |
+ return super unless dry_run? |
|
| 48 | 57 |
case options[:level] || 3 |
| 49 | 58 |
when 0..2 |
| 50 | 59 |
sev = Logger::DEBUG |
@@ -58,6 +67,7 @@ module DryRunnable |
||
| 58 | 67 |
end |
| 59 | 68 |
|
| 60 | 69 |
def create_event(event) |
| 70 |
+ return super unless dry_run? |
|
| 61 | 71 |
if can_create_events? |
| 62 | 72 |
event = build_event(event) |
| 63 | 73 |
@dry_run_results[:events] << event.payload |