Class: Twurl::RCFile
- Inherits:
-
Object
- Object
- Twurl::RCFile
- Defined in:
- lib/twurl/rcfile.rb
Constant Summary collapse
- FILE =
'.twurlrc'
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
- .default_rcfile_structure ⇒ Object
- .directory ⇒ Object
- .directory=(dir) ⇒ Object
- .file_path ⇒ Object
- .load ⇒ Object
Instance Method Summary collapse
- #<<(oauth_client) ⇒ Object
- #[](username) ⇒ Object
- #alias(name, path) ⇒ Object
- #alias_from_name(name) ⇒ Object
- #alias_from_options(options) ⇒ Object
- #aliases ⇒ Object
- #configuration ⇒ Object
- #default_profile ⇒ Object
- #default_profile=(profile) ⇒ Object
- #empty? ⇒ Boolean
- #has_oauth_profile_for_username_with_consumer_key?(username, consumer_key) ⇒ Boolean
-
#initialize ⇒ RCFile
constructor
A new instance of RCFile.
- #profiles ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize ⇒ RCFile
Returns a new instance of RCFile.
29 30 31 |
# File 'lib/twurl/rcfile.rb', line 29 def initialize @data = self.class.load end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
28 29 30 |
# File 'lib/twurl/rcfile.rb', line 28 def data @data end |
Class Method Details
.default_rcfile_structure ⇒ Object
23 24 25 |
# File 'lib/twurl/rcfile.rb', line 23 def default_rcfile_structure {'profiles' => {}, 'configuration' => {}} end |
.directory ⇒ Object
5 6 7 |
# File 'lib/twurl/rcfile.rb', line 5 def directory @@directory ||= File.('~') end |
.directory=(dir) ⇒ Object
9 10 11 |
# File 'lib/twurl/rcfile.rb', line 9 def directory=(dir) @@directory = dir end |
.file_path ⇒ Object
13 14 15 |
# File 'lib/twurl/rcfile.rb', line 13 def file_path File.join(directory, FILE) end |
.load ⇒ Object
17 18 19 20 21 |
# File 'lib/twurl/rcfile.rb', line 17 def load YAML.load_file(file_path) rescue Errno::ENOENT default_rcfile_structure end |
Instance Method Details
#<<(oauth_client) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/twurl/rcfile.rb', line 90 def <<(oauth_client) client_from_file = self[oauth_client.username] || {} client_from_file[oauth_client.consumer_key] = oauth_client.to_hash (profiles[oauth_client.username] ||= {}).update(client_from_file) self.default_profile = oauth_client unless default_profile save end |
#[](username) ⇒ Object
43 44 45 |
# File 'lib/twurl/rcfile.rb', line 43 def [](username) profiles[username] end |
#alias(name, path) ⇒ Object
63 64 65 66 67 |
# File 'lib/twurl/rcfile.rb', line 63 def alias(name, path) data['aliases'] ||= {} data['aliases'][name] = path save end |
#alias_from_name(name) ⇒ Object
81 82 83 |
# File 'lib/twurl/rcfile.rb', line 81 def alias_from_name(name) aliases[name] end |
#alias_from_options(options) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/twurl/rcfile.rb', line 73 def () .subcommands.each do |potential_alias| if path = alias_from_name(potential_alias) break path end end end |
#aliases ⇒ Object
69 70 71 |
# File 'lib/twurl/rcfile.rb', line 69 def aliases data['aliases'] end |
#configuration ⇒ Object
59 60 61 |
# File 'lib/twurl/rcfile.rb', line 59 def configuration data['configuration'] end |
#default_profile ⇒ Object
51 52 53 |
# File 'lib/twurl/rcfile.rb', line 51 def default_profile configuration['default_profile'] end |
#default_profile=(profile) ⇒ Object
55 56 57 |
# File 'lib/twurl/rcfile.rb', line 55 def default_profile=(profile) configuration['default_profile'] = [profile.username, profile.consumer_key] end |
#empty? ⇒ Boolean
33 34 35 |
# File 'lib/twurl/rcfile.rb', line 33 def empty? data == self.class.default_rcfile_structure end |
#has_oauth_profile_for_username_with_consumer_key?(username, consumer_key) ⇒ Boolean
85 86 87 88 |
# File 'lib/twurl/rcfile.rb', line 85 def has_oauth_profile_for_username_with_consumer_key?(username, consumer_key) user_profiles = self[username] !user_profiles.nil? && !user_profiles[consumer_key].nil? end |
#profiles ⇒ Object
47 48 49 |
# File 'lib/twurl/rcfile.rb', line 47 def profiles data['profiles'] end |
#save ⇒ Object
37 38 39 40 41 |
# File 'lib/twurl/rcfile.rb', line 37 def save File.open(self.class.file_path, File::RDWR|File::CREAT, 0600) do |rcfile| rcfile.write data.to_yaml end end |