Sha256: c3ec5b6c96609c92e6a4959ccb4ce102369b9530ec7babc40de5ddec1eb40395

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

require 'forwardable'
require 'active_support/concern'
require 'active_support/inflector'
require 'active_support/core_ext/hash'
require 'active_support/core_ext/class/attribute'

module JsonApiClient
  class Resource
    class_attribute :site, :primary_key, :link_style, :default_headers

    self.primary_key = :id
    self.link_style = :id # or :url
    self.default_headers = {}

    class << self
      # base URL for this resource
      def resource
        File.join(site, path)
      end

      def table_name
        resource_name.pluralize
      end

      def resource_name
        name.demodulize.underscore
      end

      def find(conditions)
        run_request(Query::Find.new(self, conditions))
      end

      def create(conditions = {})
        result = run_request(Query::Create.new(self, conditions))
        if result.has_errors?
          yield(result) if block_given?
          return nil
        end
        result.first
      end

      def run_request(query)
        parse(connection.execute(query))
      end
    end

    include Helpers::Initializable
    include Helpers::Attributable
    include Helpers::Associable
    include Helpers::Parsable
    include Helpers::Queryable
    include Helpers::Serializable
    include Helpers::Linkable

    def save
      query = persisted? ? 
        Query::Update.new(self.class, attributes) :
        Query::Create.new(self.class, attributes)

      run_request(query)
    end

    def destroy
      if run_request(Query::Destroy.new(self.class, attributes))
        self.attributes.clear
        true
      else
        false
      end
    end

    protected

    def run_request(query)
      # reset errors if a new request is being made
      self.errors.clear if self.errors
      
      result = self.class.run_request(query)
      self.errors = result.errors
      if result.has_errors?
        return false
      else
        if updated = result.first
          self.attributes = updated.attributes
        end
        return true
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json_api_client-0.1.3 lib/json_api_client/resource.rb