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.1.7 lib/timber/contexts.rb
timber-2.1.6 lib/timber/contexts.rb
timber-2.1.5 lib/timber/contexts.rb
timber-2.1.4 lib/timber/contexts.rb
timber-2.1.3 lib/timber/contexts.rb
timber-2.1.2 lib/timber/contexts.rb
timber-2.1.1 lib/timber/contexts.rb
timber-2.1.0 lib/timber/contexts.rb
timber-2.1.0.rc6 lib/timber/contexts.rb
timber-2.1.0.rc5 lib/timber/contexts.rb
timber-2.1.0.rc4 lib/timber/contexts.rb
timber-2.1.0.rc3 lib/timber/contexts.rb
timber-2.1.0.rc2 lib/timber/contexts.rb
timber-2.1.0.rc1 lib/timber/contexts.rb