Sha256: ce3eafe6ed922fb9456fbf64b7b44e010779a0bb8c4ed24029e968982c1d3a2b

Contents?: true

Size: 1.47 KB

Versions: 8

Compression:

Stored size: 1.47 KB

Contents

module Trailblazer
  module Deprecation
    class ContextWithIndifferentAccess < Trailblazer::Context
      def [](key)
        return super unless Trailblazer::Operation::PublicCall.deprecatable?(key)
        key, _ = Trailblazer::Operation::PublicCall.deprecate_string(key, nil)
        super(key)
      end

      def []=(key, value)
        return super unless Trailblazer::Operation::PublicCall.deprecatable?(key)
        key, _ = Trailblazer::Operation::PublicCall.deprecate_string(key, nil)
        super(key, value)
      end
    end
  end
end

Trailblazer::Operation::PublicCall.module_eval do
  def self.options_for_public_call(options={}, *containers)
    hash_transformer = ->(containers) { containers[0].to_hash } # FIXME: don't transform any containers into kw args.

    options = deprecate_strings(options)

    immutable_options = Trailblazer::Context::ContainerChain.new( [options, *containers], to_hash: hash_transformer ) # Runtime options, immutable.

    Trailblazer::Deprecation::ContextWithIndifferentAccess.new(immutable_options, {})
  end

  def self.deprecatable?(key)
    key.is_a?(String) && key.split(".").size == 1
  end

  def self.deprecate_strings(options)
    ary = options.collect { |k,v| deprecatable?(k) ? deprecate_string(k, v) : [k,v]  }
    Hash[ary]
  end

  def self.deprecate_string(key, value)
    warn "[Trailblazer] Using a string key for non-namespaced keys is deprecated. Please use `:#{key}` instead of `#{key.inspect}`."
    [ key.to_sym, value ]
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
trailblazer-2.1.0.rc1 lib/trailblazer/deprecation/context.rb
trailblazer-2.1.0.beta7 lib/trailblazer/deprecation/context.rb
trailblazer-2.1.0.beta6 lib/trailblazer/deprecation/context.rb
trailblazer-2.1.0.beta5 lib/trailblazer/deprecation/context.rb
trailblazer-2.1.0.beta4 lib/trailblazer/deprecation/context.rb
trailblazer-2.1.0.beta3 lib/trailblazer/deprecation/context.rb
trailblazer-2.1.0.beta2 lib/trailblazer/deprecation/context.rb
trailblazer-2.1.0.beta1 lib/trailblazer/deprecation/context.rb