Sha256: 05084ee0d40e9540f041dffffce012d4652718e56ef7f4b641dff5adeb27c850

Contents?: true

Size: 1.31 KB

Versions: 23

Compression:

Stored size: 1.31 KB

Contents

module Eco
  module API
    class MicroCases
      # Finds all the subordinates of `supervisor`.
      # @note if `supervisor` is `nil`, it will return all people with no supervisor.
      # @param supervisor [String, Ecoportal::API::V1::Person, Hash] the `supervisor` or the `id` / `external_id` thereof.
      # @param people [Eco::API::Organization::People] target existing _People_ of the current update.
      # @yield [subordinate] block that does some stuff with `subordinate`.
      # @yieldparam subordinate [Ecoportal::API::V1::Person] each one of the suborinates of `supervisor`.
      # @return [Eco::API::Organization::People] the **subordinates** of `supervisor`.
      def with_each_subordinate(supervisor, people)
        people.supervisor_id(_person_id(supervisor, people)).tap do |subordinates|
          subordinates.each do |subordinate|
            yield(subordinate) if block_given?
          end
        end
      end

      private

      def _person_id(value, people)
        case value
        when Hash
          _person_id(value["id"] || value["external_id"], people)
        when Ecoportal::API::V1::Person
          _person_id(value.id    || value.external_id, people)
        when String
          micro.with_supervisor(value, people)&.id
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
eco-helpers-3.0.21 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.20 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.19 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.18 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.17 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.16 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.15 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.14 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.13 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.12 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.11 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.10 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.9 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.8 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.7 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.6 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.5 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.4 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.3 lib/eco/api/microcases/with_each_subordinate.rb
eco-helpers-3.0.2 lib/eco/api/microcases/with_each_subordinate.rb