Module: Api::Extensions::Expand
- Defined in:
- lib/api/extensions/expand.rb
Class Method Summary collapse
-
.included(base) ⇒ Object
Add expand_with class method 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
Add expand_with class method on inclusion
10 11 12 13 14 |
# File 'lib/api/extensions/expand.rb', line 10 def self.included(base) @@fetch_method_for_expand = :get base.class_eval 'def self.expand_with(symbol); @@fetch_method_for_expand = symbol; end' end |
Instance Method Details
#expand(resource, link) ⇒ Object
Actually expand a link
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/api/extensions/expand.rb', line 46 def (resource, link) if resource['links'].include?(link) sub_resource_uri = resource['links'][link]['href'] sub_resource = send(, 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
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 42 43 |
# File 'lib/api/extensions/expand.rb', line 17 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 |