Sha256: 659758f71640ff77e9f9825f09139a06e3eb2f69b8f947d03bf23fd8206c41f3

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

module Eco
  module API
    module Organization
      class PolicyGroups <  Eco::Language::Models::Collection
        # build the shortcuts of Collection
        attr_collection :id, :name

        include Common::People

        def initialize(policy_groups = [], klass: INTERNAL::PolicyGroup)
          @klass = INTERNAL::PolicyGroup
          @caches_init = false
          super(policy_groups, klass: @klass)
          init_caches
        end

        def to_id(name)
          policy_group(name)&.id
        end

        def to_name(id)
          policy_group(id)&.name
        end

        def policy_group(id_name)
          @by_id.fetch(policy_group_id(id_name), nil)
        end

        private

        def policy_group_name(id_name)
          @by_id.fetch(id_name, nil)&.name&.downcase ||
          @by_name.fetch(id_name&.downcase, nil)&.name&.downcase
        end

        def policy_group_id(id_name)
          @by_name.fetch(id_name&.downcase, nil)&.id ||
          @by_id.fetch(id_name, nil)&.id
        end

        def init_caches
          return if @caches_init
          @by_id   = self.map { |pg| [pg.id, pg] }.to_h
          @by_name = self.map { |pg| [pg.name&.downcase, pg] }.to_h
          @caches_init = true
        end

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eco-helpers-0.6.2 lib/eco/api/organization/policy_groups.rb
eco-helpers-0.6.1 lib/eco/api/organization/policy_groups.rb
eco-helpers-0.6.0 lib/eco/api/organization/policy_groups.rb