Class: Appydave::Tools::Jump::Config

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

Examples:

Basic usage

config = Config.new
config.locations          # => Array of Location objects
config.brands             # => Hash of brand definitions
config.find('ad-tools')   # => Location or nil

Constant Summary collapse

CONFIG_VERSION =
'1.0'
DEFAULT_CONFIG_NAME =
'locations'

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dataObject (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

Parameters:

  • location (Location, Hash)

    Location to add

Returns:

  • (Boolean)

    true if added successfully

Raises:

  • (ArgumentError)

    if key already exists or location is invalid



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

#brandsHash

Get brand definitions

Returns:

  • (Hash)


45
46
47
# File 'lib/appydave/tools/jump/config.rb', line 45

def brands
  data['brands'] || {}
end

#categoriesHash

Get category definitions

Returns:

  • (Hash)


59
60
61
# File 'lib/appydave/tools/jump/config.rb', line 59

def categories
  data['categories'] || {}
end

#clientsHash

Get client definitions

Returns:

  • (Hash)


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

Parameters:

  • key (String)

    Location key

Returns:



74
75
76
# File 'lib/appydave/tools/jump/config.rb', line 74

def find(key)
  locations.find { |loc| loc.key == key }
end

#infoHash

Get info about the configuration

Returns:

  • (Hash)


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: meta['version'],
    last_updated: meta['last_updated'],
    last_validated: meta['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

Parameters:

  • key (String)

    Location key

Returns:

  • (Boolean)


82
83
84
# File 'lib/appydave/tools/jump/config.rb', line 82

def key_exists?(key)
  locations.any? { |loc| loc.key == key }
end

#locationsArray<Location>

Get all locations as Location objects

Returns:



32
33
34
# File 'lib/appydave/tools/jump/config.rb', line 32

def locations
  @locations ||= (data['locations'] || []).map { |loc| Location.new(loc) }
end

#metaHash

Get metadata

Returns:

  • (Hash)


66
67
68
# File 'lib/appydave/tools/jump/config.rb', line 66

def meta
  data['meta'] || {}
end

#reload_locationsObject

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

Parameters:

  • key (String)

    Key of location to remove

Returns:

  • (Boolean)

    true if removed

Raises:

  • (ArgumentError)

    if location not found



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

#savevoid

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_validatedvoid

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

Parameters:

  • key (String)

    Key of location to update

  • attrs (Hash)

    Attributes to update

Returns:

  • (Boolean)

    true if updated successfully

Raises:

  • (ArgumentError)

    if location not found or updates are invalid



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