Module: Snptime
- Defined in:
- lib/snaptime.rb
Constant Summary collapse
- SNAPTIME_REQUEST_STORE_KEY =
:snaptime_snaptime
- CURRENT_NOW_REQUEST_STORE_KEY =
:snaptime_current_now
- CLONED_RECORDS_STORE_KEY =
:snaptime_cloned_records
- RECORD_CLONING_SWITCH_REQUEST_STORE_KEY =
:snaptime_record_cloning
- SMALLEST_TIME_UNIT =
0.001
Class Method Summary collapse
- .after_commit_or_rollback ⇒ Object
- .consolidation_fields ⇒ Object
- .current_now ⇒ Object
-
.fake_current_now(time) ⇒ Object
Override the “current now” used for creating new versions.
- .model_class ⇒ Object
- .model_class=(model_class) ⇒ Object
- .record_cloned_in_current_tx(record) ⇒ Object
- .record_cloned_in_current_tx?(record) ⇒ Boolean
- .record_cloning_enabled? ⇒ Boolean
- .register_consolidation_field(name, aggregate_with: nil, default: Arel.sql('null')) ⇒ Object
- .reset_current_now ⇒ Object
- .reset_records_cloned_in_current_tx ⇒ Object
- .snaptime ⇒ Object
-
.with_fake_current_now(time, &_block) ⇒ Object
Override the “current now” used for creating new versions.
- .with_snaptime(snaptime, &_block) ⇒ Object
- .without_record_cloning(&_block) ⇒ Object
Class Method Details
.after_commit_or_rollback ⇒ Object
117 118 119 120 |
# File 'lib/snaptime.rb', line 117 def self.after_commit_or_rollback reset_current_now reset_records_cloned_in_current_tx end |
.consolidation_fields ⇒ Object
27 28 29 |
# File 'lib/snaptime.rb', line 27 def self.consolidation_fields @consolidation_fields end |
.current_now ⇒ Object
73 74 75 |
# File 'lib/snaptime.rb', line 73 def self.current_now RequestStore.store[CURRENT_NOW_REQUEST_STORE_KEY] ||= Time.now.utc end |
.fake_current_now(time) ⇒ Object
Override the “current now” used for creating new versions. Only use this method for testing purposes and make sure you use ‘reset_current_now` if necessary. Use `with_fake_current_now` whenever possible.
80 81 82 |
# File 'lib/snaptime.rb', line 80 def self.fake_current_now(time) RequestStore.store[CURRENT_NOW_REQUEST_STORE_KEY] ||= time.utc end |
.model_class ⇒ Object
37 38 39 |
# File 'lib/snaptime.rb', line 37 def self.model_class @model_class end |
.model_class=(model_class) ⇒ Object
33 34 35 |
# File 'lib/snaptime.rb', line 33 def self.model_class=(model_class) @model_class = model_class end |
.record_cloned_in_current_tx(record) ⇒ Object
100 101 102 103 104 |
# File 'lib/snaptime.rb', line 100 def self.record_cloned_in_current_tx(record) RequestStore.store[CLONED_RECORDS_STORE_KEY] ||= {} RequestStore.store[CLONED_RECORDS_STORE_KEY][record.class.table_name] ||= {} RequestStore.store[CLONED_RECORDS_STORE_KEY][record.class.table_name][record.send(record.class.primary_key)] = true end |
.record_cloned_in_current_tx?(record) ⇒ Boolean
106 107 108 109 110 111 |
# File 'lib/snaptime.rb', line 106 def self.record_cloned_in_current_tx?(record) RequestStore.store .try(:[], CLONED_RECORDS_STORE_KEY) .try(:[], record.class.table_name) .try(:[], record.send(record.class.primary_key)) || false end |
.record_cloning_enabled? ⇒ Boolean
65 66 67 |
# File 'lib/snaptime.rb', line 65 def self.record_cloning_enabled? RequestStore.store[RECORD_CLONING_SWITCH_REQUEST_STORE_KEY] != false end |
.register_consolidation_field(name, aggregate_with: nil, default: Arel.sql('null')) ⇒ Object
23 24 25 |
# File 'lib/snaptime.rb', line 23 def self.register_consolidation_field(name, aggregate_with: nil, default: Arel.sql('null')) @consolidation_fields[name] = { aggregate_with: aggregate_with, default: default } end |
.reset_current_now ⇒ Object
96 97 98 |
# File 'lib/snaptime.rb', line 96 def self.reset_current_now RequestStore.store[CURRENT_NOW_REQUEST_STORE_KEY] = nil end |
.reset_records_cloned_in_current_tx ⇒ Object
113 114 115 |
# File 'lib/snaptime.rb', line 113 def self.reset_records_cloned_in_current_tx RequestStore.store[CLONED_RECORDS_STORE_KEY] = nil end |
.snaptime ⇒ Object
69 70 71 |
# File 'lib/snaptime.rb', line 69 def self.snaptime RequestStore.store[SNAPTIME_REQUEST_STORE_KEY] end |
.with_fake_current_now(time, &_block) ⇒ Object
Override the “current now” used for creating new versions. Only use this method for testing purposes.
86 87 88 89 90 91 92 93 94 |
# File 'lib/snaptime.rb', line 86 def self.with_fake_current_now(time, &_block) fake_current_now time begin yield ensure reset_current_now end end |
.with_snaptime(snaptime, &_block) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/snaptime.rb', line 41 def self.with_snaptime(snaptime, &_block) previous_snaptime = RequestStore.store[SNAPTIME_REQUEST_STORE_KEY] RequestStore.store[SNAPTIME_REQUEST_STORE_KEY] = snaptime begin yield ensure RequestStore.store[SNAPTIME_REQUEST_STORE_KEY] = previous_snaptime end end |
.without_record_cloning(&_block) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/snaptime.rb', line 53 def self.without_record_cloning(&_block) previous_setting = RequestStore.store[RECORD_CLONING_SWITCH_REQUEST_STORE_KEY] RequestStore.store[RECORD_CLONING_SWITCH_REQUEST_STORE_KEY] = false begin yield ensure RequestStore.store[RECORD_CLONING_SWITCH_REQUEST_STORE_KEY] = previous_setting end end |