Module: Safrano::ExpandHandler
- Included in:
- ServiceBase
- Defined in:
- lib/safrano/service.rb
Constant Summary collapse
- PATH_SPLITTER =
%r{\A(\w+)/?(.*)\z}.freeze
- DEFERRED =
'__deferred'
- URI =
'uri'
- RESULTS_K =
default v2 overriden in ServiceV1
'results'
- COUNT_K =
'__count'
Instance Method Summary collapse
- #get_coll_odata_h(array:, template:, icount: nil) ⇒ Object
- #get_deferred_odata_h(entity_uri:, attrib:) ⇒ Object
- #get_entity_odata_h(entity:, template:) ⇒ Object
-
#get_entity_odata_link_h(entity:) ⇒ Object
handle $links …
Instance Method Details
#get_coll_odata_h(array:, template:, icount: nil) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/safrano/service.rb', line 34 def get_coll_odata_h(array:, template:, icount: nil) array.map! do |w| get_entity_odata_h(entity: w, template: template) end icount ? { RESULTS_K => array, COUNT_K => icount } : { RESULTS_K => array } end |
#get_deferred_odata_h(entity_uri:, attrib:) ⇒ Object
26 27 28 |
# File 'lib/safrano/service.rb', line 26 def get_deferred_odata_h(entity_uri:, attrib:) { DEFERRED => { URI => "#{entity_uri}/#{attrib}" } } end |
#get_entity_odata_h(entity:, template:) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/safrano/service.rb', line 47 def get_entity_odata_h(entity:, template:) hres = {} # finalise the template according to options # (eg. skip metadata and or deferred...) @final_template_func.call(template) template.each do |elmt, arg| case elmt when :meta hres[METADATA_K] = entity. when :all_values hres.merge! entity.casted_values when :selected_vals hres.merge! entity.casted_values(arg) when :expand_e arg.each do |attr, templ| enval = entity.send(attr) hres[attr] = if enval get_entity_odata_h(entity: enval, template: templ) else # FK is NULL --> nav_value is nil --> return empty json EMPTYH end end when :expand_c arg.each do |attr, templ| next unless (encoll = entity.send(attr)) # nav attributes that are a collection (x..n) hres[attr] = get_coll_odata_h(array: encoll, template: templ) # else error ? end when :deferr euri = entity.uri arg.each do |attr| hres[attr] = get_deferred_odata_h(entity_uri: euri, attrib: attr) end end end hres end |
#get_entity_odata_link_h(entity:) ⇒ Object
handle $links … Note: $expand seems to be ignored when $links are requested
43 44 45 |
# File 'lib/safrano/service.rb', line 43 def get_entity_odata_link_h(entity:) { uri: entity.uri } end |