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.
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
20 21 22 |
# File 'lib/logidze/history.rb', line 20 def initialize(data) @data = data end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
15 16 17 |
# File 'lib/logidze/history.rb', line 15 def data @data end |
Instance Method Details
#==(other) ⇒ Object
109 110 111 112 113 |
# File 'lib/logidze/history.rb', line 109 def ==(other) return super unless other.is_a?(self.class) data == other.data end |
#as_json(options = {}) ⇒ Object
115 116 117 |
# File 'lib/logidze/history.rb', line 115 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` parameter can be used as initial diff state.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/logidze/history.rb', line 52 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
90 91 92 93 |
# File 'lib/logidze/history.rb', line 90 def current_ts?(time) (current_version.time <= time) && (next_version.nil? || (next_version.time < time)) end |
#current_version ⇒ Object
38 39 40 |
# File 'lib/logidze/history.rb', line 38 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
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/logidze/history.rb', line 71 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
105 106 107 |
# File 'lib/logidze/history.rb', line 105 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
85 86 87 |
# File 'lib/logidze/history.rb', line 85 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
101 102 103 |
# File 'lib/logidze/history.rb', line 101 def find_by_time(time) versions.reverse_each.find { |v| v.time <= time } end |
#find_by_version(num) ⇒ Object
Return version by number or nil
96 97 98 |
# File 'lib/logidze/history.rb', line 96 def find_by_version(num) versions.find { |v| v.version == num } end |
#next_version ⇒ Object
46 47 48 |
# File 'lib/logidze/history.rb', line 46 def next_version find_by_version(version + 1) end |
#previous_version ⇒ Object
42 43 44 |
# File 'lib/logidze/history.rb', line 42 def previous_version find_by_version(version - 1) end |
#version ⇒ Object
Returns current version number
29 30 31 |
# File 'lib/logidze/history.rb', line 29 def version data.fetch(VERSION) end |
#version=(val) ⇒ Object
Change current version
34 35 36 |
# File 'lib/logidze/history.rb', line 34 def version=(val) data.store(VERSION, val) end |
#versions ⇒ Object
24 25 26 |
# File 'lib/logidze/history.rb', line 24 def versions @versions ||= data.fetch(HISTORY).map { |v| Version.new(v) } end |