Module: TinyClient::NestedSupport
- Included in:
- Resource
- Defined in:
- lib/tiny_client/nested_support.rb
Overview
Mixin that add support for nested resource to Resource Each nested resource will be accessible with:
<resource_name>s # List the existing ( index )
<resource_name>(id) # Show an existing ( show )
add_<resource_name>(resource) # To create a new one ( post )
remove_<resource_name>(resource) # Remove an existing ( delete )
update_<resource_name>(resource) # Update an existing ( put )
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #nested_all(resource_class, params = {}) ⇒ Object
- #nested_create(resource) ⇒ Object
- #nested_delete(resource) ⇒ Object
- #nested_index(resource_class, params = {}) ⇒ Object
- #nested_show(resource_class, id, params = {}) ⇒ Object
- #nested_update(resource) ⇒ Object
Class Method Details
.included(resource_class) ⇒ Object
13 14 15 16 |
# File 'lib/tiny_client/nested_support.rb', line 13 def self.included(resource_class) raise ArgumentError, 'Works only for TinyClient::Resource' unless resource_class <= Resource resource_class.extend(ClassMethods) end |
Instance Method Details
#nested_all(resource_class, params = {}) ⇒ Object
62 63 64 65 |
# File 'lib/tiny_client/nested_support.rb', line 62 def nested_all(resource_class, params = {}) raise ArgumentError, 'Works only for TinyClient::Resource' unless resource_class <= Resource self.class.get_all(params, @id, resource_class.path, resource_class) end |
#nested_create(resource) ⇒ Object
53 54 55 56 57 |
# File 'lib/tiny_client/nested_support.rb', line 53 def nested_create(resource) raise ArgumentError, 'resource must be an TinyClient::Resource' unless resource.is_a? Resource data = resource.changes.to_a.each_with_object({}) { |fld, h| h[fld] = resource.send(fld) } self.class.post({ resource.class.low_name => data }, @id, resource.class.path, resource.class) end |
#nested_delete(resource) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/tiny_client/nested_support.rb', line 45 def nested_delete(resource) raise ArgumentError, 'resource must be an TinyClient::Resource' unless resource.is_a? Resource raise ArgumentError, 'resource must have id set' if resource.id.nil? path = UrlBuilder.url(resource.class.path).path(resource.id).build! self.class.delete(@id, path, resource.class) end |
#nested_index(resource_class, params = {}) ⇒ Object
28 29 30 31 |
# File 'lib/tiny_client/nested_support.rb', line 28 def nested_index(resource_class, params = {}) raise ArgumentError, 'Works only for TinyClient::Resource' unless resource_class <= Resource self.class.get(params, @id, resource_class.path, resource_class) end |
#nested_show(resource_class, id, params = {}) ⇒ Object
20 21 22 23 24 |
# File 'lib/tiny_client/nested_support.rb', line 20 def nested_show(resource_class, id, params = {}) raise ArgumentError, 'Works only for TinyClient::Resource' unless resource_class <= Resource path = UrlBuilder.url(resource_class.path).path(id).build! self.class.get(params, @id, path, resource_class) end |
#nested_update(resource) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/tiny_client/nested_support.rb', line 35 def nested_update(resource) raise ArgumentError, 'resource must be an TinyClient::Resource' unless resource.is_a? Resource raise ArgumentError, 'resource must have id set' if resource.id.nil? path = UrlBuilder.url(resource.class.path).path(resource.id).build! data = resource.changes.to_a.each_with_object({}) { |fld, h| h[fld] = resource.send(fld) } self.class.put({ resource.class.low_name => data }, @id, path, resource.class) end |