Class: Twurl::RCFile
- Inherits:
-
Object
- Object
- Twurl::RCFile
- Defined in:
- lib/twurl/rcfile.rb
Constant Summary collapse
- FILE =
'.twurlrc'
Class Attribute Summary collapse
-
.directory ⇒ Object
Returns the value of attribute directory.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
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
27 28 29 |
# File 'lib/twurl/rcfile.rb', line 27 def initialize @data = self.class.load end |
Class Attribute Details
.directory ⇒ Object
Returns the value of attribute directory.
5 6 7 |
# File 'lib/twurl/rcfile.rb', line 5 def directory @directory end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
26 27 28 |
# File 'lib/twurl/rcfile.rb', line 26 def data @data end |
Class Method Details
.default_rcfile_structure ⇒ Object
21 22 23 |
# File 'lib/twurl/rcfile.rb', line 21 def default_rcfile_structure {'profiles' => {}, 'configuration' => {}} end |
.file_path ⇒ Object
11 12 13 |
# File 'lib/twurl/rcfile.rb', line 11 def file_path File.join(directory, FILE) end |
.load ⇒ Object
15 16 17 18 19 |
# File 'lib/twurl/rcfile.rb', line 15 def load YAML.load_file(file_path) rescue Errno::ENOENT default_rcfile_structure end |
Instance Method Details
#<<(oauth_client) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/twurl/rcfile.rb', line 88 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
41 42 43 |
# File 'lib/twurl/rcfile.rb', line 41 def [](username) profiles[username] end |
#alias(name, path) ⇒ Object
61 62 63 64 65 |
# File 'lib/twurl/rcfile.rb', line 61 def alias(name, path) data['aliases'] ||= {} data['aliases'][name] = path save end |
#alias_from_name(name) ⇒ Object
79 80 81 |
# File 'lib/twurl/rcfile.rb', line 79 def alias_from_name(name) aliases[name] end |
#alias_from_options(options) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/twurl/rcfile.rb', line 71 def () .subcommands.each do |potential_alias| if path = alias_from_name(potential_alias) break path end end end |
#aliases ⇒ Object
67 68 69 |
# File 'lib/twurl/rcfile.rb', line 67 def aliases data['aliases'] end |
#configuration ⇒ Object
57 58 59 |
# File 'lib/twurl/rcfile.rb', line 57 def configuration data['configuration'] end |
#default_profile ⇒ Object
49 50 51 |
# File 'lib/twurl/rcfile.rb', line 49 def default_profile configuration['default_profile'] end |
#default_profile=(profile) ⇒ Object
53 54 55 |
# File 'lib/twurl/rcfile.rb', line 53 def default_profile=(profile) configuration['default_profile'] = [profile.username, profile.consumer_key] end |
#empty? ⇒ Boolean
31 32 33 |
# File 'lib/twurl/rcfile.rb', line 31 def empty? data == self.class.default_rcfile_structure end |
#has_oauth_profile_for_username_with_consumer_key?(username, consumer_key) ⇒ Boolean
83 84 85 86 |
# File 'lib/twurl/rcfile.rb', line 83 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
45 46 47 |
# File 'lib/twurl/rcfile.rb', line 45 def profiles data['profiles'] end |
#save ⇒ Object
35 36 37 38 39 |
# File 'lib/twurl/rcfile.rb', line 35 def save File.open(self.class.file_path, 'w') do |rcfile| rcfile.write data.to_yaml end end |