Module: Branch::Name::Configurable

Includes:
Colorizable, Locatable
Included in:
Loadable, Subcommands::Config, Subcommands::Delete, Subcommands::Init
Defined in:
lib/branch/name/configurable.rb

Constant Summary collapse

CONFIG_FILENAME =
'.branch-name'
DEFAULT_BRANCH_NAME_OPTIONS =

rubocop:disable Style/StringHashKeys - YAML writing/loading necessitates this

{
  'create' => {
    'downcase' => false,
    'separator' => '_',
    'format_string' => '%t %d',
    'project' => false,
    'project_location' => "#{Locatable.project_folder}/branch-name/projects/%Y/%m (%B)",
    'project_files' => %w[readme.txt scratch.rb snippets.rb],
    'interactive' => true
  }
}.freeze

Constants included from Colorizable

Branch::Name::Colorizable::ABORTED, Branch::Name::Colorizable::ERROR, Branch::Name::Colorizable::SUCCESS, Branch::Name::Colorizable::WARNING

Instance Method Summary collapse

Methods included from Locatable

global_folder?, home_folder, local_folder, project_folder, temp_folder

Instance Method Details

#create_config_file(config_file, create_options:) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/branch/name/configurable.rb', line 89

def create_config_file(config_file, create_options:)
  folder = File.dirname(config_file)
  unless Dir.exist?(folder)
    say "Destination folder for configuration file (#{folder}) does not exist", ERROR
    return false
  end

  if File.exist?(config_file)
    say "Configuration file (#{config_file}) already exists", WARNING
    return false
  end

  File.write(config_file, create_options.to_yaml)
  say "Configuration file (#{config_file}) created.", SUCCESS

  true
end

#create_global_config_file! {|create_options| ... } ⇒ Object

Yields:

  • (create_options)


47
48
49
50
51
52
# File 'lib/branch/name/configurable.rb', line 47

def create_global_config_file!
  create_options = DEFAULT_BRANCH_NAME_OPTIONS
  yield create_options if block_given?
  create_config_file global_config_file, create_options: create_options
  print_global_config_file
end

#create_local_config_file! {|create_options| ... } ⇒ Object

Yields:

  • (create_options)


54
55
56
57
58
59
# File 'lib/branch/name/configurable.rb', line 54

def create_local_config_file!
  create_options = DEFAULT_BRANCH_NAME_OPTIONS
  yield create_options if block_given?
  create_config_file local_config_file, create_options: create_options
  print_local_config_file
end

#delete_global_config_file!Object



61
62
63
# File 'lib/branch/name/configurable.rb', line 61

def delete_global_config_file!
  delete_config_file global_config_file
end

#delete_local_config_file!Object



65
66
67
# File 'lib/branch/name/configurable.rb', line 65

def delete_local_config_file!
  delete_config_file local_config_file
end

#global_config_fileObject

rubocop:enable Style/StringHashKeys



31
32
33
# File 'lib/branch/name/configurable.rb', line 31

def global_config_file
  File.join(Locatable.global_folder, CONFIG_FILENAME)
end

#global_config_file?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/branch/name/configurable.rb', line 39

def global_config_file?
  File.exist? global_config_file
end

#local_config_fileObject



35
36
37
# File 'lib/branch/name/configurable.rb', line 35

def local_config_file
  File.join(Locatable.local_folder, CONFIG_FILENAME)
end

#local_config_file?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/branch/name/configurable.rb', line 43

def local_config_file?
  File.exist? local_config_file
end


69
70
71
72
73
74
75
76
77
# File 'lib/branch/name/configurable.rb', line 69

def print_global_config_file
  config_file = global_config_file
  if global_config_file?
    say "Global config file (#{config_file}) contents:", SUCCESS
    print_config_file config_file
  else
    say "Global config file (#{config_file}) does not exist.", WARNING
  end
end


79
80
81
82
83
84
85
86
87
# File 'lib/branch/name/configurable.rb', line 79

def print_local_config_file
  config_file = local_config_file
  if local_config_file?
    say "Local config file (#{config_file}) contents:", SUCCESS
    print_config_file config_file
  else
    say "Local config file (#{config_file}) does not exist.", WARNING
  end
end