Module: Api::Extensions::Expand
- Defined in:
- lib/api/extensions/expand.rb
Class Method Summary collapse
-
.included(base) ⇒ Object
Check requirements on inclusion.
Instance Method Summary collapse
-
#expand(resource, link) ⇒ Object
Actually expand a link.
-
#process_expand(keys, scope) ⇒ Object
Process the expand functional extension.
Class Method Details
.included(base) ⇒ Object
Check requirements on inclusion
10 11 12 |
# File 'lib/api/extensions/expand.rb', line 10 def self.included(base) raise ::Exception.new('Base class must implement a :get method to include the ExpandProcessor module') unless base.method_defined?(:get) end |
Instance Method Details
#expand(resource, link) ⇒ Object
Actually expand a link
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/api/extensions/expand.rb', line 44 def (resource, link) if resource['links'].include?(link) sub_resource_uri = resource['links'][link]['href'] sub_resource = get sub_resource_uri # 'self' links replace the original resource if link == 'self' resource = sub_resource else resource[link] = sub_resource end end resource end |
#process_expand(keys, scope) ⇒ Object
Process the expand functional extension
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/api/extensions/expand.rb', line 15 def (keys, scope) keys.split(',').each do |key| entries_parts = key.match(/entries\((.*)\)$/) if entries_parts && scope['entries'] # (2) Collective key: expand=entries(self) scope['entries'].collect! { |entry| entries_parts[1], entry } else recursive = key.match(/([^\(]+)\((.*)\)$/) if recursive # (1) Recursive key: expand=section(parent) local_key, sub_key = recursive[1, 2] # Expand the local key scope = scope, local_key # Recursively expand the remaining keys - if possible new_scope = local_key == 'self' ? scope : scope[local_key] (sub_key, new_scope) if new_scope else # (0) Simple key: expand=section scope = scope, key end end end scope end |