class Traka::Change
Public Class Methods
changes(opts={})
click to toggle source
# File lib/traka/change.rb, line 33 def changes(opts={}) opts = {:version => nil, :filter => true, :actions => [], :only => []}.merge(opts) return [] if invalid_version?(opts[:version]) # If version is specified, return only published changes from v onwards. # Otherwise, return only unstaged changes. unless opts[:version].is_a?(Range) opts[:version] = opts[:version] ? (opts[:version]..latest_version) : latest_version + 1 end c = where(["version #{opts[:version].is_a?(Range) ? "in" : ">="} (?)", opts[:version]]) unless opts[:actions].empty? c = c.where(["action_type in (?)", opts[:actions]]) end unless opts[:only].empty? c = c.where(["klass in (?)", opts[:only].map(&:to_s)]) end if opts[:filter] filter_changes(c) else c.all end end
latest_version()
click to toggle source
# File lib/traka/change.rb, line 10 def latest_version begin File.read(version_path).to_i rescue tc = self.last v = tc ? tc.version : 1 logger.warn "Latest Traka version not found. Defaulting to v#{v}" v end end
publish_new_version!()
click to toggle source
# File lib/traka/change.rb, line 23 def publish_new_version! set_version!(latest_version + 1) end
set_version!(v)
click to toggle source
# File lib/traka/change.rb, line 27 def set_version!(v) File.open(version_path, "w") do |f| f.write(v.to_s) end end
Private Class Methods
filter_changes(changes)
click to toggle source
Filters out obsolete changes in the given set. For example, there is no point giving out 4 “update” changes, just one will suffice. And if you “create” and then “destroy” a record in one changeset, they should cancel each other out. This is not an efficient algorithm. May need to re-think for huge change-sets.
# File lib/traka/change.rb, line 86 def filter_changes(changes) changes.reject do |c| if c.action_type == "create" changes.any? { |cc| cc.action_type == "destroy" and cc.uuid == c.uuid } elsif c.action_type == "destroy" changes.any? { |cc| cc.action_type == "create" and cc.uuid == c.uuid } or changes.any? { |cc| cc.action_type == "update" and cc.uuid == c.uuid } elsif c.action_type == "update" changes.any? { |cc| cc.action_type == "create" and cc.uuid == c.uuid } or changes.any? { |cc| cc.action_type == "destroy" and cc.uuid == c.uuid } or changes.any? { |cc| cc.action_type == "update" and cc.uuid == c.uuid and cc.id > c.id } end end end
invalid_version?(v)
click to toggle source
# File lib/traka/change.rb, line 68 def invalid_version?(v) (v.is_a?(Fixnum) and v > latest_version) or (v.is_a?(Range) and v.begin > latest_version) or (v.is_a?(Range) and v.begin > v.end) end
version_path()
click to toggle source
# File lib/traka/change.rb, line 74 def version_path File.join( Rails.root, "public", "system", "api", "version.txt") end
Public Instance Methods
get_record()
click to toggle source
# File lib/traka/change.rb, line 102 def get_record ar = ActiveRecord::Base.const_get(self.klass) ar.where(ar.traka_uuid => self.uuid).first end
Private Instance Methods
set_version()
click to toggle source
# File lib/traka/change.rb, line 109 def set_version self.version = self.class.latest_version + 1 end