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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jsonapionify/api/resource/definitions/scopes.rb', line 25

def collection(&block)
  context :collection do |context, scope:, includes:|
    collection = instance_exec(scope, context, &block.destructure)

    # Compute includes manipulations
    self.class.include_definitions.select do |relationship, _|
      includes.keys.include? relationship.to_s
    end.reduce(collection) do |lv, (name, include_block)|
      lv.instance_exec(lv, includes[name.to_s], &include_block)
    end

    # TODO: Compute field manipulations

    # TODO: Compute param manipulations
  end
end

#instance(&block) ⇒ Object



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

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

#new_instance(&block) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/jsonapionify/api/resource/definitions/scopes.rb', line 42

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

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



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

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