Sha256: 3af46b4374e541345f0f1877c6e00285d93b7b250859d53e38c8c32fed203425

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

module Disposable::Twin::Changed
  class Changes < Hash
    def changed?(name=nil)
      return true if name.nil? and values.find { |val| val == true } # TODO: this could be speed-improved, of course.

      !! self[name.to_s]
    end
  end


  def changed?(*args) # not recommended for external use?
    changed.changed?(*args)
  end

# FIXME: can we make #changed the only public concept? so we don't need to find twice?

  # this is usually called only once in Sync::SkipUnchanged, per twin.
  def changed
    _find_changed_twins!(@_changes)

    @_changes
  end

private
  def initialize(model, *args)
    super         # Setup#initialize.
    @_changes = Changes.new # override changed from initialize.
  end

  def _changed
    @_changes ||= Changes.new # FIXME: why do we need to re-initialize here?
  end

  def write_property(name, value, dfn)
    old_value = send(name)

    super.tap do
      _changed[name.to_s] = old_value != value
    end
  end

  def _find_changed_twins!(changes) # FIXME: this will change soon. don't touch.
    schema.each(twin: true) do |dfn|
      next unless twin = send(dfn.getter)
      next unless twin.changed?

      changes[dfn.name] = true
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
disposable-0.1.1 lib/disposable/twin/changed.rb
disposable-0.1.0 lib/disposable/twin/changed.rb