Sha256: fa91b2e6e3bc8c067cfd95830dfcda3a2bc8786c1f65b9fcf391239f8085de1a

Contents?: true

Size: 1.78 KB

Versions: 7

Compression:

Stored size: 1.78 KB

Contents

module Spyke
  module Orm
    extend ActiveSupport::Concern

    included do
      define_model_callbacks :create, :update, :save

      class_attribute :include_root
      self.include_root = true

      class_attribute :callback_methods
      self.callback_methods = { create: :post, update: :put }.freeze
    end

    module ClassMethods
      def include_root_in_json(value)
        self.include_root = value
      end

      def method_for(callback, value = nil)
        self.callback_methods = callback_methods.merge(callback => value) if value
        callback_methods[callback]
      end

      def find(id)
        raise ResourceNotFound if id.blank?
        id = strip_slug(id)
        where(id: id).find_one || raise(ResourceNotFound)
      end

      def fetch
        uri = new.uri
        get_raw uri, current_scope.params.except(*uri.variables)
      end

      def create(attributes = {})
        record = new(attributes)
        record.save
        record
      end

      def destroy(id = nil)
        new(id: id).destroy
      end

      def strip_slug(id)
        id.to_s.split('-').first
      end
    end

    def to_params
      if include_root?
        { self.class.model_name.param_key => attributes.to_params.except(*uri.variables)}
      else
        attributes.to_params.except(*uri.variables)
      end
    end

    def persisted?
      id?
    end

    def save
      run_callbacks :save do
        callback = persisted? ? :update : :create
        run_callbacks(callback) do
          send self.class.method_for(callback), to_params
        end
      end
    end

    def destroy
      delete
    end

    def update_attributes(new_attributes)
      self.attributes = new_attributes
      save
    end

    def reload
      self.attributes = self.class.find(id).attributes
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
spyke-1.8.2 lib/spyke/orm.rb
spyke-1.8.1 lib/spyke/orm.rb
spyke-1.8.0 lib/spyke/orm.rb
spyke-1.7.2 lib/spyke/orm.rb
spyke-1.7.1 lib/spyke/orm.rb
spyke-1.7.0 lib/spyke/orm.rb
spyke-1.6.0 lib/spyke/orm.rb