Class: Yoker::Configuration
- Inherits:
-
Object
- Object
- Yoker::Configuration
- Defined in:
- lib/yoker/configuration.rb
Constant Summary collapse
- SUPPORTED_DATABASES =
%w[postgresql mysql sqlite3].freeze
- SUPPORTED_VERSION_MANAGERS =
%w[mise rbenv rvm none].freeze
- SUPPORTED_CONTAINERS =
%w[docker-compose docker none].freeze
- SUPPORTED_ADDITIONAL_SERVICES =
%w[redis sidekiq mailcatcher].freeze
- DEFAULT_RUBY_VERSION =
RUBY_VERSION- DEFAULT_DATABASE =
"postgresql"- DEFAULT_VERSION_MANAGER =
"mise"- DEFAULT_CONTAINER =
"docker-compose"
Instance Attribute Summary collapse
-
#additional_services ⇒ Object
Returns the value of attribute additional_services.
-
#app_name ⇒ Object
Returns the value of attribute app_name.
-
#backup_existing ⇒ Object
Returns the value of attribute backup_existing.
-
#container ⇒ Object
Returns the value of attribute container.
-
#database ⇒ Object
Returns the value of attribute database.
-
#database_host ⇒ Object
Returns the value of attribute database_host.
-
#database_password ⇒ Object
Returns the value of attribute database_password.
-
#database_port ⇒ Object
Returns the value of attribute database_port.
-
#database_username ⇒ Object
Returns the value of attribute database_username.
-
#force_overwrite ⇒ Object
Returns the value of attribute force_overwrite.
-
#ruby_version ⇒ Object
Returns the value of attribute ruby_version.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
-
#version_manager ⇒ Object
Returns the value of attribute version_manager.
Class Method Summary collapse
Instance Method Summary collapse
- #database_service_needed? ⇒ Boolean
- #includes_service?(service) ⇒ Boolean
-
#initialize(options = {}) ⇒ Configuration
constructor
A new instance of Configuration.
-
#merge!(other_config) ⇒ Object
Merge configurations (for updating existing configs).
- #mysql? ⇒ Boolean
- #postgresql? ⇒ Boolean
- #redis_needed? ⇒ Boolean
- #save_to_file(path) ⇒ Object
- #sqlite? ⇒ Boolean
- #to_h ⇒ Object
- #uses_containers? ⇒ Boolean
- #uses_docker_compose? ⇒ Boolean
- #uses_mise? ⇒ Boolean
- #uses_version_manager? ⇒ Boolean
-
#valid? ⇒ Boolean
Validation methods.
Constructor Details
#initialize(options = {}) ⇒ Configuration
Returns a new instance of Configuration.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/yoker/configuration.rb', line 23 def initialize( = {}) @app_name = [:app_name] || detect_app_name @ruby_version = [:ruby_version] || DEFAULT_RUBY_VERSION @database = [:database] || DEFAULT_DATABASE @version_manager = [:version_manager] || DEFAULT_VERSION_MANAGER @container = [:container] || DEFAULT_CONTAINER @additional_services = Array([:additional_services]) @force_overwrite = [:force_overwrite] || false @backup_existing = [:backup_existing] || true @verbose = [:verbose] || false # Database-specific settings @database_port = [:database_port] || default_database_port @database_username = [:database_username] || default_database_username @database_password = [:database_password] || default_database_password @database_host = [:database_host] || default_database_host validate! end |
Instance Attribute Details
#additional_services ⇒ Object
Returns the value of attribute additional_services.
18 19 20 |
# File 'lib/yoker/configuration.rb', line 18 def additional_services @additional_services end |
#app_name ⇒ Object
Returns the value of attribute app_name.
18 19 20 |
# File 'lib/yoker/configuration.rb', line 18 def app_name @app_name end |
#backup_existing ⇒ Object
Returns the value of attribute backup_existing.
18 19 20 |
# File 'lib/yoker/configuration.rb', line 18 def backup_existing @backup_existing end |
#container ⇒ Object
Returns the value of attribute container.
18 19 20 |
# File 'lib/yoker/configuration.rb', line 18 def container @container end |
#database ⇒ Object
Returns the value of attribute database.
18 19 20 |
# File 'lib/yoker/configuration.rb', line 18 def database @database end |
#database_host ⇒ Object
Returns the value of attribute database_host.
18 19 20 |
# File 'lib/yoker/configuration.rb', line 18 def database_host @database_host end |
#database_password ⇒ Object
Returns the value of attribute database_password.
18 19 20 |
# File 'lib/yoker/configuration.rb', line 18 def database_password @database_password end |
#database_port ⇒ Object
Returns the value of attribute database_port.
18 19 20 |
# File 'lib/yoker/configuration.rb', line 18 def database_port @database_port end |
#database_username ⇒ Object
Returns the value of attribute database_username.
18 19 20 |
# File 'lib/yoker/configuration.rb', line 18 def database_username @database_username end |
#force_overwrite ⇒ Object
Returns the value of attribute force_overwrite.
18 19 20 |
# File 'lib/yoker/configuration.rb', line 18 def force_overwrite @force_overwrite end |
#ruby_version ⇒ Object
Returns the value of attribute ruby_version.
18 19 20 |
# File 'lib/yoker/configuration.rb', line 18 def ruby_version @ruby_version end |
#verbose ⇒ Object
Returns the value of attribute verbose.
18 19 20 |
# File 'lib/yoker/configuration.rb', line 18 def verbose @verbose end |
#version_manager ⇒ Object
Returns the value of attribute version_manager.
18 19 20 |
# File 'lib/yoker/configuration.rb', line 18 def version_manager @version_manager end |
Class Method Details
.from_file(path) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/yoker/configuration.rb', line 61 def self.from_file(path) return new unless File.exist?(path) case File.extname(path) when ".yml", ".yaml" from_yaml(path) when ".toml" from_toml(path) else raise InvalidConfigurationError, "Unsupported configuration file format: #{path}" end end |
.from_toml(path) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/yoker/configuration.rb', line 81 def self.from_toml(path) raise InvalidConfigurationError, "toml-rb gem required to load TOML configuration" unless defined?(TomlRB) config = TomlRB.load_file(path) symbolized_config = deep_symbolize_keys(config) new(symbolized_config) rescue StandardError => e raise InvalidConfigurationError, "Failed to load TOML configuration: #{e.}" end |
.from_yaml(path) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/yoker/configuration.rb', line 74 def self.from_yaml(path) config = YAML.safe_load(File.read(path), symbolize_names: true) new(config) rescue StandardError => e raise InvalidConfigurationError, "Failed to load YAML configuration: #{e.}" end |
Instance Method Details
#database_service_needed? ⇒ Boolean
144 145 146 |
# File 'lib/yoker/configuration.rb', line 144 def database_service_needed? uses_containers? && !sqlite? end |
#includes_service?(service) ⇒ Boolean
140 141 142 |
# File 'lib/yoker/configuration.rb', line 140 def includes_service?(service) additional_services.include?(service.to_s) end |
#merge!(other_config) ⇒ Object
Merge configurations (for updating existing configs)
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/yoker/configuration.rb', line 153 def merge!(other_config) return self unless other_config.is_a?(Configuration) other_config.to_h.each do |key, value| public_send("#{key}=", value) if respond_to?("#{key}=") && !value.nil? end validate! self end |
#mysql? ⇒ Boolean
116 117 118 |
# File 'lib/yoker/configuration.rb', line 116 def mysql? database == "mysql" end |
#postgresql? ⇒ Boolean
112 113 114 |
# File 'lib/yoker/configuration.rb', line 112 def postgresql? database == "postgresql" end |
#redis_needed? ⇒ Boolean
148 149 150 |
# File 'lib/yoker/configuration.rb', line 148 def redis_needed? includes_service?("redis") || includes_service?("sidekiq") end |
#save_to_file(path) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/yoker/configuration.rb', line 91 def save_to_file(path) case File.extname(path) when ".yml", ".yaml" File.write(path, to_h.to_yaml) when ".toml" raise InvalidConfigurationError, "toml-rb gem required to save TOML configuration" unless defined?(TomlRB) File.write(path, TomlRB.dump(to_h.stringify_keys)) else raise InvalidConfigurationError, "Unsupported configuration file format: #{path}" end end |
#sqlite? ⇒ Boolean
120 121 122 |
# File 'lib/yoker/configuration.rb', line 120 def sqlite? database == "sqlite3" end |
#to_h ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/yoker/configuration.rb', line 43 def to_h { app_name: app_name, ruby_version: ruby_version, database: database, version_manager: version_manager, container: container, additional_services: additional_services, force_overwrite: force_overwrite, backup_existing: backup_existing, verbose: verbose, database_port: database_port, database_username: database_username, database_password: database_password, database_host: database_host } end |
#uses_containers? ⇒ Boolean
124 125 126 |
# File 'lib/yoker/configuration.rb', line 124 def uses_containers? container != "none" end |
#uses_docker_compose? ⇒ Boolean
128 129 130 |
# File 'lib/yoker/configuration.rb', line 128 def uses_docker_compose? container == "docker-compose" end |
#uses_mise? ⇒ Boolean
136 137 138 |
# File 'lib/yoker/configuration.rb', line 136 def uses_mise? version_manager == "mise" end |
#uses_version_manager? ⇒ Boolean
132 133 134 |
# File 'lib/yoker/configuration.rb', line 132 def uses_version_manager? version_manager != "none" end |
#valid? ⇒ Boolean
Validation methods
105 106 107 108 109 110 |
# File 'lib/yoker/configuration.rb', line 105 def valid? validate! true rescue InvalidConfigurationError false end |