Class: Octopolo::UserConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/octopolo/user_config.rb

Constant Summary collapse

MissingGitHubAuth =
Class.new(StandardError)
MissingPivotalAuth =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ UserConfig

Public: Initialize a new UserConfig instance



12
13
14
15
16
17
18
19
# File 'lib/octopolo/user_config.rb', line 12

def initialize attributes={}
  self.attributes = attributes
  attributes.each do |key, value|
    # e.g., foo: "bar" translates to self.foo = "bar"
    setter = "#{key}="
    send(setter, value) if respond_to? setter
  end
end

Instance Attribute Details

#attributesObject

keep the whole hash



9
10
11
# File 'lib/octopolo/user_config.rb', line 9

def attributes
  @attributes
end

#editorObject

Public: Always use the $EDITOR when available

Returns a value or false



86
87
88
# File 'lib/octopolo/user_config.rb', line 86

def editor
  @editor
end

#full_nameObject

Public: The user’s name

Returns a String



79
80
81
# File 'lib/octopolo/user_config.rb', line 79

def full_name
  @full_name
end

#github_tokenObject

Public: The GitHub token

If none is stored, generate it for the user.

Returns a String or raises MissingGitHubAuth



104
105
106
# File 'lib/octopolo/user_config.rb', line 104

def github_token
  @github_token
end

#github_userObject

Public: The GitHub username

If none is stored, generate it for the user.

Returns a String or raises MissingGitHubAuth



4
5
6
# File 'lib/octopolo/user_config.rb', line 4

def github_user
  @github_user
end

#pivotal_tokenObject

Public: The Pivotal Tracker token

If none is stored, prompt them to generate it.

Returns a String or raises MissingPivotalAuth



113
114
115
# File 'lib/octopolo/user_config.rb', line 113

def pivotal_token
  @pivotal_token
end

Class Method Details

.attributes_from_fileObject

Public: The user’s configuration values

Returns a Hash



40
41
42
43
44
45
46
# File 'lib/octopolo/user_config.rb', line 40

def self.attributes_from_file
  YAML.load_file config_path
rescue Errno::ENOENT
  # create the file if it doesn't exist
  touch_config_file
  {}
end

.config_parentObject

Public: The parent directory of the user’s configuration file

Returns a String containing the path



58
59
60
61
62
# File 'lib/octopolo/user_config.rb', line 58

def self.config_parent
  dir = File.expand_path("~/.octopolo")
  dir = File.expand_path("~/.automation") unless Dir.exists?(dir)
  dir
end

.config_pathObject

Public: The path to the users’s configuration file

Returns a String containing the path



51
52
53
# File 'lib/octopolo/user_config.rb', line 51

def self.config_path
  File.join(config_parent, "config.yml")
end

.parseObject

Public: Parse the user’s config file

Returns a UserConfig instance



24
25
26
# File 'lib/octopolo/user_config.rb', line 24

def self.parse
  new attributes_from_file
end

.touch_config_fileObject

Public: Create the user’s configuration file NOTE this seems a mite gnarly, and its tests worse, but doesn’t seem worth splitting out into “create_config_parent_directory” and “create_config_file” at this stage



66
67
68
69
70
71
72
73
74
# File 'lib/octopolo/user_config.rb', line 66

def self.touch_config_file
  unless Dir.exist? config_parent
    Dir.mkdir config_parent
  end

  unless File.exist? config_path
    File.write UserConfig.config_path, YAML.dump({})
  end
end

Instance Method Details

#set(key, value) ⇒ Object

Public: Set and store a new configuration value



29
30
31
32
33
34
35
# File 'lib/octopolo/user_config.rb', line 29

def set key, value
  # capture new value in instance
  send("#{key}=", value)
  attributes.merge!(key => value)
  # and store it
  File.write UserConfig.config_path, YAML.dump(attributes)
end