Class: Logidze::History

Inherits:
Object
  • Object
show all
Defined in:
lib/logidze/history.rb,
lib/logidze/history/type.rb,
lib/logidze/history/version.rb

Overview

Log data wrapper

Defined Under Namespace

Classes: Type, Version

Constant Summary collapse

HISTORY =

History key

'h'
VERSION =

Version key

'v'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#dataObject (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



117
118
119
120
121
# File 'lib/logidze/history.rb', line 117

def ==(other)
  return super unless other.is_a?(self.class)

  data == other.data
end

#as_json(options = {}) ⇒ Object



123
124
125
# File 'lib/logidze/history.rb', line 123

def as_json(options = {})
  data.as_json(options)
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.

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
70
# 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

Returns:

  • (Boolean)


98
99
100
101
# File 'lib/logidze/history.rb', line 98

def current_ts?(time)
  (current_version.time <= time) &&
    (next_version.nil? || (next_version.time < time))
end

#current_versionObject



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

Examples:


diff_from(time: 2.days.ago)
#=> { "id" => 1, "changes" => { "title" => { "old" => "Hello!", "new" => "World" } } }

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
88
89
# File 'lib/logidze/history.rb', line 79

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

#dupObject



113
114
115
# File 'lib/logidze/history.rb', line 113

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

Returns:

  • (Boolean)


93
94
95
# File 'lib/logidze/history.rb', line 93

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



109
110
111
# File 'lib/logidze/history.rb', line 109

def find_by_time(time)
  versions.reverse.find { |v| v.time <= time }
end

#find_by_version(num) ⇒ Object

Return version by number or nil



104
105
106
# File 'lib/logidze/history.rb', line 104

def find_by_version(num)
  versions.find { |v| v.version == num }
end

#next_versionObject



54
55
56
# File 'lib/logidze/history.rb', line 54

def next_version
  find_by_version(version + 1)
end

#previous_versionObject



50
51
52
# File 'lib/logidze/history.rb', line 50

def previous_version
  find_by_version(version - 1)
end

#versionObject

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

#versionsObject



32
33
34
# File 'lib/logidze/history.rb', line 32

def versions
  @versions ||= data.fetch(HISTORY).map { |v| Version.new(v) }
end