Sha256: 3163c45dd1605ec55f9e8105d396c30bd397830fdaa3c01a66e4ab4460cd9fe5

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

require_relative 'init'
require_relative '../active_mocker/collection/queries'
require_relative '../active_mocker/collection/base'
require_relative '../active_mocker/collection/relation'
module ActiveMocker
  module ActiveHash

    module ARApi

      include ::ActiveHash::ARApi::Init

      def delete
        self.class.send(:record_index).delete("#{self.id}")
        records = self.class.instance_variable_get(:@records)
        index = records.index(self)
        records.delete_at(index)
      end

      def update(options={})
        options.each do |method, value|
          send("#{method}=", value)
        end

      end

      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        include ActiveMocker::Collection::Queries

        def create(attributes = {}, &block)
          record = new(attributes) unless block_given?
          record = new(attributes, &block) if block_given?
          record.save
          mark_dirty
          record
        end

        def find_or_create_by(attributes)
          find_by(attributes) || create(attributes)
        end

        def find_or_initialize_by(attributes)
          find_by(attributes) || new(attributes)
        end

        def delete(id)
          find(id).delete
        end

        def to_a
          @records
        end

        alias_method :destroy, :delete

        def delete_all(options=nil)
          return super() if options.nil?
          where(options).map{|r| r.delete}.count
        end

        alias_method :destroy_all, :delete_all

      end

    end



  end

end



Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_mocker-1.3.2 lib/active_hash/ar_api.rb
active_mocker-1.3.1 lib/active_hash/ar_api.rb