Class: Multilocale::ConfigFile
- Inherits:
-
Object
- Object
- Multilocale::ConfigFile
- Defined in:
- lib/multilocale/config_file.rb
Overview
multilocale.json — the same file the npm CLI reads, so a repository that uses both tools describes its project once.
{
"projectId": "website",
"defaultLocale": "en",
"locales": ["en", "es", "fr"],
"paths": ["config/locales/%lang%.yml"]
}
projectId accepts an id or a name, exactly like the CLI's --project.
Paths are resolved relative to the config file, not to the working
directory: rake from a subdirectory writes the same files as rake from
the root. (The npm CLI resolves them against the process cwd instead.)
Constant Summary collapse
- FILENAME =
"multilocale.json"
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
-
.discover(directory = Dir.pwd) ⇒ Object
Walks up from
directorylooking for multilocale.json, the way git finds .git. - .load(path) ⇒ Object
Instance Method Summary collapse
- #default_locale ⇒ Object
- #directory ⇒ Object
- #format ⇒ Object
- #header ⇒ Object
-
#initialize(path, data) ⇒ ConfigFile
constructor
A new instance of ConfigFile.
- #locales ⇒ Object
-
#nested? ⇒ Boolean
Gem-only key.
-
#paths ⇒ Object
The npm CLI's
importandunusedcommands destructure this without a default and crash with "Cannot read properties of undefined" when it is missing; this gem answers with an empty list and lets the caller decide. - #paths! ⇒ Object
-
#project ⇒ Object
Id or name.
- #project! ⇒ Object
Constructor Details
#initialize(path, data) ⇒ ConfigFile
Returns a new instance of ConfigFile.
26 27 28 29 |
# File 'lib/multilocale/config_file.rb', line 26 def initialize(path, data) @path = path @data = data end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
24 25 26 |
# File 'lib/multilocale/config_file.rb', line 24 def data @data end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
24 25 26 |
# File 'lib/multilocale/config_file.rb', line 24 def path @path end |
Class Method Details
.discover(directory = Dir.pwd) ⇒ Object
Walks up from directory looking for multilocale.json, the way git finds
.git. Returns nil rather than raising: callers decide whether a missing
config is fatal.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/multilocale/config_file.rb', line 34 def self.discover(directory = Dir.pwd) current = File.(directory) loop do candidate = File.join(current, FILENAME) return load(candidate) if File.exist?(candidate) parent = File.dirname(current) return nil if parent == current current = parent end end |
.load(path) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/multilocale/config_file.rb', line 48 def self.load(path) raise ConfigurationError, "No such config file: #{path}" unless File.exist?(path) data = begin JSON.parse(File.read(path)) rescue JSON::ParserError => error raise ConfigurationError, "#{path} is not valid JSON: #{error.}" end new(File.(path), data) end |
Instance Method Details
#default_locale ⇒ Object
71 72 73 |
# File 'lib/multilocale/config_file.rb', line 71 def default_locale data["defaultLocale"] end |
#directory ⇒ Object
61 62 63 |
# File 'lib/multilocale/config_file.rb', line 61 def directory File.dirname(path) end |
#format ⇒ Object
86 87 88 |
# File 'lib/multilocale/config_file.rb', line 86 def format data["format"] end |
#header ⇒ Object
90 91 92 |
# File 'lib/multilocale/config_file.rb', line 90 def header data["header"] end |
#locales ⇒ Object
75 76 77 |
# File 'lib/multilocale/config_file.rb', line 75 def locales data["locales"] end |
#nested? ⇒ Boolean
Gem-only key. Absent from the CLI's vocabulary, which ignores unknown
keys, so adding it here does not break npx multilocale.
96 97 98 |
# File 'lib/multilocale/config_file.rb', line 96 def nested? data.fetch("nested", true) end |
#paths ⇒ Object
The npm CLI's import and unused commands destructure this without a
default and crash with "Cannot read properties of undefined" when it is
missing; this gem answers with an empty list and lets the caller decide.
82 83 84 |
# File 'lib/multilocale/config_file.rb', line 82 def paths Array(data["paths"]) end |
#paths! ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/multilocale/config_file.rb', line 115 def paths! values = paths if values.empty? raise ConfigurationError, <<~MESSAGE #{path} has no "paths". Add at least one path template containing #{LocaleFile::PLACEHOLDER}: { "paths": ["config/locales/#{LocaleFile::PLACEHOLDER}.yml"] } MESSAGE end values end |
#project ⇒ Object
Id or name. Named projectId in the file for CLI compatibility even
though a name is equally valid there.
67 68 69 |
# File 'lib/multilocale/config_file.rb', line 67 def project data["projectId"] || data["project"] end |
#project! ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/multilocale/config_file.rb', line 100 def project! value = project if value.nil? || value.to_s.empty? raise ConfigurationError, <<~MESSAGE #{path} does not say which project it describes. Add the project id or name: { "projectId": "website", "locales": ["en", "es"], "paths": ["config/locales/%lang%.yml"] } MESSAGE end value end |