From b31df0fb3d297db1c1930251990bba808417f532 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 13 May 2015 15:32:08 -0400 Subject: [PATCH] Mordernize code base with __dir__ and require_relative --- example/server/liquid_servlet.rb | 2 +- example/server/server.rb | 8 +++----- lib/liquid.rb | 2 +- lib/liquid/i18n.rb | 2 +- lib/liquid/tags/unless.rb | 2 +- performance/benchmark.rb | 2 +- performance/profile.rb | 2 +- performance/shopify/database.rb | 2 +- performance/shopify/liquid.rb | 18 +++++++++--------- performance/theme_runner.rb | 6 +++--- test/test_helper.rb | 4 ++-- 11 files changed, 24 insertions(+), 26 deletions(-) diff --git a/example/server/liquid_servlet.rb b/example/server/liquid_servlet.rb index ac0483f..40d491e 100644 --- a/example/server/liquid_servlet.rb +++ b/example/server/liquid_servlet.rb @@ -24,6 +24,6 @@ class LiquidServlet < WEBrick::HTTPServlet::AbstractServlet end def read_template(filename = @action) - File.read( File.dirname(__FILE__) + "/templates/#{filename}.liquid" ) + File.read("#{__dir__}/templates/#{filename}.liquid") end end diff --git a/example/server/server.rb b/example/server/server.rb index 78cb7ff..e56edbd 100644 --- a/example/server/server.rb +++ b/example/server/server.rb @@ -1,11 +1,9 @@ require 'webrick' require 'rexml/document' -DIR = File.expand_path(File.dirname(__FILE__)) - -require DIR + '/../../lib/liquid' -require DIR + '/liquid_servlet' -require DIR + '/example_servlet' +require_relative '../../lib/liquid' +require_relative 'liquid_servlet' +require_relative 'example_servlet' # Setup webrick server = WEBrick::HTTPServer.new( :Port => ARGV[1] || 3000 ) diff --git a/lib/liquid.rb b/lib/liquid.rb index 393454f..7bf8de1 100644 --- a/lib/liquid.rb +++ b/lib/liquid.rb @@ -73,4 +73,4 @@ require 'liquid/token' # Load all the tags of the standard library # -Dir[File.dirname(__FILE__) + '/liquid/tags/*.rb'].each { |f| require f } +Dir["#{__dir__}/liquid/tags/*.rb"].each { |f| require f } diff --git a/lib/liquid/i18n.rb b/lib/liquid/i18n.rb index 250436b..5d538b7 100644 --- a/lib/liquid/i18n.rb +++ b/lib/liquid/i18n.rb @@ -2,7 +2,7 @@ require 'yaml' module Liquid class I18n - DEFAULT_LOCALE = File.join(File.expand_path(File.dirname(__FILE__)), "locales", "en.yml") + DEFAULT_LOCALE = File.join(File.expand_path(__dir__), "locales", "en.yml") TranslationError = Class.new(StandardError) diff --git a/lib/liquid/tags/unless.rb b/lib/liquid/tags/unless.rb index 8f7fe7a..dce3971 100644 --- a/lib/liquid/tags/unless.rb +++ b/lib/liquid/tags/unless.rb @@ -1,4 +1,4 @@ -require File.dirname(__FILE__) + '/if' +require_relative 'if' module Liquid # Unless is a conditional just like 'if' but works on the inverse logic. diff --git a/performance/benchmark.rb b/performance/benchmark.rb index 98c3985..a6f8185 100644 --- a/performance/benchmark.rb +++ b/performance/benchmark.rb @@ -1,5 +1,5 @@ require 'benchmark/ips' -require File.dirname(__FILE__) + '/theme_runner' +require_relative 'theme_runner' Liquid::Template.error_mode = ARGV.first.to_sym if ARGV.first profiler = ThemeRunner.new diff --git a/performance/profile.rb b/performance/profile.rb index 1feba24..6931774 100644 --- a/performance/profile.rb +++ b/performance/profile.rb @@ -1,5 +1,5 @@ require 'stackprof' rescue fail("install stackprof extension/gem") -require File.dirname(__FILE__) + '/theme_runner' +require_relative 'theme_runner' Liquid::Template.error_mode = ARGV.first.to_sym if ARGV.first profiler = ThemeRunner.new diff --git a/performance/shopify/database.rb b/performance/shopify/database.rb index 147ca05..8755684 100644 --- a/performance/shopify/database.rb +++ b/performance/shopify/database.rb @@ -5,7 +5,7 @@ module Database # to liquid as assigns. All this is based on Shopify def self.tables @tables ||= begin - db = YAML.load_file(File.dirname(__FILE__) + '/vision.database.yml') + db = YAML.load_file("#{__dir__}/vision.database.yml") # From vision source db['products'].each do |product| diff --git a/performance/shopify/liquid.rb b/performance/shopify/liquid.rb index 29be34a..7716deb 100644 --- a/performance/shopify/liquid.rb +++ b/performance/shopify/liquid.rb @@ -1,13 +1,13 @@ -$:.unshift File.dirname(__FILE__) + '/../../lib' -require File.dirname(__FILE__) + '/../../lib/liquid' +$:.unshift __dir__ + '/../../lib' +require_relative '../../lib/liquid' -require File.dirname(__FILE__) + '/comment_form' -require File.dirname(__FILE__) + '/paginate' -require File.dirname(__FILE__) + '/json_filter' -require File.dirname(__FILE__) + '/money_filter' -require File.dirname(__FILE__) + '/shop_filter' -require File.dirname(__FILE__) + '/tag_filter' -require File.dirname(__FILE__) + '/weight_filter' +require_relative 'comment_form' +require_relative 'paginate' +require_relative 'json_filter' +require_relative 'money_filter' +require_relative 'shop_filter' +require_relative 'tag_filter' +require_relative 'weight_filter' Liquid::Template.register_tag 'paginate', Paginate Liquid::Template.register_tag 'form', CommentForm diff --git a/performance/theme_runner.rb b/performance/theme_runner.rb index 5c959ac..7b15d03 100644 --- a/performance/theme_runner.rb +++ b/performance/theme_runner.rb @@ -6,8 +6,8 @@ # Shopify which is likely the biggest user of liquid in the world which something to the tune of several # million Template#render calls a day. -require File.dirname(__FILE__) + '/shopify/liquid' -require File.dirname(__FILE__) + '/shopify/database.rb' +require_relative 'shopify/liquid' +require_relative 'shopify/database' class ThemeRunner class FileSystem @@ -25,7 +25,7 @@ class ThemeRunner # Load all templates into memory, do this now so that # we don't profile IO. def initialize - @tests = Dir[File.dirname(__FILE__) + '/tests/**/*.liquid'].collect do |test| + @tests = Dir[__dir__ + '/tests/**/*.liquid'].collect do |test| next if File.basename(test) == 'theme.liquid' theme_path = File.dirname(test) + '/theme.liquid' diff --git a/test/test_helper.rb b/test/test_helper.rb index 32f8204..8006c9d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -4,7 +4,7 @@ ENV["MT_NO_EXPECTATIONS"] = "1" require 'minitest/autorun' require 'spy/integration' -$:.unshift(File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib')) +$:.unshift(File.join(File.expand_path(__dir__), '..', 'lib')) require 'liquid.rb' require 'liquid/profiler' @@ -25,7 +25,7 @@ end module Minitest class Test def fixture(name) - File.join(File.expand_path(File.dirname(__FILE__)), "fixtures", name) + File.join(File.expand_path(__dir__), "fixtures", name) end end