Sha256: b84c767db8a32d5e31d13719e28b8e504a4f1c934b82004db852cce7e5d723fa

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

module Signaling::Base::Persistence
  extend ActiveSupport::Concern

  module ClassMethods
    def create(params)
      from_response(request(:create, params))
    rescue Signaling::Error::UnprocessableEntity => e
      from_response(e.response[:body])
    end

    def destroy(id)
      from_response(request(:destroy, id: id))
    end

  end

  def new?
    self.id.blank?
  end

  def persisted?
    !new?
  end

  def save
    new? ? create : update
    true
  rescue Signaling::Error::UnprocessableEntity => e
    self.errors = e.response[:body][:errors]
    false
  end

  def save!
    save || raise(ResourceInvalid.new(self))
  end

  def update_attributes(params)
    update(params)
  rescue Signaling::Error::UnprocessableEntity => e
    self.attributes = e.response[:body]
    false
  end


  private

  def update(params = attributes)
    self.class.request(:update, params.merge(id: self.id)) do |response|
      self.attributes = response
    end
  end

  def create
    self.class.request(:create, attributes.except(:id)) do |response|
      self.attributes = response
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
signaling-1.1.1 lib/signaling/base/persistence.rb
signaling-1.1.0 lib/signaling/base/persistence.rb
signaling-1.0.0 lib/signaling/base/persistence.rb