Class: Garner::Strategies::Context::Key::Caller

Inherits:
Base
  • Object
show all
Defined in:
lib/garner/strategies/context/key/caller.rb

Class Method Summary collapse

Class Method Details

.apply(identity, ruby_context = self) ⇒ Garner::Cache::Identity

Injects the caller’s location into the key hash.

Parameters:

  • identity (Garner::Cache::Identity)

    The cache identity.

  • ruby_context (Object) (defaults to: self)

    An optional Ruby context.

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/garner/strategies/context/key/caller.rb', line 39

def self.apply(identity, ruby_context = self)
  value = nil

  if ruby_context.send(:caller)
    ruby_context.send(:caller).compact.each do |line|
      parts = line.match(/(?<filename>[^:]+)\:(?<lineno>[^:]+)/)
      file = (Pathname.new(parts[:filename]).realpath.to_s rescue nil)
      next if file.nil? || file == ""
      next if file.include?(File.join("lib", "garner"))

      if (root = Garner.config.caller_root)
        root += File::SEPARATOR unless root[-1] == File::SEPARATOR
        next unless file =~ /^#{root}/
        value = "#{file.gsub(root || "", "")}:#{parts[:lineno]}"
      else
        value = "#{file}:#{parts[:lineno]}"
      end

      break
    end
  end

  value ? identity.key(field => value) : identity
end

.default_rootString

Determine the most likely root path for the current Garner client application. If in a Rails application, Rails.root is used. Otherwise, the nearest ancestor directory containing a Gemfile is used.

Returns:

  • (String)

    The default root path.

See Also:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/garner/strategies/context/key/caller.rb', line 19

def self.default_root
  if defined?(::Rails) && ::Rails.respond_to?(:root)
    ::Rails.root
  else
    # Try to use the nearest ancestor directory containing a Gemfile.
    requiring_caller = send(:caller).detect do |line|
      !line.include?(File.join("lib", "garner"))
    end
    return nil unless requiring_caller

    requiring_file = requiring_caller.split(":")[0]
    gemfile_root(File.dirname(requiring_file))
  end
end

.fieldObject



7
8
9
# File 'lib/garner/strategies/context/key/caller.rb', line 7

def self.field
  :caller
end