Module: JSONAPIonify::Api::Resource::Definitions::Scopes
- Defined in:
- lib/jsonapionify/api/resource/definitions/scopes.rb
Instance Method Summary collapse
- #collection(&block) ⇒ Object
- #instance(&block) ⇒ Object
- #new_instance(&block) ⇒ Object
- #scope(&block) ⇒ Object (also: #resource_class)
Instance Method Details
#collection(&block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/jsonapionify/api/resource/definitions/scopes.rb', line 32 def collection(&block) context :collection do |context, scope:, includes:| collection = instance_exec(scope, context, &block.destructure(object: context)) # 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 24 25 26 27 28 29 30 |
# 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(object: OpenStruct.new)) end context :instance, persisted: true do |context, scope:, id:, action: nil| case action&.name when :create new_instance when :list nil else instance_exec(scope, id, context, &block.destructure(object: context)) end end end |
#new_instance(&block) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/jsonapionify/api/resource/definitions/scopes.rb', line 49 def new_instance(&block) define_singleton_method(:build_instance) do Object.new.instance_exec(current_scope, &block.destructure(object: OpenStruct.new)) end context :new_instance, persisted: true, readonly: true do |context, scope:| Object.new.instance_exec(scope, context, &block.destructure(object: context)) 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 |