Class: Appydave::Tools::Jump::Config
- Inherits:
-
Object
- Object
- Appydave::Tools::Jump::Config
- Includes:
- KLog::Logging
- Defined in:
- lib/appydave/tools/jump/config.rb
Overview
Config manages the locations.json configuration file
Follows the same pattern as other configuration models in appydave-tools, using ConfigBase for file loading/saving with automatic backups.
Constant Summary collapse
- CONFIG_VERSION =
'1.0'- DEFAULT_CONFIG_NAME =
'locations'
Instance Attribute Summary collapse
-
#config_path ⇒ Object
readonly
Returns the value of attribute config_path.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
-
#add(location) ⇒ Boolean
Add a new location.
-
#brands ⇒ Hash
Get brand definitions.
-
#categories ⇒ Hash
Get category definitions.
-
#clients ⇒ Hash
Get client definitions.
-
#find(key) ⇒ Location?
Find a location by key.
-
#info ⇒ Hash
Get info about the configuration.
-
#initialize(config_path: nil) ⇒ Config
constructor
A new instance of Config.
-
#key_exists?(key) ⇒ Boolean
Check if a location key exists.
-
#locations ⇒ Array<Location>
Get all locations as Location objects.
-
#meta ⇒ Hash
Get metadata.
-
#reload_locations ⇒ Object
Reload locations from data (after modifications).
-
#remove(key) ⇒ Boolean
Remove a location by key.
-
#save ⇒ void
Save configuration to file with backup.
-
#touch_validated ⇒ void
Update validation timestamp.
-
#update(key, attrs) ⇒ Boolean
Update an existing location.
Constructor Details
#initialize(config_path: nil) ⇒ Config
Returns a new instance of Config.
24 25 26 27 |
# File 'lib/appydave/tools/jump/config.rb', line 24 def initialize(config_path: nil) @config_path = config_path || default_config_path @data = load_config end |
Instance Attribute Details
#config_path ⇒ Object (readonly)
Returns the value of attribute config_path.
22 23 24 |
# File 'lib/appydave/tools/jump/config.rb', line 22 def config_path @config_path end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
22 23 24 |
# File 'lib/appydave/tools/jump/config.rb', line 22 def data @data end |
Instance Method Details
#add(location) ⇒ Boolean
Add a new location
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/appydave/tools/jump/config.rb', line 91 def add(location) location = Location.new(location) if location.is_a?(Hash) raise ArgumentError, "Location key '#{location.key}' already exists" if key_exists?(location.key) errors = location.validate raise ArgumentError, "Invalid location: #{errors.join(', ')}" unless errors.empty? data['locations'] ||= [] data['locations'] << location.to_h reload_locations true end |
#brands ⇒ Hash
Get brand definitions
45 46 47 |
# File 'lib/appydave/tools/jump/config.rb', line 45 def brands data['brands'] || {} end |
#categories ⇒ Hash
Get category definitions
59 60 61 |
# File 'lib/appydave/tools/jump/config.rb', line 59 def categories data['categories'] || {} end |
#clients ⇒ Hash
Get client definitions
52 53 54 |
# File 'lib/appydave/tools/jump/config.rb', line 52 def clients data['clients'] || {} end |
#find(key) ⇒ Location?
Find a location by key
74 75 76 |
# File 'lib/appydave/tools/jump/config.rb', line 74 def find(key) locations.find { |loc| loc.key == key } end |
#info ⇒ Hash
Get info about the configuration
178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/appydave/tools/jump/config.rb', line 178 def info { config_path: config_path, exists: File.exist?(config_path), version: ['version'], last_updated: ['last_updated'], last_validated: ['last_validated'], location_count: locations.size, brand_count: brands.size, client_count: clients.size } end |
#key_exists?(key) ⇒ Boolean
Check if a location key exists
82 83 84 |
# File 'lib/appydave/tools/jump/config.rb', line 82 def key_exists?(key) locations.any? { |loc| loc.key == key } end |
#locations ⇒ Array<Location>
Get all locations as Location objects
32 33 34 |
# File 'lib/appydave/tools/jump/config.rb', line 32 def locations @locations ||= (data['locations'] || []).map { |loc| Location.new(loc) } end |
#meta ⇒ Hash
Get metadata
66 67 68 |
# File 'lib/appydave/tools/jump/config.rb', line 66 def data['meta'] || {} end |
#reload_locations ⇒ Object
Reload locations from data (after modifications)
37 38 39 40 |
# File 'lib/appydave/tools/jump/config.rb', line 37 def reload_locations @locations = nil locations end |
#remove(key) ⇒ Boolean
Remove a location by key
134 135 136 137 138 139 140 141 |
# File 'lib/appydave/tools/jump/config.rb', line 134 def remove(key) index = (data['locations'] || []).find_index { |loc| loc['key'] == key || loc[:key] == key } raise ArgumentError, "Location '#{key}' not found" if index.nil? data['locations'].delete_at(index) reload_locations true end |
#save ⇒ void
This method returns an undefined value.
Save configuration to file with backup
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/appydave/tools/jump/config.rb', line 146 def save # Create backup if file exists if File.exist?(config_path) backup_path = "#{config_path}.backup.#{Time.now.strftime('%Y%m%d-%H%M%S')}" FileUtils.cp(config_path, backup_path) end # Update timestamp data['meta'] ||= {} data['meta']['version'] = CONFIG_VERSION data['meta']['last_updated'] = Time.now.utc.iso8601 # Ensure directory exists FileUtils.mkdir_p(File.dirname(config_path)) # Write atomically (temp file then rename) temp_path = "#{config_path}.tmp" File.write(temp_path, JSON.pretty_generate(data)) File.rename(temp_path, config_path) end |
#touch_validated ⇒ void
This method returns an undefined value.
Update validation timestamp
170 171 172 173 |
# File 'lib/appydave/tools/jump/config.rb', line 170 def touch_validated data['meta'] ||= {} data['meta']['last_validated'] = Time.now.utc.iso8601 end |
#update(key, attrs) ⇒ Boolean
Update an existing location
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/appydave/tools/jump/config.rb', line 111 def update(key, attrs) index = (data['locations'] || []).find_index { |loc| loc['key'] == key || loc[:key] == key } raise ArgumentError, "Location '#{key}' not found" if index.nil? # Merge attributes current = data['locations'][index].transform_keys(&:to_sym) updated_attrs = current.merge(attrs.transform_keys(&:to_sym)) # Validate updated = Location.new(updated_attrs) errors = updated.validate raise ArgumentError, "Invalid update: #{errors.join(', ')}" unless errors.empty? data['locations'][index] = updated.to_h.transform_keys(&:to_s) reload_locations true end |