Class: Kobold::Config
- Inherits:
-
Object
- Object
- Kobold::Config
- Defined in:
- lib/Kobold/config.rb
Overview
Reads, writes, validates, and generates .kobold TOML configuration files.
Constant Summary collapse
- REQUIRED_KEYS =
%w[repo source].freeze
- OPTIONAL_KEYS =
%w[dir branch commit label].freeze
- ALL_DEP_KEYS =
(REQUIRED_KEYS + OPTIONAL_KEYS).freeze
Instance Attribute Summary collapse
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
-
#format_version ⇒ Object
readonly
Returns the value of attribute format_version.
-
#includes ⇒ Object
readonly
Returns the value of attribute includes.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .generate(dependencies: [], includes: []) ⇒ Object
- .read(path) ⇒ Object
- .validate(data) ⇒ Object
- .write(path, config) ⇒ Object
Instance Method Summary collapse
- #add_dependency(name, dep) ⇒ Object
- #dependency(name) ⇒ Object
- #dependency?(name) ⇒ Boolean
- #each_dependency(&block) ⇒ Object
-
#initialize(data:, path: nil) ⇒ Config
constructor
A new instance of Config.
- #remove_dependency(name) ⇒ Object
- #to_h ⇒ Object
- #update_dependency(name, key, value) ⇒ Object
Constructor Details
#initialize(data:, path: nil) ⇒ Config
Returns a new instance of Config.
38 39 40 41 42 43 44 45 |
# File 'lib/Kobold/config.rb', line 38 def initialize(data:, path: nil) self.class.validate(data) @path = path kobold = data["kobold"] @format_version = kobold["format_version"] @includes = kobold.fetch("includes", []) @dependencies = data.fetch("dependencies", {}) end |
Instance Attribute Details
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
12 13 14 |
# File 'lib/Kobold/config.rb', line 12 def dependencies @dependencies end |
#format_version ⇒ Object (readonly)
Returns the value of attribute format_version.
12 13 14 |
# File 'lib/Kobold/config.rb', line 12 def format_version @format_version end |
#includes ⇒ Object (readonly)
Returns the value of attribute includes.
12 13 14 |
# File 'lib/Kobold/config.rb', line 12 def includes @includes end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/Kobold/config.rb', line 12 def path @path end |
Class Method Details
.generate(dependencies: [], includes: []) ⇒ Object
25 26 27 28 29 |
# File 'lib/Kobold/config.rb', line 25 def self.generate(dependencies: [], includes: []) data = build_base_data(includes) dependencies.each { |dep| add_dep_to_data(data, dep) } new(data: data) end |
.read(path) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/Kobold/config.rb', line 14 def self.read(path) full_path = File.(path) raise Errors::ConfigError, "Config file not found: #{full_path}" unless File.file?(full_path) new(data: TomlRB.load_file(full_path), path: full_path) end |
.validate(data) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/Kobold/config.rb', line 31 def self.validate(data) validate_kobold_section(data) validate_includes(data["kobold"]) validate_dependencies(data["dependencies"]) true end |
.write(path, config) ⇒ Object
21 22 23 |
# File 'lib/Kobold/config.rb', line 21 def self.write(path, config) File.write(path, TomlRB.dump(config.to_h)) end |
Instance Method Details
#add_dependency(name, dep) ⇒ Object
58 59 60 |
# File 'lib/Kobold/config.rb', line 58 def add_dependency(name, dep) @dependencies[name] = dep.each_with_object({}) { |(k, v), h| h[k.to_s] = v unless v.nil? } end |
#dependency(name) ⇒ Object
70 71 72 |
# File 'lib/Kobold/config.rb', line 70 def dependency(name) @dependencies[name] end |
#dependency?(name) ⇒ Boolean
66 67 68 |
# File 'lib/Kobold/config.rb', line 66 def dependency?(name) @dependencies.key?(name) end |
#each_dependency(&block) ⇒ Object
54 55 56 |
# File 'lib/Kobold/config.rb', line 54 def each_dependency(&block) @dependencies.each(&block) end |
#remove_dependency(name) ⇒ Object
62 63 64 |
# File 'lib/Kobold/config.rb', line 62 def remove_dependency(name) @dependencies.delete(name) end |
#to_h ⇒ Object
47 48 49 50 51 52 |
# File 'lib/Kobold/config.rb', line 47 def to_h h = { "kobold" => { "format_version" => @format_version } } h["kobold"]["includes"] = @includes unless @includes.empty? h["dependencies"] = @dependencies unless @dependencies.empty? h end |
#update_dependency(name, key, value) ⇒ Object
74 75 76 77 78 |
# File 'lib/Kobold/config.rb', line 74 def update_dependency(name, key, value) raise Errors::DependencyNotFound, name unless @dependencies.key?(name) value.nil? ? @dependencies[name].delete(key.to_s) : @dependencies[name][key.to_s] = value end |