Class: Au::Diff
Instance Attribute Summary collapse
-
#change ⇒ Object
readonly
Returns the value of attribute change.
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#md5 ⇒ Object
readonly
Returns the value of attribute md5.
-
#parent_id ⇒ Object
readonly
Returns the value of attribute parent_id.
Class Method Summary collapse
- .create(db_name, parent_id, change, md5) ⇒ Object
- .find(id, db_name: nil, db: nil) ⇒ Object
-
.get_db(db_name) ⇒ Object
memoize all encountered dbs Note, may need to watch memory usage.
- .replay_changes_upto(id, db_name) ⇒ Object
Instance Method Summary collapse
- #has_parent? ⇒ Boolean
-
#initialize(id, parent_id, change, md5, db) ⇒ Diff
constructor
A new instance of Diff.
- #parent ⇒ Object
Constructor Details
#initialize(id, parent_id, change, md5, db) ⇒ Diff
Returns a new instance of Diff.
51 52 53 54 55 56 57 |
# File 'lib/au/models/diff.rb', line 51 def initialize(id, parent_id, change, md5, db) @id = id @parent_id = parent_id @change = change @md5 = md5 @db = db end |
Instance Attribute Details
#change ⇒ Object (readonly)
Returns the value of attribute change.
49 50 51 |
# File 'lib/au/models/diff.rb', line 49 def change @change end |
#db ⇒ Object (readonly)
Returns the value of attribute db.
49 50 51 |
# File 'lib/au/models/diff.rb', line 49 def db @db end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
49 50 51 |
# File 'lib/au/models/diff.rb', line 49 def id @id end |
#md5 ⇒ Object (readonly)
Returns the value of attribute md5.
49 50 51 |
# File 'lib/au/models/diff.rb', line 49 def md5 @md5 end |
#parent_id ⇒ Object (readonly)
Returns the value of attribute parent_id.
49 50 51 |
# File 'lib/au/models/diff.rb', line 49 def parent_id @parent_id end |
Class Method Details
.create(db_name, parent_id, change, md5) ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/au/models/diff.rb', line 6 def self.create(db_name, parent_id, change, md5) db = get_db(db_name) id = Time.now.to_f db.transaction do db[id] = [parent_id, change, md5] end id end |
.find(id, db_name: nil, db: nil) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/au/models/diff.rb', line 27 def self.find(id, db_name: nil, db: nil) raise 'Missing both db name and db' if db_name.nil? && db.nil? db ||= get_db(db_name) stored_values = db.transaction{ db[id] } raise 'Diff not found' unless stored_values parent_id, change, md5 = stored_values new(id, parent_id, change, md5, db) end |
.get_db(db_name) ⇒ Object
memoize all encountered dbs Note, may need to watch memory usage. Note, depending on PStore initialization, may not need this
40 41 42 43 44 45 46 47 |
# File 'lib/au/models/diff.rb', line 40 def self.get_db(db_name) if @dbs @dbs[db_name] ||= PStore.new(db_name) else @dbs = {} get_db(db_name) end end |
.replay_changes_upto(id, db_name) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/au/models/diff.rb', line 15 def self.replay_changes_upto(id, db_name) raise 'Missing block' unless block_given? diffs = [find(id, db_name: db_name)] while diffs.last.has_parent? diffs << diffs.last.parent end # apply the oldest first diffs.reverse.each do |diff| yield(diff) end end |
Instance Method Details
#has_parent? ⇒ Boolean
64 65 66 |
# File 'lib/au/models/diff.rb', line 64 def has_parent? !!parent_id end |
#parent ⇒ Object
59 60 61 62 |
# File 'lib/au/models/diff.rb', line 59 def parent return unless parent_id self.class.find(parent_id, db: db) end |