Class: Yoker::Configuration

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

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
  @app_name = options[:app_name] || detect_app_name
  @ruby_version = options[:ruby_version] || DEFAULT_RUBY_VERSION
  @database = options[:database] || DEFAULT_DATABASE
  @version_manager = options[:version_manager] || DEFAULT_VERSION_MANAGER
  @container = options[:container] || DEFAULT_CONTAINER
  @additional_services = Array(options[:additional_services])
  @force_overwrite = options[:force_overwrite] || false
  @backup_existing = options[:backup_existing] || true
  @verbose = options[:verbose] || false

  # Database-specific settings
  @database_port = options[:database_port] || default_database_port
  @database_username = options[:database_username] || default_database_username
  @database_password = options[:database_password] || default_database_password
  @database_host = options[:database_host] || default_database_host

  validate!
end

Instance Attribute Details

#additional_servicesObject

Returns the value of attribute additional_services.



18
19
20
# File 'lib/yoker/configuration.rb', line 18

def additional_services
  @additional_services
end

#app_nameObject

Returns the value of attribute app_name.



18
19
20
# File 'lib/yoker/configuration.rb', line 18

def app_name
  @app_name
end

#backup_existingObject

Returns the value of attribute backup_existing.



18
19
20
# File 'lib/yoker/configuration.rb', line 18

def backup_existing
  @backup_existing
end

#containerObject

Returns the value of attribute container.



18
19
20
# File 'lib/yoker/configuration.rb', line 18

def container
  @container
end

#databaseObject

Returns the value of attribute database.



18
19
20
# File 'lib/yoker/configuration.rb', line 18

def database
  @database
end

#database_hostObject

Returns the value of attribute database_host.



18
19
20
# File 'lib/yoker/configuration.rb', line 18

def database_host
  @database_host
end

#database_passwordObject

Returns the value of attribute database_password.



18
19
20
# File 'lib/yoker/configuration.rb', line 18

def database_password
  @database_password
end

#database_portObject

Returns the value of attribute database_port.



18
19
20
# File 'lib/yoker/configuration.rb', line 18

def database_port
  @database_port
end

#database_usernameObject

Returns the value of attribute database_username.



18
19
20
# File 'lib/yoker/configuration.rb', line 18

def database_username
  @database_username
end

#force_overwriteObject

Returns the value of attribute force_overwrite.



18
19
20
# File 'lib/yoker/configuration.rb', line 18

def force_overwrite
  @force_overwrite
end

#ruby_versionObject

Returns the value of attribute ruby_version.



18
19
20
# File 'lib/yoker/configuration.rb', line 18

def ruby_version
  @ruby_version
end

#verboseObject

Returns the value of attribute verbose.



18
19
20
# File 'lib/yoker/configuration.rb', line 18

def verbose
  @verbose
end

#version_managerObject

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.message}"
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.message}"
end

Instance Method Details

#database_service_needed?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/yoker/configuration.rb', line 144

def database_service_needed?
  uses_containers? && !sqlite?
end

#includes_service?(service) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


116
117
118
# File 'lib/yoker/configuration.rb', line 116

def mysql?
  database == "mysql"
end

#postgresql?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/yoker/configuration.rb', line 112

def postgresql?
  database == "postgresql"
end

#redis_needed?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


120
121
122
# File 'lib/yoker/configuration.rb', line 120

def sqlite?
  database == "sqlite3"
end

#to_hObject



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

Returns:

  • (Boolean)


124
125
126
# File 'lib/yoker/configuration.rb', line 124

def uses_containers?
  container != "none"
end

#uses_docker_compose?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/yoker/configuration.rb', line 128

def uses_docker_compose?
  container == "docker-compose"
end

#uses_mise?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/yoker/configuration.rb', line 136

def uses_mise?
  version_manager == "mise"
end

#uses_version_manager?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/yoker/configuration.rb', line 132

def uses_version_manager?
  version_manager != "none"
end

#valid?Boolean

Validation methods

Returns:

  • (Boolean)


105
106
107
108
109
110
# File 'lib/yoker/configuration.rb', line 105

def valid?
  validate!
  true
rescue InvalidConfigurationError
  false
end