Skip to content

Commit

Permalink
small optimization (used a few less objects)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jun 5, 2018
1 parent ea081c9 commit 18baa8b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/pagy.rb
Expand Up @@ -16,7 +16,7 @@ def self.root; Pathname.new(__FILE__).dirname end

# merge and validate the options, do some simple aritmetic and set the instance variables
def initialize(vars)
@vars = VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned instance vars
@vars = VARS.merge(vars.delete_if{|_,v| v.nil? || v == ''.freeze }) # default vars + cleaned instance vars
{ count:0, items:1, outset:0, page:1 }.each do |k,min| # validate core variables
(@vars[k] && instance_variable_set(:"@#{k}", @vars.delete(k).to_i) >= min) \
or raise(ArgumentError, "expected :#{k} >= #{min}; got #{instance_variable_get(:"@#{k}").inspect}")
Expand Down
10 changes: 5 additions & 5 deletions lib/pagy/frontend.rb
Expand Up @@ -36,7 +36,7 @@ def pagy_info(pagy)

# this works with all Rack-based frameworks (Sinatra, Padrino, Rails, ...)
def pagy_url_for(page, pagy)
params = request.GET.merge(pagy.vars[:page_param] => page).merge!(pagy.vars[:params])
params = request.GET.merge(pagy.vars[:page_param] => page, **pagy.vars[:params])
"#{request.path}?#{Rack::Utils.build_nested_query(pagy_get_params(params))}#{pagy.vars[:anchor]}"
end

Expand All @@ -57,15 +57,15 @@ def pagy_link_proc(pagy, link_extra=''.freeze)
end

# Pagy::Frontend::I18N constant
zero_one = [:zero, :one]; I18N = { plurals: -> (c) {(zero_one[c] || :other).to_s.freeze}, data: {}}
def I18N.load_file(file) I18N[:data].replace(YAML.load_file(file).first[1]) end
I18N[:data] = I18N.load_file(Pagy.root.join('locales', 'pagy.yml'))
I18N_DATA = YAML.load_file(Pagy.root.join('locales', 'pagy.yml')).first[1]
zero_one = ['zero'.freeze, 'one'.freeze]; I18N = { plurals: -> (c) {zero_one[c] || 'other'.freeze}}
def I18N.load_file(file) I18N_DATA.replace(YAML.load_file(file).first[1]) end

# Similar to I18n.t for interpolation and pluralization but without translation
# Use only for single-language apps: it is specialized for pagy and 5x faster than I18n.t
# See also https://ddnexus.github.io/pagy/extras/i18n to use the standard I18n gem instead
def pagy_t(path, vars={})
value = I18N[:data].dig(*path.to_s.split('.'.freeze)) or return %(translation missing: "#{path}")
value = I18N_DATA.dig(*path.to_s.split('.'.freeze)) or return %(translation missing: "#{path}")
if value.is_a?(Hash)
vars.key?(:count) or return value
plural = I18N[:plurals].call(vars[:count])
Expand Down
6 changes: 3 additions & 3 deletions test/pagy/frontend_test.rb
Expand Up @@ -122,8 +122,8 @@ def test_link_extras

describe "#pagy_t" do
def test_data
assert_equal "‹ Prev", Pagy::Frontend::I18N[:data]['pagy']['nav']['prev']
assert_equal "…", Pagy::Frontend::I18N[:data]['pagy']['nav']['gap']
assert_equal "‹ Prev", Pagy::Frontend::I18N_DATA['pagy']['nav']['prev']
assert_equal "…", Pagy::Frontend::I18N_DATA['pagy']['nav']['gap']
end

def test_translation
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_render_info_no_118n_key
end

def test_render_info_with_existing_118n_key
Pagy::Frontend::I18N[:data]['pagy']['info']['product'] = { 'zero' => 'Products',
Pagy::Frontend::I18N_DATA['pagy']['info']['product'] = { 'zero' => 'Products',
'one' => 'Product',
'other' => 'Products' }
pagy = Pagy.new count: 0, item_path: 'pagy.info.product'
Expand Down

0 comments on commit 18baa8b

Please sign in to comment.