Module: JSONAPIonify::Api::Resource::Definitions::Scopes

Defined in:
lib/jsonapionify/api/resource/definitions/scopes.rb

Instance Method Summary collapse

Instance Method Details

#collection(&block) ⇒ Object



24
25
26
27
28
# File 'lib/jsonapionify/api/resource/definitions/scopes.rb', line 24

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

#instance(&block) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/jsonapionify/api/resource/definitions/scopes.rb', line 15

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

#new_instance(&block) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/jsonapionify/api/resource/definitions/scopes.rb', line 30

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

#scope(&block) ⇒ Object Also known as: resource_class



4
5
6
7
8
9
10
11
# File 'lib/jsonapionify/api/resource/definitions/scopes.rb', line 4

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