Sha256: 05921e287eb4060d4b9cca58b7a0a96c45e552eb3cb6f5751c478c916764a1e3

Contents?: true

Size: 1.34 KB

Versions: 89

Compression:

Stored size: 1.34 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)
          @by_id.fetch(schema_id(id_name), nil)
        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

89 entries across 89 versions & 1 rubygems

Version Path
eco-helpers-2.0.18 lib/eco/api/organization/person_schemas.rb
eco-helpers-2.0.17 lib/eco/api/organization/person_schemas.rb
eco-helpers-2.0.16 lib/eco/api/organization/person_schemas.rb
eco-helpers-2.0.15 lib/eco/api/organization/person_schemas.rb
eco-helpers-2.0.14 lib/eco/api/organization/person_schemas.rb
eco-helpers-2.0.13 lib/eco/api/organization/person_schemas.rb
eco-helpers-2.0.12 lib/eco/api/organization/person_schemas.rb
eco-helpers-2.0.11 lib/eco/api/organization/person_schemas.rb
eco-helpers-2.0.10 lib/eco/api/organization/person_schemas.rb
eco-helpers-2.0.9 lib/eco/api/organization/person_schemas.rb
eco-helpers-2.0.8 lib/eco/api/organization/person_schemas.rb
eco-helpers-2.0.7 lib/eco/api/organization/person_schemas.rb
eco-helpers-2.0.6 lib/eco/api/organization/person_schemas.rb
eco-helpers-2.0.5 lib/eco/api/organization/person_schemas.rb
eco-helpers-2.0.4 lib/eco/api/organization/person_schemas.rb
eco-helpers-2.0.3 lib/eco/api/organization/person_schemas.rb
eco-helpers-2.0.2 lib/eco/api/organization/person_schemas.rb
eco-helpers-1.5.15 lib/eco/api/organization/person_schemas.rb
eco-helpers-1.5.14 lib/eco/api/organization/person_schemas.rb
eco-helpers-1.5.13 lib/eco/api/organization/person_schemas.rb