Module: Garner::Strategies::Keys::Caller

Defined in:
lib/garner/strategies/keys/caller_strategy.rb

Overview

Injects the caller’s location into the key.

Class Method Summary collapse

Class Method Details

.apply(key, context = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/garner/strategies/keys/caller_strategy.rb', line 12

def apply(key, context = {})
  rc = key ? key.dup : {}
  if context.keys.include?(field)
    rc[field] = context[field]
    return rc
  end

  if caller
    caller.each do |line|
      next unless line
      split = line.split(":")
      next unless split && split.length >= 2
      path = (Pathname.new(split[0]).realpath.to_s rescue nil)
      next if (! path) || path.empty? || path.include?("lib/garner")
      next unless path.include?("/app/") || path.include?("/spec/")
      rc[field] = "#{path}:#{split[1]}"
      break
    end
  end

  rc
end

.fieldObject



8
9
10
# File 'lib/garner/strategies/keys/caller_strategy.rb', line 8

def field
  :caller
end