Skip to content

Commit

Permalink
Refactore: method to get current locale
Browse files Browse the repository at this point in the history
  • Loading branch information
sota-horiuchi committed Mar 26, 2024
1 parent ce01093 commit 72046b1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/ingreedy/amount_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class AmountParser < Parslet::Parser
private

def current_locale
Ingreedy.dictionaries.current_locale
Ingreedy.dictionaries.current.locale
end

def word_digits
Expand Down
5 changes: 3 additions & 2 deletions lib/ingreedy/dictionary.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module Ingreedy
class Dictionary
attr_reader :units, :numbers, :prepositions, :range_separators
attr_reader :units, :numbers, :prepositions, :range_separators, :locale
attr_reader :imprecise_amounts

def initialize(units:, numbers: {}, prepositions: [], range_separators: %w{- ~}, imprecise_amounts: [])
def initialize(units:, numbers: {}, prepositions: [], range_separators: %w{- ~}, imprecise_amounts: [], locale: nil)
@units = units
@numbers = sort_by_length(numbers)
@prepositions = prepositions
@range_separators = range_separators
@imprecise_amounts = imprecise_amounts
@locale = locale
end

# https://en.wikipedia.org/wiki/Number_Forms
Expand Down
18 changes: 5 additions & 13 deletions lib/ingreedy/dictionary_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,21 @@ def initialize
end

def []=(locale, attributes)
@collection[locale] = Dictionary.new(**attributes)
@collection[locale] = Dictionary.new(locale:, **attributes)
end

def current
fetch_dictionary(current_locale)
end

def current_locale
find_locale
end

private

def find_locale
candidate_locales.each do |locale|
if dictionary = fetch_dictionary(locale)
return locale
return dictionary
end
end

raise "No dictionary found for locales: #{candidate_locales}"
end

private

def candidate_locales
Array(Ingreedy.locale || i18n_gem_locales || :en)
end
Expand All @@ -46,7 +38,7 @@ def i18n_gem_locales
end

def fetch_dictionary(locale)
@collection[locale] ||= Dictionary.new **load_yaml(locale)
@collection[locale] ||= Dictionary.new(locale:, **load_yaml(locale))
rescue Errno::ENOENT
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ingreedy/root_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def parse
attr_reader :original_query

def current_locale
Ingreedy.dictionaries.current_locale
Ingreedy.dictionaries.current.locale
end

def imprecise_amounts
Expand Down

0 comments on commit 72046b1

Please sign in to comment.