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
15 16 17 18 |
# File 'lib/tiny_client/nested_support.rb', line 15 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
64 65 66 67 |
# File 'lib/tiny_client/nested_support.rb', line 64 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
55 56 57 58 59 |
# File 'lib/tiny_client/nested_support.rb', line 55 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
47 48 49 50 51 52 |
# File 'lib/tiny_client/nested_support.rb', line 47 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
30 31 32 33 |
# File 'lib/tiny_client/nested_support.rb', line 30 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
22 23 24 25 26 |
# File 'lib/tiny_client/nested_support.rb', line 22 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
37 38 39 40 41 42 43 |
# File 'lib/tiny_client/nested_support.rb', line 37 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 |