Class: PackAPI::Types::AggregateType
- Defined in:
- lib/pack_api/types/aggregate_type.rb
Overview
Define a type that blends attributes from multiple resources into a single data structure. For each kind of input resource, pass a block to the ‘combine_attributes` method, wherein are enumerated the attributes in the output data structure which originate from that input resource.
Example:
class MyAggregateType < PackAPI::Types::AggregateType
combine_attributes from: :resource_one do
attribute :id, Types::String
attribute :name, Types::String
end
combine_attributes from: :resource_two do
attribute :description, Types::String
attribute :created_at, Types::Time
end
end
This will create a type with attributes ‘id`, `name`, `description`, and `created_at`, where:
- `id` and `name` come from `resource_one`
- `description` and `created_at` come from `resource_two`
For each input resource, helper methods must be defined to enable the CRUD operations:
- query_<resource>s(**params) - returns a hash of resources, indexed by their IDs
- get_<resource>(id) - returns a single resource, identified by its ID
- create_<resource>(params) - creates a new resource and returns it
- update_<resource>(id, params) - updates an existing resource and returns it
- delete_<resource>(id)
For example, for the above example, you would need to define:
- query_resource_ones
- get_resource_one
- create_resource_one
- update_resource_one
- delete_resource_one
@note: There are 2 kinds of resources:
- primary resources
- secondary resources
The first resource drawn from via ‘combine_attributes` is considered the primary resource.
Defined Under Namespace
Classes: AttributeBlender
Class Method Summary collapse
- .attribute(name, type = Undefined, &block) ⇒ Object
- .combine_attributes(from:, &block) ⇒ Object
- .create(params) ⇒ Object
- .delete(id) ⇒ Object
- .get(id) ⇒ Object
- .inherited(subclass) ⇒ Object
- .query(**params) ⇒ Object
- .update(id, params) ⇒ Object
Methods inherited from BaseType
filterable_attributes, #merge, optional_attribute, optional_attributes, #to_data
Class Method Details
.attribute(name, type = Undefined, &block) ⇒ Object
57 58 59 60 |
# File 'lib/pack_api/types/aggregate_type.rb', line 57 def attribute(name, type = Undefined, &block) super @next_attribute_list << name if @next_attribute_list end |
.combine_attributes(from:, &block) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/pack_api/types/aggregate_type.rb', line 62 def combine_attributes(from:, &block) @next_attribute_list = [] block.call @attribute_sources[from] = @next_attribute_list @next_attribute_list = nil end |
.create(params) ⇒ Object
81 82 83 |
# File 'lib/pack_api/types/aggregate_type.rb', line 81 def create(params) new(attribute_blender.create(params)) end |
.delete(id) ⇒ Object
85 86 87 |
# File 'lib/pack_api/types/aggregate_type.rb', line 85 def delete(id) attribute_blender.delete(id) end |
.get(id) ⇒ Object
73 74 75 |
# File 'lib/pack_api/types/aggregate_type.rb', line 73 def get(id) new(attribute_blender.get(id)) end |
.inherited(subclass) ⇒ Object
51 52 53 54 55 |
# File 'lib/pack_api/types/aggregate_type.rb', line 51 def inherited(subclass) subclass.instance_variable_set(:@attribute_sources, {}) subclass.instance_variable_set(:@next_attribute_list, nil) super end |
.query(**params) ⇒ Object
69 70 71 |
# File 'lib/pack_api/types/aggregate_type.rb', line 69 def query(**params) attribute_blender.query(**params).map { new(it) } end |
.update(id, params) ⇒ Object
77 78 79 |
# File 'lib/pack_api/types/aggregate_type.rb', line 77 def update(id, params) new(attribute_blender.update(id, params)) end |