Class: Garner::Strategies::Context::Key::Caller
- Defined in:
- lib/garner/strategies/context/key/caller.rb
Class Method Summary collapse
-
.apply(identity, ruby_context = self) ⇒ Garner::Cache::Identity
Injects the caller’s location into the key hash.
-
.default_root ⇒ String
Determine the most likely root path for the current Garner client application.
- .field ⇒ Object
Class Method Details
.apply(identity, ruby_context = self) ⇒ Garner::Cache::Identity
Injects the caller’s location into the key hash.
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_root ⇒ String
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.
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 |
.field ⇒ Object
7 8 9 |
# File 'lib/garner/strategies/context/key/caller.rb', line 7 def self.field :caller end |