Class: T::RCFile

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

Constant Summary collapse

FILE_NAME =
".trc".freeze
XRC_FILE_NAME =
".xrc".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRCFile

Returns a new instance of RCFile.



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

def initialize
  @path = self.class.default_path
  @data = load_file
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/t/rcfile.rb', line 7

def path
  @path
end

Class Method Details

.default_pathObject



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

def self.default_path
  xrc = File.join(File.expand_path("~"), XRC_FILE_NAME)
  trc = File.join(File.expand_path("~"), FILE_NAME)
  File.exist?(xrc) ? xrc : trc
end

Instance Method Details

#[](username) ⇒ Object



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

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

#[]=(username, profile) ⇒ Object



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

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

#active_consumer_keyObject



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

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

#active_consumer_secretObject



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

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

#active_profileObject



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

def active_profile
  configuration["default_profile"]
end

#active_profile=(profile) ⇒ Object



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

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

#active_secretObject



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

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

#active_tokenObject



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

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

#configurationObject



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

def configuration
  @data["configuration"]
end

#deleteObject



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

def delete
  FileUtils.rm_f(@path)
end

#delete_key(profile, key) ⇒ Object



110
111
112
113
# File 'lib/t/rcfile.rb', line 110

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

#delete_profile(profile) ⇒ Object



105
106
107
108
# File 'lib/t/rcfile.rb', line 105

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @data == default_structure
end

#find(username) ⇒ Object

Raises:

  • (ArgumentError)


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

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



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

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

#find_case_insensitive_possibilities(username) ⇒ Object



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

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

#load_fileObject



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

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

#profilesObject



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

def profiles
  @data["profiles"]
end

#resetObject



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

def reset
  send(:initialize)
end