Sha256: 8307d95af51965b0234468702f73d86a503138a9e5bd10e99ccbd578abcee28f

Contents?: true

Size: 729 Bytes

Versions: 1

Compression:

Stored size: 729 Bytes

Contents

module Ddr
  Language = Struct.new(:code, :label, keyword_init: true) do

    def self.config
      @config ||= YAML.load_file(::File.expand_path('../../../config/aux/language.yml', __dir__))
    end

    def self.keystore
      @keystore ||= Hash[config.map { |entry| [entry['code'], new(entry).freeze] }].freeze
    end

    def self.all
      keystore
    end

    def self.call(obj)
      obj.language.map { |lang| find_by_code(lang) }
    end

    def self.find_by_code(code)
      return unless code

      keystore.fetch(code)
    rescue KeyError => _
      raise Ddr::NotFoundError, "Language code '#{code}' not found."
    end

    def self.codes
      keystore.keys
    end

    def to_s
      label
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ddr-core-1.13.1 app/models/ddr/language.rb