class Git::Plan::ConfigFile

Constants

FILE_NAME

Attributes

path[R]

Public Class Methods

new() click to toggle source
# File lib/git/plan/configfile.rb, line 11
def initialize
  @path = File.join(File.expand_path('~'), FILE_NAME)
  @data = load_file
end

Public Instance Methods

add_command(hash) click to toggle source
# File lib/git/plan/configfile.rb, line 16
def add_command(hash)
  @data['commands'].merge!(hash)
  write
end
delete() click to toggle source
# File lib/git/plan/configfile.rb, line 29
def delete
  File.delete(@path) if File.exist?(@path)
end
empty?() click to toggle source
# File lib/git/plan/configfile.rb, line 33
def empty?
  @data == default_structure
end
get_all() click to toggle source
# File lib/git/plan/configfile.rb, line 25
def get_all
  @data['commands']
end
load_file() click to toggle source
# File lib/git/plan/configfile.rb, line 37
def load_file
  require 'yaml'
  YAML.load_file(@path)
rescue Errno::ENOENT
  default_structure
end
path=(path) click to toggle source
# File lib/git/plan/configfile.rb, line 44
def path=(path)
  @path = path
  @data = load_file
  @path
end
run(command) click to toggle source
# File lib/git/plan/configfile.rb, line 21
def run(command)
  @data['commands'][command]
end

Private Instance Methods

default_structure() click to toggle source
# File lib/git/plan/configfile.rb, line 60
def default_structure
  # TODO: Load a json file with specific default commands, so the user can run default set of git commands by default
  {'commands' => {}}
end
write() click to toggle source
# File lib/git/plan/configfile.rb, line 53
def write
  require 'yaml'
  File.open(@path, File::RDWR | File::TRUNC | File::CREAT, 0o0600) do |cfile|
    cfile.write @data.to_yaml
  end
end