Sha256: 5ca4bdf919c4962d5fc66390d92b259eaa9fea5beb17816415730715378fac25

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

require 'active_hash'
require 'active_repository/sql_query_executor'

begin
  klass = Module.const_get(ActiveRecord::Rollback)
  unless klass.is_a?(Class)
    raise "Not defined"
  end
rescue
  module ActiveRecord
    class ActiveRecordError < StandardError
    end
    class Rollback < ActiveRecord::ActiveRecordError
    end
  end
end

module ActiveHash
  class Base
    def self.insert(record)
      if self.all.map(&:to_s).include?(record.to_s)
        record_index.delete(record.id.to_s)
        self.all.delete(record)
      end

      if record_index[record.id.to_s].nil? || !self.all.map(&:to_s).include?(record.to_s)
        @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
    end

    def self.where(query)
      if query.is_a?(String)
        return ActiveHash::SQLQueryExecutor.execute(self, query)
      else
        (@records || []).select do |record|
          query.all? { |col, match| record[col] == match }
        end
      end
    end

    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

    def readonly?
      false
    end

    def save(*args)
      record = self.class.find_by_id(self.id)

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

    def persisted?
      other = self.class.find_by_id(id)
      other.present? && other.created_at
    end

    def eql?(other)
      (other.instance_of?(self.class) || other.instance_of?(self.class.get_model_class)) && id.present? && (id == other.id) && (created_at == other.created_at)
    end

    alias == eql?
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_repository-0.0.1 lib/active_repository/write_support.rb