Sha256: 035f4b2a7794a7faee595da1585aae70229ea52a164c17dd565fbce4423c40e4

Contents?: true

Size: 879 Bytes

Versions: 5

Compression:

Stored size: 879 Bytes

Contents

class ChangeSensitiveHash
  attr_reader :hash
  def initialize(record, field, hash)
    @record = record
    @field = field
    @hash = hash
  end
  
  def inspect
    @hash.inspect
  end
  
  def to_hash
    @hash
  end
  
  def merge!(other_hash)
    notify!
    @hash.merge!(other_hash)
  end
  
  def delete(key)
    notify!
    @hash.delete(key)
  end
  
  def clear
    notify!
    @hash.clear
  end
  
  def []=(key, value)
    notify!
    @hash[key] = value
  end
  
  def method_missing(name, *args, &block)
    notify! if name.to_s.end_with?('!')
    @hash.send(name, *args, &block)
  end
  
  # See ChangeSensitiveArray#dup for an explanation of this method
  def dup
    copy = ChangeSensitiveHash.new(@record.dup, @field.dup, @hash.dup)
    @record.typecast[@field] = copy
    self
  end
  
  private
    def notify!
      @record.try(:changed!, @field)
    end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
yodel-0.0.7 lib/yodel/models/core/fields/change_sensitive_hash.rb
yodel-0.0.4 lib/yodel/models/core/fields/change_sensitive_hash.rb
yodel-0.0.3 lib/yodel/models/core/fields/change_sensitive_hash.rb
yodel-0.0.2 lib/yodel/models/core/fields/change_sensitive_hash.rb
yodel-0.0.1 lib/yodel/models/core/fields/change_sensitive_hash.rb