Sha256: b3525cd1033f34f4b9ee00faa49187f28a1e082a547d9d79147151553f479a13

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

class Sinclair
  module Settable
    # @api public
    #
    # Class responsible for casting the values
    #
    # New types can be added
    #
    # @example Adding a casting type to {Sinclair::Settable::Caster Sinclair::Settable::Caster}
    #   class Sinclair
    #     module Settable
    #       class Caster
    #         cast_with(:squared) { |value| value.to_f**2 }
    #       end
    #     end
    #   end
    #
    #   class MathEnvSettings
    #     extend Sinclair::EnvSettable
    #
    #     settings_prefix 'MATH'
    #
    #     setting_with_options :value, type: :squared
    #   end
    #
    #   ENV['MATH_VALUE'] = '80'
    #
    #   settable.value # returns 6400.0
    #
    # @example Creating a new caster
    #   module JsonEnvSettable
    #     include Sinclair::EnvSettable
    #     extend Sinclair::Settable::ClassMethods
    #
    #     class Caster < Sinclair::Settable::Caster
    #       cast_with(:json) { |value| JSON.parse(value) }
    #     end
    #   end
    #
    #   class JsonEnvSettings
    #     extend JsonEnvSettable
    #
    #     settings_prefix 'JSON'
    #
    #     setting_with_options :config, type: :json
    #   end
    #
    #   hash = { key: 'value' }
    #
    #   ENV['JSON_CONFIG'] = hash.to_json
    #
    #   settable.config # returns { 'key' => 'value' }
    class Caster < Sinclair::Caster
      cast_with(:integer, &:to_i)
      cast_with(:string, &:to_s)
      cast_with(:float, &:to_f)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sinclair-2.1.1 lib/sinclair/settable/caster.rb
sinclair-2.1.0 lib/sinclair/settable/caster.rb