Sha256: 0d950572b42174a13d9fb6cf35be2f8f7105c57d825a1dac3c10e144d895662f

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module Signable
  module Concerns
    module Query
      extend ActiveSupport::Concern

      def save
        return false unless valid?
        persisted? ? update : create
      end

      def delete
        self.class.client.delete self.class.entry_point, id
      end

      def persisted?
        id.present? rescue false
      end

      private

      def update
        response = self.class.client.update self.class.entry_point, id, self
        response.ok?
      end

      def create
        response = self.class.client.create self.class.entry_point, self
        if response.ok?
          self.attributes = response.object
          true
        else
          false
        end
      end

      module ClassMethods
        def all(offset = 0, limit = 10)
          response = client.all(entry_point, offset, limit)

          if response.ok?
            List.new(self, response.object)
          else
            []
          end
        end

        def find(id)
          response = client.find(entry_point, id)

          if response.ok?
            self.new response.object
          else
            nil
          end
        end

        def entry_point
          prefix.pluralize
        end

        def client
          @client ||= Signable::Query::Client.new
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
signable-0.0.1 lib/signable/concerns/query.rb