Sha256: 5c64defa0511b20ab4e4ac40318ccb76538eb8a3be58c1994c59b011874c2422

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 KB

Contents

module ActiveRecord
  
  module InstanceMethods
        
    def attributes
      @backing_record.attributes
    end
    
    def initialize(hash = {})
      if hash.is_a? ReactiveRecord::Base
        @backing_record = hash
      else
        # standard active_record new -> creates a new instance, primary key is ignored if present
        hash[primary_key] = nil
        @backing_record = ReactiveRecord::Base.new(self.class, hash, self)
      end
    end
    
    def primary_key
      self.class.primary_key
    end
    
    def id
      @backing_record.reactive_get!(primary_key)
    end
    
    def id=(value)
      @backing_record.id = value
    end

    def model_name
      # in reality should return ActiveModel::Name object, blah blah
      self.class.model_name
    end
    
    def revert
      @backing_record.revert
    end
    
    def changed?
      @backing_record.changed?
    end
    
    def ==(ar_instance)
      @backing_record == ar_instance.instance_eval { @backing_record }
    end
    
    def method_missing(name, *args, &block)
      if name =~ /_changed\?$/
        @backing_record.changed?(name.gsub(/_changed\?$/,""))
      elsif args.count == 1 && name =~ /=$/ && !block
        attribute_name = name.gsub(/=$/,"")
        @backing_record.reactive_set!(attribute_name, args[0])
      elsif args.count == 0 && !block
        @backing_record.reactive_get!(name) 
      else
        super
      end
    end
    
    def save(&block) 
      @backing_record.save &block
    end
    
    def saving?
      @backing_record.saving?
    end
    
    def destroy(&block)
      @backing_record.destroy &block
    end
    
  end
  
end
    

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
reactive-record-0.7.6 lib/reactive_record/active_record/instance_methods.rb
reactive-record-0.7.5 lib/reactive_record/active_record/instance_methods.rb
reactive-record-0.7.4 lib/reactive_record/active_record/instance_methods.rb
reactive-record-0.7.1 lib/reactive_record/active_record/instance_methods.rb
reactive-record-0.7.0 lib/reactive_record/active_record/instance_methods.rb