Sha256: 59e667c4e817b567fb51ddc880fc75b783a40fe0fb05831a1d13735b4cd5f3b6

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

module Datacaster
  module Config
    extend self

    attr_accessor :i18n_t
    attr_accessor :i18n_exists
    attr_accessor :i18n_module

    def add_predefined_caster(name, definition)
      caster =
        case definition
        when Proc
          Datacaster.partial_schema(&definition)
        when Base
          definition
        else
          raise ArgumentError.new("Expected Datacaster defintion lambda or Datacaster instance")
        end

      Predefined.define_method(name.to_sym) { caster }
    end

    def i18n_t
      if @i18n_t.nil? && @i18n_module.nil?
        i18n_initialize!
      end
      @i18n_t || ->(*args, **kwargs) { @i18n_module.t(*args, **kwargs) }
    end

    def i18n_exists?
      if @i18n_t.nil? && @i18n_module.nil?
        i18n_initialize!
      end
      @i18n_exists || ->(*args, **kwargs) { @i18n_module.exists?(*args, **kwargs) }
    end

    def i18n_initialize!
      @i18n_module ||=
        if defined?(::I18n)
          I18n
        else
          SubstituteI18n
        end
      @i18n_module.load_path += [__dir__ + '/../../config/locales/en.yml']
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
datacaster-3.1.3 lib/datacaster/config.rb
datacaster-3.1.2 lib/datacaster/config.rb
datacaster-3.1.1 lib/datacaster/config.rb
datacaster-3.1.0 lib/datacaster/config.rb
datacaster-3.0.0 lib/datacaster/config.rb