Sha256: 2dfd22ebe41d257ce95694d5c8cea219e22e9e5db5c1e47bd08ba905328e8fc1

Contents?: true

Size: 1.99 KB

Versions: 4

Compression:

Stored size: 1.99 KB

Contents

module Bisu
  class Config
    def initialize(hash:)
      @hash = hash.deep_symbolize
      @hash.validate_structure!(CONFIG_STRUCT)

      unless dict_struct = DICTIONARY_STRUCT[@hash[:dictionary][:type]]
        raise ArgumentError.new("unknown dictionary type '#{@hash[:dictionary][:type]}'")
      end

      @hash[:dictionary].validate_structure!(dict_struct)
    end

    def to_h
      @hash
    end

    def dictionary
      @hash[:dictionary]
    end

    def type
      @hash[:type]
    end

    def localize_files
      @hash[:translate].each do |t|
        @hash[:languages].each do |l|
          downcase_locale = l[:locale].downcase.gsub("-", "_").gsub(" ", "_")
          yield(t[:in], (t[:"out_#{downcase_locale}"] || t[:out]) % l, l[:language], l[:locale])
        end
      end
    end

    private

    CONFIG_STRUCT = {
      type: Hash,
      elements: {
        type: { type: String },
        dictionary: { type: Hash, elements: {
          type: { type: String }
        } },
        translate: { type: Array, elements: {
          type: Hash, elements: {
            in: { type: String },
            out: { type: String }
          }
        } },
        languages: { type: Array, elements: {
          type: Hash, elements: {
            locale: { type: String },
            language: { type: String }
          }
        } }
      }
    }

    GOOGLE_SHEET_STRUCT = {
      type: Hash,
      elements: {
        type: { type: String },
        sheet_id: { type: String },
        keys_column: { type: String }
      }
    }

    ONE_SKY_STRUCT = {
      type: Hash,
      elements: {
        api_key: { type: String },
        api_secret: { type: String },
        project_id: { type: Integer },
        file_name: { type: String }
      }
    }

    URL_STRUCT = {
      type: Hash,
      elements: {
        url: { type: String }
      }
    }

    DICTIONARY_STRUCT = {
      "google_sheet" => GOOGLE_SHEET_STRUCT,
      "one_sky"      => ONE_SKY_STRUCT,
      "url"          => URL_STRUCT
    }
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bisu-1.7.3 lib/bisu/config.rb
bisu-1.7.2 lib/bisu/config.rb
bisu-1.7.1 lib/bisu/config.rb
bisu-1.7.0 lib/bisu/config.rb