Module: Ecoportal::API::Common::Content::DoubleModel::Attributable::Nesting::CascadedCallback
- Defined in:
- lib/ecoportal/api/common/content/double_model/attributable/nesting/cascaded_callback.rb
Overview
Note:
this happens at instance level (not tracked at class level)
We offer helpers to implement cascaded callbacks to nested objects
i.e. #as_update or #dirty?
Instance Method Summary collapse
Instance Method Details
#cascaded_callback(value = nil, method:, args: [], kargs: {}, path: [], &block) ⇒ Object
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 |
# File 'lib/ecoportal/api/common/content/double_model/attributable/nesting/cascaded_callback.rb', line 51 def cascaded_callback(value = nil, method:, args: [], kargs: {}, path: [], &block) method = method.to_sym return value unless respond_to?(method, true) value = if path.empty? send(method, *args, **kargs) else yield(value, send(method, *args, **kargs), path, self) end return value unless respond_to?(:_cascaded_attributes) _cascaded_attributes.reduce(value) do |mem, (attribute, obj_k)| next mem unless (obj = send(attribute)) next mem unless obj.respond_to(method, true) obj_path = path.dup.push(obj_k) unless obj.respond_to?(:cascaded_callback) next yield( mem, obj.send(method, *args, **kargs), obj_path, obj ) end obj.cascaded_callback( mem, method: method, path: obj_path, &block ) end end |