Class: Diffit::Changes
- Inherits:
-
Object
- Object
- Diffit::Changes
- Includes:
- Enumerable
- Defined in:
- lib/diffit/changes.rb
Instance Attribute Summary collapse
-
#records ⇒ Object
readonly
Returns the value of attribute records.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
Instance Method Summary collapse
-
#append(model, data) ⇒ self
Appends provided data to
self. - #cleanup! ⇒ Object
-
#each ⇒ Enumerator, Array(Diffit::Record)
Calls the given block once for each record in the collection.
-
#empty? ⇒ Boolean
Are there any changes?.
-
#initialize(timestamp) ⇒ Diffit::Changes
constructor
Instantiates a Diffit::Changes with provided timestamp.
-
#length ⇒ Fixnum
Number of changes.
- #prepare! ⇒ Object
-
#to_h ⇒ Hash
(also: #to_hash)
A
Hashrepresentation ofself. -
#to_json ⇒ String
A JSON representation of
self. -
#to_s ⇒ String
(also: #to_str)
A short
Stringrepresentation ofself.
Constructor Details
#initialize(timestamp) ⇒ Diffit::Changes
Instantiates a Diffit::Changes with provided timestamp.
13 14 15 16 |
# File 'lib/diffit/changes.rb', line 13 def initialize() @timestamp = @records = [] end |
Instance Attribute Details
#records ⇒ Object (readonly)
Returns the value of attribute records.
7 8 9 |
# File 'lib/diffit/changes.rb', line 7 def records @records end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
6 7 8 |
# File 'lib/diffit/changes.rb', line 6 def @timestamp end |
Instance Method Details
#append(model, data) ⇒ self
Appends provided data to self.
23 24 25 26 27 28 29 |
# File 'lib/diffit/changes.rb', line 23 def append(model, data) data.group_by { |row| row[:record_id] }.each do |record_id, changes| @records << Record.new(model, record_id, changes.map { |c| c.slice(:column_name, :value, :changed_at)}) end @length = nil self end |
#cleanup! ⇒ Object
92 93 94 95 |
# File 'lib/diffit/changes.rb', line 92 def cleanup! @records.clear @length = nil end |
#each ⇒ Enumerator, Array(Diffit::Record)
Calls the given block once for each record in the collection.
49 50 51 52 53 54 55 |
# File 'lib/diffit/changes.rb', line 49 def each if block_given? @records.each { |c| yield c } else @records.enum_for(:each) end end |
#empty? ⇒ Boolean
Are there any changes?
34 35 36 |
# File 'lib/diffit/changes.rb', line 34 def empty? @records.empty? end |
#length ⇒ Fixnum
Number of changes.
41 42 43 |
# File 'lib/diffit/changes.rb', line 41 def length @length ||= @records.inject(0) { |v,r| v += r.changes.length } end |
#prepare! ⇒ Object
86 87 88 89 90 |
# File 'lib/diffit/changes.rb', line 86 def prepare! @records.sort_by! { |record| record.last_changed_at } @records.uniq! { |record| [record.model, record.record_id] } nil end |
#to_h ⇒ Hash Also known as: to_hash
A Hash representation of self.
73 74 75 |
# File 'lib/diffit/changes.rb', line 73 def to_h {timestamp: .to_i, changes: @records.map(&:to_h)} end |
#to_json ⇒ String
A JSON representation of self.
82 83 84 |
# File 'lib/diffit/changes.rb', line 82 def to_json to_h.to_json end |
#to_s ⇒ String Also known as: to_str
A short String representation of self.
60 61 62 63 64 65 66 |
# File 'lib/diffit/changes.rb', line 60 def to_s sprintf '#<%s:%#0x @timestamp: %s @changes: {%d}>', self.class.to_s, self.object_id, @timestamp.strftime('%d/%b/%Y:%H:%M:%S %z'), length end |