Class: Octopolo::UserConfig
- Inherits:
-
Object
- Object
- Octopolo::UserConfig
- Defined in:
- lib/octopolo/user_config.rb
Constant Summary collapse
- MissingGitHubAuth =
Class.new(StandardError)
- MissingPivotalAuth =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#attributes ⇒ Object
keep the whole hash.
-
#editor ⇒ Object
Public: Always use the $EDITOR when available.
-
#full_name ⇒ Object
Public: The user’s name.
-
#github_token ⇒ Object
Public: The GitHub token.
-
#github_user ⇒ Object
Public: The GitHub username.
-
#pivotal_token ⇒ Object
Public: The Pivotal Tracker token.
Class Method Summary collapse
-
.attributes_from_file ⇒ Object
Public: The user’s configuration values.
-
.config_parent ⇒ Object
Public: The parent directory of the user’s configuration file.
-
.config_path ⇒ Object
Public: The path to the users’s configuration file.
-
.parse ⇒ Object
Public: Parse the user’s config file.
-
.touch_config_file ⇒ Object
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.
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ UserConfig
constructor
Public: Initialize a new UserConfig instance.
-
#set(key, value) ⇒ Object
Public: Set and store a new configuration value.
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
#attributes ⇒ Object
keep the whole hash
9 10 11 |
# File 'lib/octopolo/user_config.rb', line 9 def attributes @attributes end |
#editor ⇒ Object
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_name ⇒ Object
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_token ⇒ Object
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_user ⇒ Object
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_token ⇒ Object
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_file ⇒ Object
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_parent ⇒ Object
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.("~/.octopolo") dir = File.("~/.automation") unless Dir.exists?(dir) dir end |
.config_path ⇒ Object
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 |
.parse ⇒ Object
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_file ⇒ Object
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 |