Class: RandomWords::Config

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(lang) ⇒ Config

Initialize the config with the given language

Parameters:

  • lang (Symbol)

    The language to use

Raises:

  • (RuntimeError)

    if no dictionary is found for 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

#dictionaryObject (readonly)

Dictionary for source



32
33
34
# File 'lib/random-words/config.rb', line 32

def dictionary
  @dictionary
end

#source_dirObject (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

Parameters:

  • dir (String)

    The directory to check

  • lang (String) (defaults to: nil)

    The language to check

Returns:

  • (Boolean)

    true if all parts of speech files exist, false otherwise



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_configHash

Return base configuration

Returns:

  • (Hash)

    The 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

Parameters:

  • lang (String) (defaults to: nil)

    The language to use

  • basedir (String) (defaults to: nil)

    The base directory to use

Returns:

  • (String, nil)

    The path to the builtin language directory or nil if not found



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

#configObject

The base configuration



187
188
189
# File 'lib/random-words/config.rb', line 187

def config
  @config ||= base_config
end

#config_dirString

The user configuration directory path

Returns:

  • (String)

    The path to the config directory



195
196
197
# File 'lib/random-words/config.rb', line 195

def config_dir
  @config_dir ||= File.expand_path(File.join(Dir.home, '.config', 'random-words'))
end

#config_dir=(dir) ⇒ String

Set the user configuration directory path

Parameters:

  • dir (String)

    The path to the config directory

Returns:

  • (String)

    The path to the config directory



202
203
204
# File 'lib/random-words/config.rb', line 202

def config_dir=(dir)
  @config_dir = File.expand_path(dir)
end

#config_fileString?

Look for a config.yml file in the config directory

Returns:

  • (String, nil)

    The path to the config.yml file or nil if not found



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

Parameters:

  • config_file (String)

    The path to the config.yml file

Returns:

  • (String)

    The path to the config.yml file



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

Parameters:

  • lang (String) (defaults to: nil)

    The language to create the dictionary for

Returns:

  • (Symbol, nil)

    The language symbol if successful, nil otherwise



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

Parameters:

  • configuration (Hash)

    The configuration hash

Returns:

  • (Hash)

    The converted configuration hash



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

#sourcesHash

List all sources available, builtin and custom

Returns:

  • (Hash)

    A hash of source names and their corresponding RandomWords::Source objects



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

Returns:

  • (Boolean)

    true if the user dictionary exists, false otherwise

Raises:

  • (RuntimeError)

    if the user dictionary is incomplete



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_dirString

The user language directory path

Returns:

  • (String)

    The path to the user language directory



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