Sha256: 6cc79ae5006d08d25b3e29a614c508fdff639665d61b074a9c9890dd99a00354

Contents?: true

Size: 1.12 KB

Versions: 34

Compression:

Stored size: 1.12 KB

Contents

require "timber/contexts/custom"
require "timber/contexts/http"
require "timber/contexts/organization"
require "timber/contexts/release"
require "timber/contexts/runtime"
require "timber/contexts/session"
require "timber/contexts/system"
require "timber/contexts/user"

module Timber
  # Namespace for all Timber supported Contexts.
  module Contexts
    # Protocol for casting objects into a {Timber::Context}.
    #
    # @example Casting a hash
    #   Timber::Contexts.build(deploy: {version: "1.0.0"})
    def self.build(obj)
      if obj.is_a?(::Timber::Context)
        obj
      elsif obj.respond_to?(:to_timber_context)
        obj.to_timber_context
      elsif obj.is_a?(Hash) && obj.length == 1
        type = obj.keys.first
        data = obj.values.first

        Contexts::Custom.new(
          type: type,
          data: data
        )
      elsif obj.is_a?(Struct) && obj.respond_to?(:type)
        Contexts::Custom.new(
          type: obj.type,
          data: obj.respond_to?(:to_h) ? obj.to_h : Timber::Util::Struct.to_hash(obj) # ruby 1.9.3 does not have to_h
        )
      else
        nil
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
timber-2.6.2 lib/timber/contexts.rb
timber-2.6.1 lib/timber/contexts.rb
timber-2.6.0 lib/timber/contexts.rb
timber-2.6.0.pre.beta2 lib/timber/contexts.rb
timber-2.6.0.pre.beta1 lib/timber/contexts.rb
timber-2.5.1 lib/timber/contexts.rb
timber-2.5.0 lib/timber/contexts.rb
timber-2.4.0 lib/timber/contexts.rb
timber-2.3.4 lib/timber/contexts.rb
timber-2.3.3 lib/timber/contexts.rb
timber-2.3.2 lib/timber/contexts.rb
timber-2.3.1 lib/timber/contexts.rb
timber-2.3.0 lib/timber/contexts.rb
timber-2.2.3 lib/timber/contexts.rb
timber-2.2.2 lib/timber/contexts.rb
timber-2.2.1 lib/timber/contexts.rb
timber-2.2.0 lib/timber/contexts.rb
timber-2.1.10 lib/timber/contexts.rb
timber-2.1.9 lib/timber/contexts.rb
timber-2.1.8 lib/timber/contexts.rb