lib/braid/config.rb in braid-1.1.6 vs lib/braid/config.rb in braid-1.1.7

- old
+ new

@@ -1,5 +1,6 @@ +# typed: true require 'yaml' require 'json' require 'yaml/store' # Some info about the configuration versioning design: @@ -46,35 +47,47 @@ # commit, but you don't need to because people can just run `git log` on this # file.) module Braid class Config + extend T::Sig + # TODO (typing): Migrate to T::Enum? + ConfigMode = T.type_alias { Integer } + MODE_UPGRADE = 1 MODE_READ_ONLY = 2 MODE_MAY_WRITE = 3 CURRENT_CONFIG_VERSION = 1 class PathAlreadyInUse < BraidError + sig {returns(String)} def message "path already in use: #{super}" end end class MirrorDoesNotExist < BraidError + sig {returns(String)} def message "mirror does not exist: #{super}" end end class RemoveMirrorDueToBreakingChange < StandardError end # For upgrade-config command only. XXX: Ideally would be immutable. - attr_reader :config_version, :config_existed, :breaking_change_descs + sig {returns(Integer)} + attr_reader :config_version + sig {returns(T::Boolean)} + attr_reader :config_existed + sig {returns(T::Array[String])} + attr_reader :breaking_change_descs # options: config_file, old_config_files, mode + sig {params(options: T.untyped).void} def initialize(options = {}) @config_file = options['config_file'] || CONFIG_FILE old_config_files = options['old_config_files'] || [OLD_CONFIG_FILE] @mode = options['mode'] || MODE_MAY_WRITE @@ -148,51 +161,59 @@ MSG end end + sig {params(url: String, options: T.untyped).returns(Mirror)} def add_from_options(url, options) mirror = Mirror.new_from_options(url, options) add(mirror) mirror end + sig {returns(T::Array[Mirror])} def mirrors @db.keys end + sig {params(path: String).returns(T.nilable(Mirror))} def get(path) key = path.to_s.sub(/\/$/, '') attributes = @db[key] attributes ? Mirror.new(path, attributes) : nil end + sig {params(path: String).returns(Mirror)} def get!(path) mirror = get(path) raise MirrorDoesNotExist, path unless mirror mirror end + sig {params(mirror: Mirror).void} def add(mirror) raise PathAlreadyInUse, mirror.path if get(mirror.path) write_mirror(mirror) write_db end + sig {params(mirror: Mirror).void} def remove(mirror) @db.delete(mirror.path) write_db end + sig {params(mirror: Mirror).void} def update(mirror) raise MirrorDoesNotExist, mirror.path unless get(mirror.path) write_mirror(mirror) write_db end # Public for upgrade-config command only. + sig {void} def write_db new_db = {} @db.keys.sort.each do |key| new_db[key] = {} Braid::Mirror::ATTRIBUTES.each do |k| @@ -209,15 +230,16 @@ end end private + sig {params(config_file: String, old_config_files: T::Array[String]).returns(T.untyped)} def load_config(config_file, old_config_files) (old_config_files + [config_file]).each do |file| next unless File.exist?(file) begin - store = YAML::Store.new(file) + store = T.let(YAML::Store, T.untyped).new(file) data = {} store.transaction(true) do store.roots.each do |path| data[path] = store[path] end @@ -226,17 +248,19 @@ rescue data = JSON.parse(file) return data if data end end - return nil + nil end + sig {params(mirror: Mirror).void} def write_mirror(mirror) @db[mirror.path] = clean_attributes(mirror.attributes) end + sig {params(hash: T::Hash[String, T.untyped]).returns(T::Hash[String, T.untyped])} def clean_attributes(hash) - hash.reject { |k, v| v.nil? } + hash.reject { |_, v| v.nil? } end end end