class Logidze::History
Log data wrapper
Constants
Attributes
data[R]
Public Class Methods
new(data)
click to toggle source
# File lib/logidze/history.rb, line 20 def initialize(data) @data = data end
Public Instance Methods
==(other)
click to toggle source
Calls superclass method
# File lib/logidze/history.rb, line 109 def ==(other) return super unless other.is_a?(self.class) data == other.data end
as_json(options = {})
click to toggle source
# File lib/logidze/history.rb, line 115 def as_json(options = {}) data.as_json(options) end
changes_to(time: nil, version: nil, data: {}, from: 0)
click to toggle source
Return diff from the initial state to specified time or version. Optional ‘data` parameter can be used as initial diff state.
# 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)
click to toggle source
Return true iff time corresponds to current version
# 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()
click to toggle source
# File lib/logidze/history.rb, line 38 def current_version find_by_version(version) end
diff_from(time: nil, version: nil)
click to toggle source
Return diff object representing changes since specified time or version.
@example
diff_from(time: 2.days.ago) #=> { "id" => 1, "changes" => { "title" => { "old" => "Hello!", "new" => "World" } } }
rubocop:disable Metrics/AbcSize
# 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()
click to toggle source
# File lib/logidze/history.rb, line 105 def dup self.class.new(data.deep_dup) end
exists_ts?(time)
click to toggle source
Return true iff time greater or equal to the first version time
# File lib/logidze/history.rb, line 85 def exists_ts?(time) versions.present? && versions.first.time <= time end
find_by_time(time)
click to toggle source
Return nearest (from the bottom) version to the specified time
# 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)
click to toggle source
Return version by number or nil
# File lib/logidze/history.rb, line 96 def find_by_version(num) versions.find { |v| v.version == num } end
next_version()
click to toggle source
# File lib/logidze/history.rb, line 46 def next_version find_by_version(version + 1) end
previous_version()
click to toggle source
# File lib/logidze/history.rb, line 42 def previous_version find_by_version(version - 1) end
version()
click to toggle source
Returns current version number
# File lib/logidze/history.rb, line 29 def version data.fetch(VERSION) end
version=(val)
click to toggle source
Change current version
# File lib/logidze/history.rb, line 34 def version=(val) data.store(VERSION, val) end
versions()
click to toggle source
# File lib/logidze/history.rb, line 24 def versions @versions ||= data.fetch(HISTORY).map { |v| Version.new(v) } end
Private Instance Methods
build_changes(a, b)
click to toggle source
# File lib/logidze/history.rb, line 121 def build_changes(a, b) b.each_with_object({}) do |(k, v), acc| acc[k] = {"old" => a[k], "new" => v} unless v == a[k] end end
time_filter(item, _, time)
click to toggle source
# File lib/logidze/history.rb, line 131 def time_filter(item, _, time) item.time > time end
version_filter(item, version, _)
click to toggle source
# File lib/logidze/history.rb, line 127 def version_filter(item, version, _) item.version > version end