Class: T::RCFile
Constant Summary collapse
- FILE_NAME =
".trc".freeze
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
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
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
6 7 8 |
# File 'lib/t/rcfile.rb', line 6 def path @path end |
Instance Method Details
#[](username) ⇒ Object
15 16 17 |
# File 'lib/t/rcfile.rb', line 15 def [](username) profiles[find(username)] end |
#[]=(username, profile) ⇒ Object
34 35 36 37 38 |
# File 'lib/t/rcfile.rb', line 34 def []=(username, profile) profiles[username] ||= {} profiles[username].merge!(profile) write end |
#active_consumer_key ⇒ Object
44 45 46 |
# File 'lib/t/rcfile.rb', line 44 def active_consumer_key profiles[active_profile[0]][active_profile[1]]["consumer_key"] if active_profile? end |
#active_consumer_secret ⇒ Object
48 49 50 |
# File 'lib/t/rcfile.rb', line 48 def active_consumer_secret profiles[active_profile[0]][active_profile[1]]["consumer_secret"] if active_profile? end |
#active_profile ⇒ Object
52 53 54 |
# File 'lib/t/rcfile.rb', line 52 def active_profile configuration["default_profile"] end |
#active_profile=(profile) ⇒ Object
56 57 58 59 |
# File 'lib/t/rcfile.rb', line 56 def active_profile=(profile) configuration["default_profile"] = [profile["username"], profile["consumer_key"]] write end |
#active_secret ⇒ Object
61 62 63 |
# File 'lib/t/rcfile.rb', line 61 def active_secret profiles[active_profile[0]][active_profile[1]]["secret"] if active_profile? end |
#active_token ⇒ Object
65 66 67 |
# File 'lib/t/rcfile.rb', line 65 def active_token profiles[active_profile[0]][active_profile[1]]["token"] if active_profile? end |
#configuration ⇒ Object
40 41 42 |
# File 'lib/t/rcfile.rb', line 40 def configuration @data["configuration"] end |
#delete ⇒ Object
69 70 71 |
# File 'lib/t/rcfile.rb', line 69 def delete FileUtils.rm_f(@path) end |
#delete_key(profile, key) ⇒ Object
102 103 104 105 |
# File 'lib/t/rcfile.rb', line 102 def delete_key(profile, key) profiles[profile].delete(key) write end |
#delete_profile(profile) ⇒ Object
97 98 99 100 |
# File 'lib/t/rcfile.rb', line 97 def delete_profile(profile) profiles.delete(profile) write end |
#empty? ⇒ Boolean
73 74 75 |
# File 'lib/t/rcfile.rb', line 73 def empty? @data == default_structure end |
#find(username) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/t/rcfile.rb', line 19 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
26 27 28 |
# File 'lib/t/rcfile.rb', line 26 def find_case_insensitive_match(username) profiles.keys.detect { |u| username.casecmp(u).zero? } end |
#find_case_insensitive_possibilities(username) ⇒ Object
30 31 32 |
# File 'lib/t/rcfile.rb', line 30 def find_case_insensitive_possibilities(username) profiles.keys.select { |u| username.casecmp(u[0, username.length]).zero? } end |
#load_file ⇒ Object
77 78 79 80 81 82 |
# File 'lib/t/rcfile.rb', line 77 def load_file require "yaml" YAML.load_file(@path) rescue Errno::ENOENT default_structure end |
#profiles ⇒ Object
89 90 91 |
# File 'lib/t/rcfile.rb', line 89 def profiles @data["profiles"] end |
#reset ⇒ Object
93 94 95 |
# File 'lib/t/rcfile.rb', line 93 def reset send(:initialize) end |