Class: T::RCFile

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/t/rcfile.rb

Constant Summary collapse

FILE_NAME =
'.trc'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRCFile

Returns a new instance of RCFile.



9
10
11
12
# File 'lib/t/rcfile.rb', line 9

def initialize
  @path = File.join(File.expand_path('~'), FILE_NAME)
  @data = load_file
end

Instance Attribute Details

#pathObject

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



14
15
16
# File 'lib/t/rcfile.rb', line 14

def [](username)
  profiles[find(username)]
end

#[]=(username, profile) ⇒ Object



32
33
34
35
36
# File 'lib/t/rcfile.rb', line 32

def []=(username, profile)
  profiles[username] ||= {}
  profiles[username].merge!(profile)
  write
end

#active_consumer_keyObject



42
43
44
# File 'lib/t/rcfile.rb', line 42

def active_consumer_key
  profiles[active_profile[0]][active_profile[1]]['consumer_key'] if active_profile?
end

#active_consumer_secretObject



46
47
48
# File 'lib/t/rcfile.rb', line 46

def active_consumer_secret
  profiles[active_profile[0]][active_profile[1]]['consumer_secret'] if active_profile?
end

#active_profileObject



50
51
52
# File 'lib/t/rcfile.rb', line 50

def active_profile
  configuration['default_profile']
end

#active_profile=(profile) ⇒ Object



54
55
56
57
# File 'lib/t/rcfile.rb', line 54

def active_profile=(profile)
  configuration['default_profile'] = [profile['username'], profile['consumer_key']]
  write
end

#active_secretObject



59
60
61
# File 'lib/t/rcfile.rb', line 59

def active_secret
  profiles[active_profile[0]][active_profile[1]]['secret'] if active_profile?
end

#active_tokenObject



63
64
65
# File 'lib/t/rcfile.rb', line 63

def active_token
  profiles[active_profile[0]][active_profile[1]]['token'] if active_profile?
end

#configurationObject



38
39
40
# File 'lib/t/rcfile.rb', line 38

def configuration
  @data['configuration']
end

#deleteObject



67
68
69
# File 'lib/t/rcfile.rb', line 67

def delete
  File.delete(@path) if File.exist?(@path)
end

#delete_key(profile, key) ⇒ Object



101
102
103
104
# File 'lib/t/rcfile.rb', line 101

def delete_key(profile, key)
  profiles[profile].delete(key)
  write
end

#delete_profile(profile) ⇒ Object



96
97
98
99
# File 'lib/t/rcfile.rb', line 96

def delete_profile(profile)
  profiles.delete(profile)
  write
end

#empty?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/t/rcfile.rb', line 71

def empty?
  @data == default_structure
end

#find(username) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
# File 'lib/t/rcfile.rb', line 18

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



24
25
26
# File 'lib/t/rcfile.rb', line 24

def find_case_insensitive_match(username)
  profiles.keys.detect { |u| username.casecmp(u).zero? }
end

#find_case_insensitive_possibilities(username) ⇒ Object



28
29
30
# File 'lib/t/rcfile.rb', line 28

def find_case_insensitive_possibilities(username)
  profiles.keys.select { |u| username.casecmp(u[0, username.length]).zero? }
end

#load_fileObject



75
76
77
78
79
80
# File 'lib/t/rcfile.rb', line 75

def load_file
  require 'yaml'
  YAML.load_file(@path)
rescue Errno::ENOENT
  default_structure
end

#profilesObject



88
89
90
# File 'lib/t/rcfile.rb', line 88

def profiles
  @data['profiles']
end

#resetObject



92
93
94
# File 'lib/t/rcfile.rb', line 92

def reset
  send(:initialize)
end