Sha256: e4c720c65453b76e48499adac67ecb2c0b9e37c3ca7dfae2bb25f52b104f2ecf

Contents?: true

Size: 1.39 KB

Versions: 131

Compression:

Stored size: 1.39 KB

Contents

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

        def initialize(schemas = [], klass: Ecoportal::API::Internal::PersonSchema)
          @klass       = Ecoportal::API::Internal::PersonSchema
          @caches_init = false
          super(schemas, klass: @klass)
          init_caches
        end

        def to_id(name)
          case name
          when Enumerable
            name.map do |n|
              schema(n)&.id
            end.compact
          else
            schema(name)&.id
          end
        end

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

        def schema(id_name)
          self[id_name]
        end

        def [](id_name)
          @by_id[schema_id(id_name)]
        end

        private

        def schema_name(id_name)
          (@by_id[id_name] || @by_name[id_name&.downcase])&.name&.downcase
        end

        def schema_id(id_name)
          (@by_name[id_name&.downcase] || @by_id[id_name])&.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

131 entries across 131 versions & 1 rubygems

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