Sha256: e66323d6714e30a79f85a6e757b06f9ee34bfe51c6cdbb9d58ea585bcb79e712

Contents?: true

Size: 1.08 KB

Versions: 20

Compression:

Stored size: 1.08 KB

Contents

require "timber/contexts/custom"
require "timber/contexts/http"
require "timber/contexts/organization"
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

20 entries across 20 versions & 1 rubygems

Version Path
timber-2.0.22 lib/timber/contexts.rb
timber-2.0.21 lib/timber/contexts.rb
timber-2.0.20 lib/timber/contexts.rb
timber-2.0.19 lib/timber/contexts.rb
timber-2.0.17 lib/timber/contexts.rb
timber-2.0.16 lib/timber/contexts.rb
timber-2.0.15 lib/timber/contexts.rb
timber-2.0.14 lib/timber/contexts.rb
timber-2.0.12 lib/timber/contexts.rb
timber-2.0.11 lib/timber/contexts.rb
timber-2.0.10 lib/timber/contexts.rb
timber-2.0.9 lib/timber/contexts.rb
timber-2.0.8 lib/timber/contexts.rb
timber-2.0.7 lib/timber/contexts.rb
timber-2.0.6 lib/timber/contexts.rb
timber-2.0.5 lib/timber/contexts.rb
timber-2.0.4 lib/timber/contexts.rb
timber-2.0.3 lib/timber/contexts.rb
timber-2.0.2 lib/timber/contexts.rb
timber-2.0.1 lib/timber/contexts.rb