Sha256: 5781eba1c3dcf2e0330e92b5bc0f21f9f11fc1ede1a878551c953da11ea2bf4a

Contents?: true

Size: 1.34 KB

Versions: 14

Compression:

Stored size: 1.34 KB

Contents

module PassiveRecord
  module InstanceMethods
    def update(attrs={})
      self.class.before_update_hooks.each do |hook|
        hook.run(self)
      end

      attrs.each do |k,v|
        send("#{k}=", v)
      end

      self.class.after_update_hooks.each do |hook|
        hook.run(self)
      end

      self
    end

    def destroy
      self.class.before_destroy_hooks.each do |hook|
        hook.run(self)
      end

      self.class.destroy(self.id)

      self.class.after_destroy_hooks.each do |hook|
        hook.run(self)
      end
    end

    # from http://stackoverflow.com/a/8417341/90042
    def to_h
      Hash[
        attribute_names.
        map do |name| [
          name.to_s.gsub("@","").to_sym,  # key
          (instance_variable_get(name) rescue send(name))] # val
        end
      ]
    end

    protected
    def attribute_names
      attr_names = instance_variables
      attr_names += self.class.associations_id_syms
      attr_names += members rescue []
      attr_names.reject! { |name| name.to_s.start_with?("@_") || name.match(/join_model/) }
      attr_names - blacklisted_attribute_names
    end

    def blacklisted_attribute_names
      []
    end

    private

    def relata
      @_relata ||= self.class.associations && self.class.associations.map do |assn|
        assn.to_relation(self)
      end || []
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
passive_record-0.4.14 lib/passive_record/instance_methods.rb
passive_record-0.4.13 lib/passive_record/instance_methods.rb
passive_record-0.4.12 lib/passive_record/instance_methods.rb
passive_record-0.4.11 lib/passive_record/instance_methods.rb
passive_record-0.4.10 lib/passive_record/instance_methods.rb
passive_record-0.4.8 lib/passive_record/instance_methods.rb
passive_record-0.4.7 lib/passive_record/instance_methods.rb
passive_record-0.4.6 lib/passive_record/instance_methods.rb
passive_record-0.4.5 lib/passive_record/instance_methods.rb
passive_record-0.4.4 lib/passive_record/instance_methods.rb
passive_record-0.4.3 lib/passive_record/instance_methods.rb
passive_record-0.4.2 lib/passive_record/instance_methods.rb
passive_record-0.4.1 lib/passive_record/instance_methods.rb
passive_record-0.4.0 lib/passive_record/instance_methods.rb