class ActiveHash::Base

Public Class Methods

insert(record) click to toggle source
# File lib/active_repository/write_support.rb, line 14
def self.insert(record)
  record_id   = record.id.to_s
  record_hash = record.hash

  remove(record)

  if record_index[record_id].nil? || !self.all.map(&:hash).include?(record_hash)
    insert_record(record)
  end
end
new(attributes = {}) click to toggle source
# File lib/active_repository/write_support.rb, line 6
def initialize(attributes = {})
  attributes = attributes.symbolize_keys
  @attributes = attributes
  attributes.dup.each do |key, value|
    send "#{key}=", value
  end
end
remove(record) click to toggle source
# File lib/active_repository/write_support.rb, line 25
def self.remove(record)
  record_id   = record.id.to_s
  record_hash = record.hash

  if self.all.map(&:hash).include?(record_hash)
    record_index.delete(record_id)
    self.all.delete(record)
  end
end
validate_unique_id(record) click to toggle source
# File lib/active_repository/write_support.rb, line 35
def self.validate_unique_id(record)
  raise IdError.new("Duplicate Id found for record #{record.attributes}") if record_index.has_key?(record.id.to_s)
end

Private Class Methods

insert_record(record) click to toggle source
# File lib/active_repository/write_support.rb, line 80
def self.insert_record(record)
  @records ||= []
  record.attributes[:id] ||= next_id

  validate_unique_id(record) if dirty
  mark_dirty

  if record.valid?
    add_to_record_index({ record.id.to_s => @records.length })
    @records << record
  end
end

Public Instance Methods

==(other)
Alias for: eql?
delete() click to toggle source
# File lib/active_repository/write_support.rb, line 56
def delete
  record = self.class.find_by(id: self.id)

  self.class.remove(self)

  self.class.find_by(id: self.id).nil?
end
eql?(other) click to toggle source
# File lib/active_repository/write_support.rb, line 73
def eql?(other)
  (other.instance_of?(self.class) || other.instance_of?(persistence_class)) && id.present? && (id == other.id) && (!self.respond_to?(:created_at) || (created_at == other.created_at))
end
Also aliased as: ==
persisted?() click to toggle source
# File lib/active_repository/write_support.rb, line 68
def persisted?
  other = self.class.find_by(id: id)
  other.present?
end
readonly?() click to toggle source
# File lib/active_repository/write_support.rb, line 39
def readonly?
  false
end
save(*args) click to toggle source
# File lib/active_repository/write_support.rb, line 43
def save(*args)
  if self.valid?
    record = self.class.find_by(id: self.id)

    self.class.insert(self) if record.nil? && record != self

    self.id = self.class.last.id if self.id.nil?
    true
  else
    false
  end
end
to_param() click to toggle source
# File lib/active_repository/write_support.rb, line 64
def to_param
  id.present? ? id.to_s : nil
end