Sha256: 876643a584d77aec793f726478cdb1f1fc094172b0b9754cacfd04baf1a7345f

Contents?: true

Size: 1010 Bytes

Versions: 1

Compression:

Stored size: 1010 Bytes

Contents

module JSONAPIonify::Api
  module Resource::Definitions::Scopes

    def scope(&block)
      define_singleton_method(:current_scope) do
        Object.new.instance_eval(&block)
      end
      context :scope do
        self.class.current_scope
      end
    end

    alias_method :resource_class, :scope

    def instance(&block)
      define_singleton_method(:find_instance) do |id|
        instance_exec(current_scope, id, OpenStruct.new, &block)
      end
      context :instance do |context|
        instance_exec(context.scope, context.id, context, &block)
      end
    end

    def collection(&block)
      context :collection do |context|
        Object.new.instance_exec(context.scope, context, &block)
      end
    end

    def new_instance(&block)
      define_singleton_method(:build_instance) do
        Object.new.instance_exec(current_scope, &block)
      end
      context :new_instance do |context|
        Object.new.instance_exec(context.scope, context, &block)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jsonapionify-0.9.0 lib/jsonapionify/api/resource/definitions/scopes.rb