Sha256: 7cd3a1f1322e4a646b1b7095c8592998f9d2b8ca4fde52f70f6bc2bc4d7fcfe9

Contents?: true

Size: 1.64 KB

Versions: 10

Compression:

Stored size: 1.64 KB

Contents

require 'active_support/rescuable'
require 'rack/response'
require 'active_support/json'

module JSONAPIonify::Api
  class Resource
    extend JSONAPIonify::Autoload
    autoload_all

    include Callbacks
    extend ClassMethods
    extend Definitions
    extend Documentation

    include ErrorHandling
    include Includer
    include Builders
    include Defaults

    delegate :type, :attributes, :relationships, to: :class

    def self.inherited(subclass)
      super(subclass)
      subclass.class_eval do
        context(:api, readonly: true, persisted: true) { self.class.api }
        context(:resource, readonly: true, persisted: true) { self }
      end
    end

    def self.cache_key(**options)
      api.cache_key(
        **options,
        resource: name
      )
    end

    attr_reader :errors, :action, :response_headers

    def initialize(
      request:,
      context_definitions: self.class.context_definitions,
      commit: true,
      callbacks: true,
      context_overrides: {},
      cacheable: true,
      action: nil
    )
      context_overrides[:action_name] ||= action.name if action
      @__context                      = ContextDelegate.new(
        request,
        self,
        context_definitions,
        context_overrides
      )
      @errors                         = @__context.errors
      @action                         = action
      @response_headers               = @__context.response_headers
      @callbacks                      = action ? callbacks : false
      @cache_options                  = {}
      extend Caller if commit && action
      extend Exec unless action
      extend Caching if cacheable
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
jsonapionify-0.12.10 lib/jsonapionify/api/resource.rb
jsonapionify-0.12.9 lib/jsonapionify/api/resource.rb
jsonapionify-0.12.8 lib/jsonapionify/api/resource.rb
jsonapionify-0.12.7 lib/jsonapionify/api/resource.rb
jsonapionify-0.12.5 lib/jsonapionify/api/resource.rb
jsonapionify-0.12.4 lib/jsonapionify/api/resource.rb
jsonapionify-0.12.3 lib/jsonapionify/api/resource.rb
jsonapionify-0.12.2 lib/jsonapionify/api/resource.rb
jsonapionify-0.12.1 lib/jsonapionify/api/resource.rb
jsonapionify-0.12.0 lib/jsonapionify/api/resource.rb