Sha256: af85780134cc7707c7018787130849e00e612b5c25e147f66bbbf5b82814d4f9

Contents?: true

Size: 1.05 KB

Versions: 16

Compression:

Stored size: 1.05 KB

Contents

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

16 entries across 16 versions & 1 rubygems

Version Path
timber-2.0.0 lib/timber/contexts.rb
timber-1.1.14 lib/timber/contexts.rb
timber-1.1.13 lib/timber/contexts.rb
timber-1.1.12 lib/timber/contexts.rb
timber-1.1.11 lib/timber/contexts.rb
timber-1.1.10 lib/timber/contexts.rb
timber-1.1.9 lib/timber/contexts.rb
timber-1.1.8 lib/timber/contexts.rb
timber-1.1.7 lib/timber/contexts.rb
timber-1.1.6 lib/timber/contexts.rb
timber-1.1.5 lib/timber/contexts.rb
timber-1.1.4 lib/timber/contexts.rb
timber-1.1.3 lib/timber/contexts.rb
timber-1.1.2 lib/timber/contexts.rb
timber-1.1.1 lib/timber/contexts.rb
timber-1.1.0 lib/timber/contexts.rb