Class: RandomWords::Config
- Inherits:
-
Object
- Object
- RandomWords::Config
- Defined in:
- lib/random-words/config.rb
Overview
Configuration
Constant Summary collapse
- CONFIG_FILES =
Language config files
%w[ config numbers ].freeze
- SPEECH_PARTS =
Parts of speech
%w[ adjectives adverbs articles-plural articles-singular clauses conjunctions-coordinating conjunctions-subordinate nouns-plural nouns-singular prepositions terminators verbs-passive verbs-plural verbs-singular ].freeze
Instance Attribute Summary collapse
-
#dictionary ⇒ Object
readonly
Dictionary for source.
-
#source_dir ⇒ Object
readonly
Source directory for languages.
Instance Method Summary collapse
-
#all_parts_of_speech?(dir, lang = nil) ⇒ Boolean
Check if all parts of speech files exist in the given directory.
-
#base_config ⇒ Hash
Return base configuration.
-
#builtin_lang_dir(lang = nil, basedir: nil) ⇒ String?
The builtin language directory path.
-
#config ⇒ Object
The base configuration.
-
#config_dir ⇒ String
The user configuration directory path.
-
#config_dir=(dir) ⇒ String
Set the user configuration directory path.
-
#config_file ⇒ String?
Look for a config.yml file in the config directory.
-
#create_base_config(config_file) ⇒ String
Create a base config.yml file if it doesn’t exist.
-
#create_user_dictionary(lang = nil) ⇒ Symbol?
Create a user dictionary for the given language.
-
#handle_config(configuration) ⇒ Hash
Convert a config file’s options to regular config.
-
#initialize(lang) ⇒ Config
constructor
Initialize the config with the given language.
-
#sources ⇒ Hash
List all sources available, builtin and custom.
-
#user_dictionary_exist? ⇒ Boolean
Tests if a uer dictionary exists.
-
#user_lang_dir ⇒ String
The user language directory path.
Constructor Details
#initialize(lang) ⇒ Config
Initialize the config with the given language
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/random-words/config.rb', line 40 def initialize(lang) @lang = lang.to_s FileUtils.mkdir_p(config_dir) unless File.directory?(config_dir) @source_dir = user_dictionary_exist? ? user_lang_dir : builtin_lang_dir raise "No dictionary found for #{@lang}" unless @source_dir rw_source = RandomWords::Source.new(@lang, @source_dir) @dictionary = rw_source.dictionary end |
Instance Attribute Details
#dictionary ⇒ Object (readonly)
Dictionary for source
32 33 34 |
# File 'lib/random-words/config.rb', line 32 def dictionary @dictionary end |
#source_dir ⇒ Object (readonly)
Source directory for languages
35 36 37 |
# File 'lib/random-words/config.rb', line 35 def source_dir @source_dir end |
Instance Method Details
#all_parts_of_speech?(dir, lang = nil) ⇒ Boolean
Check if all parts of speech files exist in the given directory
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/random-words/config.rb', line 92 def all_parts_of_speech?(dir, lang = nil) lang ||= @lang dir ||= @source_dir exists = true SPEECH_PARTS.each do |part| next if File.exist?(File.join(dir, "#{part}.txt")) warn "Missing #{File.join(dir, "#{part}.txt")} for #{lang}" exists = false break end CONFIG_FILES.each do |file| unless File.exist?(File.join(dir, "#{file}.yml")) warn "Missing #{File.join(dir, "#{file}.yml")} for #{lang}" exists = false end end exists end |
#base_config ⇒ Hash
Return base configuration
221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/random-words/config.rb', line 221 def base_config config_file = File.join(config_dir, 'config.yml') if File.exist?(config_file) config = YAML.load_file(config_file).symbolize_keys return handle_config(config) end # If the config file doesn't exist, create it # and return the default configuration create_base_config(config_file) config = YAML.load_file(config_file).symbolize_keys handle_config(config) end |
#builtin_lang_dir(lang = nil, basedir: nil) ⇒ String?
The builtin language directory path
78 79 80 81 82 83 84 85 86 |
# File 'lib/random-words/config.rb', line 78 def builtin_lang_dir(lang = nil, basedir: nil) lang ||= @lang basedir ||= __dir__ dir = File.join(basedir, 'words', lang) return dir if File.directory?(dir) warn "No dictionary found for #{lang}" nil end |
#config ⇒ Object
The base configuration
187 188 189 |
# File 'lib/random-words/config.rb', line 187 def config @config ||= base_config end |
#config_dir ⇒ String
The user configuration directory path
195 196 197 |
# File 'lib/random-words/config.rb', line 195 def config_dir @config_dir ||= File.(File.join(Dir.home, '.config', 'random-words')) end |
#config_dir=(dir) ⇒ String
Set the user configuration directory path
202 203 204 |
# File 'lib/random-words/config.rb', line 202 def config_dir=(dir) @config_dir = File.(dir) end |
#config_file ⇒ String?
Look for a config.yml file in the config directory
236 237 238 239 240 241 242 |
# File 'lib/random-words/config.rb', line 236 def config_file config_file = File.join(config_dir, 'config.yml') return config_file if File.exist?(config_file) create_base_config(config_file) config_file end |
#create_base_config(config_file) ⇒ String
Create a base config.yml file if it doesn’t exist
247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/random-words/config.rb', line 247 def create_base_config(config_file) FileUtils.mkdir_p(config_dir) unless File.directory?(config_dir) config = { 'source' => 'latin', 'length' => 'medium', 'paragraph_length' => 5, 'extended_punctuation' => false } File.write(config_file, config.to_yaml) warn "Created #{config_file}" config_file end |
#create_user_dictionary(lang = nil) ⇒ Symbol?
Create a user dictionary for the given language
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/random-words/config.rb', line 115 def create_user_dictionary(lang = nil) return lang.to_sym if File.directory?(File.join(config_dir, 'words', lang)) && all_parts_of_speech?( File.join(config_dir, 'words', lang), lang ) lang_dir = File.join(config_dir, 'words', lang) FileUtils.mkdir_p(lang_dir) unless File.directory?(lang_dir) SPEECH_PARTS.each do |part| source_file = File.join(builtin_lang_dir('english'), "#{part}.txt") target_file = File.join(lang_dir, "#{part}.txt") unless File.exist?(target_file) FileUtils.cp(source_file, target_file) warn "Created #{part}.txt" end end # Copy numbers.yml file from the builtin directory source_file = File.join(builtin_lang_dir('english'), 'numbers.yml') target_file = File.join(lang_dir, 'numbers.yml') unless File.exist?(target_file) FileUtils.cp(source_file, target_file) warn 'Created numbers.yml' end # Create the config.yml file if it doesn't exist target_file = File.join(lang_dir, 'config.yml') unless File.exist?(target_file) config = { 'name' => lang, 'triggers' => [lang], 'description' => "User dictionary for #{lang}" } File.write(target_file, config.to_yaml) warn "Created #{target_file}" end unless all_parts_of_speech?(lang_dir, lang) || (RandomWords.testing && !RandomWords.tested.include?('create_user_dictionary')) return end RandomWords.tested << 'create_user_dictionary' warn "Created #{lang} in #{lang_dir}" lang.to_sym end |
#handle_config(configuration) ⇒ Hash
Convert a config file’s options to regular config
209 210 211 212 213 214 215 216 217 |
# File 'lib/random-words/config.rb', line 209 def handle_config(configuration) ext_punc = configuration[:extended_punctuation] { source: configuration[:source].to_source || :latin, sentence_length: configuration[:length].to_length || :medium, paragraph_length: configuration[:paragraph_length].to_i || 5, use_extended_punctuation: ext_punc&.trueish? || false } end |
#sources ⇒ Hash
List all sources available, builtin and custom
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/random-words/config.rb', line 167 def sources return @sources if @sources @sources = {} Dir[File.join(__dir__, 'words', '*')].each do |dir| next unless File.directory?(dir) name = File.basename(dir) @sources[name] = RandomWords::Source.new(name, dir) end Dir[File.join(config_dir, 'words', '*')].each do |dir| next unless File.directory?(dir) name = File.basename(dir) @sources[name] = RandomWords::Source.new(name, dir) end @sources end |
#user_dictionary_exist? ⇒ Boolean
Tests if a uer dictionary exists
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/random-words/config.rb', line 56 def user_dictionary_exist? if user_lang_dir raise "User dictionary for #{@lang} is incomplete. Please run create_user_dictionary." unless all_parts_of_speech?( user_lang_dir, @lang ) true else false end end |
#user_lang_dir ⇒ String
The user language directory path
70 71 72 |
# File 'lib/random-words/config.rb', line 70 def user_lang_dir File.join(config_dir, 'words', @lang) if File.exist?(File.join(config_dir, 'words', @lang)) end |