Class: T::RCFile
Constant Summary collapse
- FILE_NAME =
".trc".freeze
- XRC_FILE_NAME =
".xrc".freeze
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #[](username) ⇒ Object
- #[]=(username, profile) ⇒ Object
- #active_consumer_key ⇒ Object
- #active_consumer_secret ⇒ Object
- #active_profile ⇒ Object
- #active_profile=(profile) ⇒ Object
- #active_secret ⇒ Object
- #active_token ⇒ Object
- #configuration ⇒ Object
- #delete ⇒ Object
- #delete_key(profile, key) ⇒ Object
- #delete_profile(profile) ⇒ Object
- #empty? ⇒ Boolean
- #find(username) ⇒ Object
- #find_case_insensitive_match(username) ⇒ Object
- #find_case_insensitive_possibilities(username) ⇒ Object
-
#initialize ⇒ RCFile
constructor
A new instance of RCFile.
- #load_file ⇒ Object
- #profiles ⇒ Object
- #reset ⇒ Object
Constructor Details
#initialize ⇒ RCFile
Returns a new instance of RCFile.
18 19 20 21 |
# File 'lib/t/rcfile.rb', line 18 def initialize @path = self.class.default_path @data = load_file end |
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
7 8 9 |
# File 'lib/t/rcfile.rb', line 7 def path @path end |
Class Method Details
.default_path ⇒ Object
12 13 14 15 16 |
# File 'lib/t/rcfile.rb', line 12 def self.default_path xrc = File.join(File.("~"), XRC_FILE_NAME) trc = File.join(File.("~"), FILE_NAME) File.exist?(xrc) ? xrc : trc end |
Instance Method Details
#[](username) ⇒ Object
23 24 25 |
# File 'lib/t/rcfile.rb', line 23 def [](username) profiles[find(username)] end |
#[]=(username, profile) ⇒ Object
42 43 44 45 46 |
# File 'lib/t/rcfile.rb', line 42 def []=(username, profile) profiles[username] ||= {} profiles[username].merge!(profile) write end |
#active_consumer_key ⇒ Object
52 53 54 |
# File 'lib/t/rcfile.rb', line 52 def active_consumer_key profiles[active_profile[0]][active_profile[1]]["consumer_key"] if active_profile? end |
#active_consumer_secret ⇒ Object
56 57 58 |
# File 'lib/t/rcfile.rb', line 56 def active_consumer_secret profiles[active_profile[0]][active_profile[1]]["consumer_secret"] if active_profile? end |
#active_profile ⇒ Object
60 61 62 |
# File 'lib/t/rcfile.rb', line 60 def active_profile configuration["default_profile"] end |
#active_profile=(profile) ⇒ Object
64 65 66 67 |
# File 'lib/t/rcfile.rb', line 64 def active_profile=(profile) configuration["default_profile"] = [profile["username"], profile["consumer_key"]] write end |
#active_secret ⇒ Object
69 70 71 |
# File 'lib/t/rcfile.rb', line 69 def active_secret profiles[active_profile[0]][active_profile[1]]["secret"] if active_profile? end |
#active_token ⇒ Object
73 74 75 |
# File 'lib/t/rcfile.rb', line 73 def active_token profiles[active_profile[0]][active_profile[1]]["token"] if active_profile? end |
#configuration ⇒ Object
48 49 50 |
# File 'lib/t/rcfile.rb', line 48 def configuration @data["configuration"] end |
#delete ⇒ Object
77 78 79 |
# File 'lib/t/rcfile.rb', line 77 def delete FileUtils.rm_f(@path) end |
#delete_key(profile, key) ⇒ Object
110 111 112 113 |
# File 'lib/t/rcfile.rb', line 110 def delete_key(profile, key) profiles[profile].delete(key) write end |
#delete_profile(profile) ⇒ Object
105 106 107 108 |
# File 'lib/t/rcfile.rb', line 105 def delete_profile(profile) profiles.delete(profile) write end |
#empty? ⇒ Boolean
81 82 83 |
# File 'lib/t/rcfile.rb', line 81 def empty? @data == default_structure end |
#find(username) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/t/rcfile.rb', line 27 def find(username) possibilities = Array(find_case_insensitive_match(username) || find_case_insensitive_possibilities(username)) raise(ArgumentError.new("Username #{username} is #{possibilities.empty? ? 'not found.' : "ambiguous, matching #{possibilities.join(', ')}"}")) unless possibilities.size == 1 possibilities.first end |
#find_case_insensitive_match(username) ⇒ Object
34 35 36 |
# File 'lib/t/rcfile.rb', line 34 def find_case_insensitive_match(username) profiles.keys.detect { |u| username.casecmp(u).zero? } end |
#find_case_insensitive_possibilities(username) ⇒ Object
38 39 40 |
# File 'lib/t/rcfile.rb', line 38 def find_case_insensitive_possibilities(username) profiles.keys.select { |u| username.casecmp(u[0, username.length]).zero? } end |
#load_file ⇒ Object
85 86 87 88 89 90 |
# File 'lib/t/rcfile.rb', line 85 def load_file require "yaml" YAML.load_file(@path) rescue Errno::ENOENT default_structure end |
#profiles ⇒ Object
97 98 99 |
# File 'lib/t/rcfile.rb', line 97 def profiles @data["profiles"] end |
#reset ⇒ Object
101 102 103 |
# File 'lib/t/rcfile.rb', line 101 def reset send(:initialize) end |