Class: CreateRubyGem::Config::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/create_ruby_gem/config/store.rb

Overview

YAML persistence for presets and last-used options.

Stores configuration at ~/.config/create-ruby-gem/config.yml (or $XDG_CONFIG_HOME/create-ruby-gem/config.yml). Writes are atomic via Tempfile + rename.

Constant Summary collapse

SCHEMA_VERSION =

Returns current config file schema version.

Returns:

  • (Integer)

    current config file schema version

1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil) ⇒ Store

Returns a new instance of Store.

Parameters:

  • path (String, nil) (defaults to: nil)

    override the default config file path



19
20
21
# File 'lib/create_ruby_gem/config/store.rb', line 19

def initialize(path: nil)
  @path = path || default_path
end

Instance Attribute Details

#pathString (readonly)

Returns absolute path to the config file.

Returns:

  • (String)

    absolute path to the config file



24
25
26
# File 'lib/create_ruby_gem/config/store.rb', line 24

def path
  @path
end

Instance Method Details

#delete_preset(name) ⇒ void

This method returns an undefined value.

Deletes a named preset (no-op if it does not exist).

Parameters:

  • name (String)


73
74
75
76
77
# File 'lib/create_ruby_gem/config/store.rb', line 73

def delete_preset(name)
  payload = data
  payload.fetch('presets').delete(name.to_s)
  write(payload)
end

#last_usedHash{String => Object}

Returns the last-used option hash (empty hash if none saved).

Returns:

  • (Hash{String => Object})


29
30
31
# File 'lib/create_ruby_gem/config/store.rb', line 29

def last_used
  data.fetch('last_used')
end

#preset(name) ⇒ Hash{String => Object}?

Returns a preset by name, or nil if it does not exist.

Parameters:

  • name (String)

Returns:

  • (Hash{String => Object}, nil)


47
48
49
# File 'lib/create_ruby_gem/config/store.rb', line 47

def preset(name)
  data.fetch('presets').fetch(name.to_s, nil)
end

#preset_namesArray<String>

Returns all preset names sorted alphabetically.

Returns:

  • (Array<String>)


54
55
56
# File 'lib/create_ruby_gem/config/store.rb', line 54

def preset_names
  data.fetch('presets').keys.sort
end

#save_last_used(options) ⇒ void

This method returns an undefined value.

Persists the given options as last-used.

Parameters:

  • options (Hash{Symbol => Object})


37
38
39
40
41
# File 'lib/create_ruby_gem/config/store.rb', line 37

def save_last_used(options)
  payload = data
  payload['last_used'] = stringify_keys(options)
  write(payload)
end

#save_preset(name, options) ⇒ void

This method returns an undefined value.

Saves a named preset.

Parameters:

  • name (String)
  • options (Hash{Symbol => Object})


63
64
65
66
67
# File 'lib/create_ruby_gem/config/store.rb', line 63

def save_preset(name, options)
  payload = data
  payload.fetch('presets')[name.to_s] = stringify_keys(options)
  write(payload)
end