Module: Timber::Contexts

Defined in:
lib/timber/contexts.rb,
lib/timber/contexts/http.rb,
lib/timber/contexts/user.rb,
lib/timber/contexts/custom.rb,
lib/timber/contexts/system.rb,
lib/timber/contexts/runtime.rb,
lib/timber/contexts/organization.rb

Overview

Namespace for all Timber supported Contexts.

Defined Under Namespace

Classes: Custom, HTTP, Organization, Runtime, System, User

Class Method Summary collapse

Class Method Details

.build(obj) ⇒ Object

Protocol for casting objects into a ‘Timber::Context`.

Examples:

Casting a hash

Timber::Contexts.build(deploy: {version: "1.0.0"})


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/timber/contexts.rb', line 15

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