Class: Logidze::History
- Inherits:
-
Object
- Object
- Logidze::History
- Defined in:
- lib/logidze/history.rb,
lib/logidze/history/type.rb,
lib/logidze/history/version.rb
Overview
Log data wrapper
Defined Under Namespace
Constant Summary collapse
- HISTORY =
History key
'h'
- VERSION =
Version key
'v'
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
-
.dump(object) ⇒ Object
Rails 4 ###.
- .load(json) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #as_json(options = {}) ⇒ Object
-
#changes_to(time: nil, version: nil, data: {}, from: 0) ⇒ Object
Return diff from the initial state to specified time or version.
-
#current_ts?(time) ⇒ Boolean
Return true iff time corresponds to current version.
- #current_version ⇒ Object
-
#diff_from(time: nil, version: nil) ⇒ Object
Return diff object representing changes since specified time or version.
- #dup ⇒ Object
-
#exists_ts?(time) ⇒ Boolean
Return true iff time greater or equal to the first version time.
-
#find_by_time(time) ⇒ Object
Return nearest (from the bottom) version to the specified time.
-
#find_by_version(num) ⇒ Object
Return version by number or nil.
-
#initialize(data) ⇒ History
constructor
A new instance of History.
- #next_version ⇒ Object
- #previous_version ⇒ Object
-
#version ⇒ Object
Returns current version number.
-
#version=(val) ⇒ Object
Change current version.
- #versions ⇒ Object
Constructor Details
#initialize(data) ⇒ History
Returns a new instance of History.
28 29 30 |
# File 'lib/logidze/history.rb', line 28 def initialize(data) @data = data end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
14 15 16 |
# File 'lib/logidze/history.rb', line 14 def data @data end |
Class Method Details
.dump(object) ⇒ Object
Rails 4 ###
20 21 22 |
# File 'lib/logidze/history.rb', line 20 def self.dump(object) ActiveSupport::JSON.encode(object) end |
.load(json) ⇒ Object
24 25 26 |
# File 'lib/logidze/history.rb', line 24 def self.load(json) new(json) if json.present? end |
Instance Method Details
#==(other) ⇒ Object
115 116 117 118 |
# File 'lib/logidze/history.rb', line 115 def ==(other) return super unless other.is_a?(self.class) data == other.data end |
#as_json(options = {}) ⇒ Object
120 121 122 |
# File 'lib/logidze/history.rb', line 120 def as_json( = {}) data.as_json() end |
#changes_to(time: nil, version: nil, data: {}, from: 0) ⇒ Object
Return diff from the initial state to specified time or version. Optional ‘data` paramater can be used as initial diff state.
60 61 62 63 64 65 66 67 68 |
# File 'lib/logidze/history.rb', line 60 def changes_to(time: nil, version: nil, data: {}, from: 0) raise ArgumentError, "Time or version must be specified" if time.nil? && version.nil? filter = time.nil? ? method(:version_filter) : method(:time_filter) versions.each_with_object(data.dup) do |v, acc| next if v.version < from break acc if filter.call(v, version, time) acc.merge!(v.changes) end end |
#current_ts?(time) ⇒ Boolean
Return true iff time corresponds to current version
96 97 98 99 |
# File 'lib/logidze/history.rb', line 96 def current_ts?(time) (current_version.time <= time) && (next_version.nil? || (next_version.time < time)) end |
#current_version ⇒ Object
46 47 48 |
# File 'lib/logidze/history.rb', line 46 def current_version find_by_version(version) end |
#diff_from(time: nil, version: nil) ⇒ Object
Return diff object representing changes since specified time or version.
rubocop:disable Metrics/AbcSize
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/logidze/history.rb', line 77 def diff_from(time: nil, version: nil) raise ArgumentError, "Time or version must be specified" if time.nil? && version.nil? from_version = version.nil? ? find_by_time(time) : find_by_version(version) from_version ||= versions.first base = changes_to(version: from_version.version) diff = changes_to(version: self.version, data: base, from: from_version.version + 1) build_changes(base, diff) end |
#dup ⇒ Object
111 112 113 |
# File 'lib/logidze/history.rb', line 111 def dup self.class.new(data.deep_dup) end |
#exists_ts?(time) ⇒ Boolean
Return true iff time greater or equal to the first version time
91 92 93 |
# File 'lib/logidze/history.rb', line 91 def exists_ts?(time) versions.present? && versions.first.time <= time end |
#find_by_time(time) ⇒ Object
Return nearest (from the bottom) version to the specified time
107 108 109 |
# File 'lib/logidze/history.rb', line 107 def find_by_time(time) versions.reverse.find { |v| v.time <= time } end |
#find_by_version(num) ⇒ Object
Return version by number or nil
102 103 104 |
# File 'lib/logidze/history.rb', line 102 def find_by_version(num) versions.find { |v| v.version == num } end |
#next_version ⇒ Object
54 55 56 |
# File 'lib/logidze/history.rb', line 54 def next_version find_by_version(version + 1) end |
#previous_version ⇒ Object
50 51 52 |
# File 'lib/logidze/history.rb', line 50 def previous_version find_by_version(version - 1) end |
#version ⇒ Object
Returns current version number
37 38 39 |
# File 'lib/logidze/history.rb', line 37 def version data.fetch(VERSION) end |
#version=(val) ⇒ Object
Change current version
42 43 44 |
# File 'lib/logidze/history.rb', line 42 def version=(val) data.store(VERSION, val) end |