class Translatomatic::Config::Selector
Selects which set of config settings to use
Constants
- LOCATIONS
valid location list in order of precedence
Attributes
for_file[R]
location[R]
Public Class Methods
new(settings, default_location, params = {})
click to toggle source
# File lib/translatomatic/config/selector.rb, line 8 def initialize(settings, default_location, params = {}) @settings = settings @for_file = params[:for_file] @location = params[:location] @default_location = default_location end
Public Instance Methods
settings_for_read(key)
click to toggle source
Find a settings object for reading the specified option @param key [Symbol] Option
name @return [LocationSettings] settings
# File lib/translatomatic/config/selector.rb, line 18 def settings_for_read(key) if location.nil? # no location, find first settings for key according to precedence settings_with_precedence(key) else # location is set check_valid_location location_settings_for_read(key, location) end end
settings_for_write(key)
click to toggle source
Find a settings object for writing the specified option @param key [Symbol] Option
name @return [LocationSettings] settings
# File lib/translatomatic/config/selector.rb, line 32 def settings_for_write(key) effective = effective_location(key, location) settings = @settings[effective] if for_file path = file(settings) data = settings.files[path.to_s.to_sym] ||= {} file_location_settings(settings, data) else settings end end
Private Instance Methods
check_valid_location()
click to toggle source
# File lib/translatomatic/config/selector.rb, line 76 def check_valid_location valid = valid_location? raise t('config.invalid_location', location: location) unless valid end
effective_location(key, loc)
click to toggle source
# File lib/translatomatic/config/selector.rb, line 70 def effective_location(key, loc) effective = loc || @default_location effective = :user if Options.option(key).user_location_only effective end
file(settings)
click to toggle source
# File lib/translatomatic/config/selector.rb, line 91 def file(settings) path = Pathname.new(for_file) unless path.relative? settings_path = Pathname.new(settings.path) path = path.relative_path_from(settings_path) end path end
file_location_settings(settings, data)
click to toggle source
# File lib/translatomatic/config/selector.rb, line 117 def file_location_settings(settings, data) options = { path: settings.path, location: settings.location, no_files: true } LocationSettings.new(data, options) end
for_file_settings(settings)
click to toggle source
# File lib/translatomatic/config/selector.rb, line 85 def for_file_settings(settings) return nil unless for_file data = merged_file_data(settings) file_location_settings(settings, data) end
location_settings_for_read(key, loc)
click to toggle source
# File lib/translatomatic/config/selector.rb, line 60 def location_settings_for_read(key, loc) effective = effective_location(key, loc) settings = @settings[effective] file_settings = for_file_settings(settings) [file_settings, settings].each do |i| return i if i && i.include?(key) end return nil end
merged_file_data(settings)
click to toggle source
find matching file configurations
# File lib/translatomatic/config/selector.rb, line 101 def merged_file_data(settings) merged_data = {} file = file(settings) paths = settings.files.keys.collect(&:to_s) paths.sort_by(&:length).each do |path| next unless path_match?(file, path) merged_data.merge!(settings.files[path.to_sym]) end merged_data end
path_match?(file, path)
click to toggle source
check if file is equal to or a child of the given path
# File lib/translatomatic/config/selector.rb, line 113 def path_match?(file, path) file.to_s == path || file.to_s.start_with?(path.to_s) end
settings_with_precedence(key)
click to toggle source
# File lib/translatomatic/config/selector.rb, line 51 def settings_with_precedence(key) # find the first setting found by precedence LOCATIONS.each do |loc| settings = location_settings_for_read(key, loc) return settings if settings end nil end
valid_location?()
click to toggle source
# File lib/translatomatic/config/selector.rb, line 81 def valid_location? location.present? && LOCATIONS.include?(location.to_sym) end