mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 00:05:42 +03:00
Compare commits
2 Commits
pz-float-v
...
usage-trac
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cbea19b59f | ||
|
|
0521d78b30 |
@@ -75,7 +75,12 @@ require 'liquid/utils'
|
||||
require 'liquid/tokenizer'
|
||||
require 'liquid/parse_context'
|
||||
require 'liquid/partial_cache'
|
||||
require 'liquid/usage'
|
||||
|
||||
# Load all the tags of the standard library
|
||||
#
|
||||
Dir["#{__dir__}/liquid/tags/*.rb"].each { |f| require f }
|
||||
|
||||
# Load all usage tracking
|
||||
#
|
||||
Dir["#{__dir__}/liquid/usages/*.rb"].each { |f| require f }
|
||||
|
||||
23
lib/liquid/usage.rb
Normal file
23
lib/liquid/usage.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
module Liquid
|
||||
# Usage is used to store
|
||||
class Usage
|
||||
@messages = {}
|
||||
class << self
|
||||
def enable
|
||||
Liquid::Context.send(:alias_method, :try_variable_find_in_environments, :try_variable_find_in_environments_usage)
|
||||
end
|
||||
|
||||
def disable
|
||||
Liquid::Context.send(:alias_method, :try_variable_find_in_environments, :try_variable_find_in_environments_original)
|
||||
end
|
||||
|
||||
def track(message)
|
||||
@messages[message] = true
|
||||
end
|
||||
|
||||
def results
|
||||
@messages
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
26
lib/liquid/usages/try_variables.rb
Normal file
26
lib/liquid/usages/try_variables.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
module Liquid
|
||||
class Context
|
||||
alias try_variable_find_in_environments_original try_variable_find_in_environments
|
||||
|
||||
def try_variable_find_in_environments_usage(key, raise_on_not_found:)
|
||||
Usage.track("Using try_variable_find_in_environment")
|
||||
@environments.each do |environment|
|
||||
found_variable = lookup_and_evaluate(environment, key, raise_on_not_found: raise_on_not_found)
|
||||
if !found_variable.nil? || @strict_variables && raise_on_not_found
|
||||
return found_variable
|
||||
end
|
||||
|
||||
Usage.track("try_variable_find_in_environment reports Nil but responds to key") if environment.key?(key)
|
||||
end
|
||||
@static_environments.each do |environment|
|
||||
found_variable = lookup_and_evaluate(environment, key, raise_on_not_found: raise_on_not_found)
|
||||
if !found_variable.nil? || @strict_variables && raise_on_not_found
|
||||
return found_variable
|
||||
end
|
||||
|
||||
Usage.track("try_variable_find_in_environment reports Nil but responds to key") if environment.key?(key)
|
||||
end
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
1
lib/liquid/usages/usage_enabled.rb
Normal file
1
lib/liquid/usages/usage_enabled.rb
Normal file
@@ -0,0 +1 @@
|
||||
Liquid::Usage.track("Usage is enabled")
|
||||
14
test/integration/usages/try_variables_test.rb
Normal file
14
test/integration/usages/try_variables_test.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
require 'test_helper'
|
||||
|
||||
class TryVariablesUsageTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
def test_test_usages
|
||||
Usage.enable
|
||||
template = Template.parse(%({{test}}))
|
||||
assert_equal 'worked', template.render!('test' => 'worked')
|
||||
assert_equal 'worked wonderfully', template.render!('test' => 'worked wonderfully')
|
||||
assert_equal true, Usage.results["Using try_variable_find_in_environment"]
|
||||
Usage.disable
|
||||
end
|
||||
end
|
||||
9
test/integration/usages/usage_enabled_test.rb
Normal file
9
test/integration/usages/usage_enabled_test.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
require 'test_helper'
|
||||
|
||||
class UsageEnabledUsageTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
def test_live_usages
|
||||
assert_equal true, Usage.results["Usage is enabled"]
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user