lib/ecoportal/api/v2/page/force/bindings.rb in ecoportal-api-v2-0.8.26 vs lib/ecoportal/api/v2/page/force/bindings.rb in ecoportal-api-v2-0.8.27

- old
+ new

@@ -11,9 +11,66 @@ def ooze self._parent.ooze end + def force + self._parent + end + + # @note first local binding name will shadow later ones + # @param only_winner [Boolean] specifies if retrieving multiple bindings with same name or just the winner + # @return [Hash] where **key** is `name` and **value** is + # 1. a single _binding_, if `only_winner` is `true` + # 2. an `Array` of _bindings_ with same _name_, otherwise + def by_name(only_winner: false) + self.each_with_object({}) do |binding, hash| + if winner_only + hash[binding.name] ||= binding + else + hash[binding.name] ||= [] + hash[binding.name].push(binding) + end + end + end + + + # @note first local binding name will shadow later ones. + # @param only_winner [Boolean] specifies if shadowed bindings (inactive) should be excluded (`true`) + # @return [Hash] where **key** is a _section_ or a _component_ and **value** is eitheran `Array` of _bindings_. + def by_reference(only_winner: false) + if only_winner + by_name(only_winner: true).each_with_object({}) do |(name, binds), hash| + if binds.is_a?(Array) + binds.each {|binding| (hash[binding.reference] ||= []).push(binding)} + else + (hash[binds.reference] ||= []).push(binds) + end + end + else + self.each_with_object({}) do |binding, hash| + (hash[binding.reference] ||= []).push(binding) + end + end + end + + # @param obj [Ecoportal::API::V2::Page::Section, Ecoportal::API::V2::Page::Component)] the referred component or section. + # @return [Boolean] `true` if `obj` is referred in the bindings, `false` otherwise + def reference?(obj) + get_by_reference(obj).count > 0 + end + + # @param obj [Ecoportal::API::V2::Page::Section, Ecoportal::API::V2::Page::Component)] the referred component or section. + # @return [Array<Ecoportal::API::V2::Page::Force::Binding>] binding to the component or section. + def get_by_reference(obj) + unless obj.is_a?(Ecoportal::API::V2::Page::Section) || obj.is_a?(Ecoportal::API::V2::Page::Component) + raise ArgumentError.new("Expected either a Ecoportal::API::V2::Page::Section or a Ecoportal::API::V2::Page::Component. Given: #{obj.class}") + end + self.select do |bind| + bind.reference == obj + end + end + # @param id [String] the `id` of the binding to find. # @return [Ecoportal::API::V2::Page::Force::Binding] binding with `id` def get_by_id(id) self.find do |bind| bind.id == id