Class: T::RCFile

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

Constant Summary collapse

FILE_NAME =
'.trc'

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



35
36
37
38
39
# File 'lib/t/rcfile.rb', line 35

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

#active_consumer_keyObject



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

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

#active_consumer_secretObject



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

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

#active_profileObject



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

def active_profile
  configuration['default_profile']
end

#active_profile=(profile) ⇒ Object



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

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

#active_secretObject



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

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

#active_tokenObject



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

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

#configurationObject



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

def configuration
  @data['configuration']
end

#deleteObject



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

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

#empty?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/t/rcfile.rb', line 74

def empty?
  @data == default_structure
end

#find(username) ⇒ Object



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

def find(username)
  possibilities = Array(find_case_insensitive_match(username) || find_case_insensitive_possibilities(username))
  if possibilities.size == 1
    possibilities.first
  else
    fail(ArgumentError.new("Username #{username} is #{possibilities.size < 1 ? 'not found.' : 'ambiguous, matching ' + possibilities.join(', ')}"))
  end
end

#find_case_insensitive_match(username) ⇒ Object



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

def find_case_insensitive_match(username)
  profiles.keys.detect { |u| username.downcase == u.downcase }
end

#find_case_insensitive_possibilities(username) ⇒ Object



31
32
33
# File 'lib/t/rcfile.rb', line 31

def find_case_insensitive_possibilities(username)
  profiles.keys.select { |u| username.downcase == u.downcase[0, username.length] }
end

#load_fileObject



78
79
80
81
82
83
# File 'lib/t/rcfile.rb', line 78

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

#profilesObject



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

def profiles
  @data['profiles']
end

#resetObject



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

def reset
  send(:initialize)
end