Sha256: 31bf3e957a9733000c6e070678c3c7bbb87155ecd273503b3068aa836f43750b

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 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?("@_") }
      attr_names - blacklisted_attribute_names
    end

    def blacklisted_attribute_names
      []
    end

    private

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
passive_record-0.3.12 lib/passive_record/instance_methods.rb