Class: Twat::Config
Instance Method Summary
collapse
Methods included from Exceptions
#with_handled_exceptions
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth) ⇒ Object
49
50
51
|
# File 'lib/twat/config.rb', line 49
def method_missing(meth)
self[meth]
end
|
Instance Method Details
#[]=(k, v) ⇒ Object
73
74
75
|
# File 'lib/twat/config.rb', line 73
def []=(k, v)
config[k] = v
end
|
#account ⇒ Object
95
96
97
98
|
# File 'lib/twat/config.rb', line 95
def account
raise NoSuchAccount unless accounts.include?(account_name)
accounts[account_name]
end
|
#account_name ⇒ Object
86
87
88
89
90
91
92
93
|
# File 'lib/twat/config.rb', line 86
def account_name
if $args[:account]
return $args[:account].to_sym
else
raise ::Twat::Exceptions::NoDefaultAccount unless config.include?(:default)
return config[:default].to_sym
end
end
|
#accounts ⇒ Object
53
54
55
|
# File 'lib/twat/config.rb', line 53
def accounts
return config[:accounts]
end
|
#beep? ⇒ Boolean
61
62
63
|
# File 'lib/twat/config.rb', line 61
def beep?
config[:beep].nil? ? false : config[:beep]
end
|
#colors? ⇒ Boolean
57
58
59
|
# File 'lib/twat/config.rb', line 57
def colors?
config[:colors].nil? ? false : config[:colors]
end
|
#config ⇒ Object
27
28
29
30
31
32
33
34
|
# File 'lib/twat/config.rb', line 27
def config
begin
@config ||= YAML.load_file(config_path)
validate!(@config)
rescue Errno::ENOENT
raise NoConfigFile
end
end
|
#config_path ⇒ Object
6
7
8
|
# File 'lib/twat/config.rb', line 6
def config_path
@config_path ||= ENV['TWAT_CONFIG'] || "#{ENV['HOME']}/.twatrc"
end
|
#create! ⇒ Object
14
15
16
17
|
# File 'lib/twat/config.rb', line 14
def create!
@config = { accounts: {} }
save!
end
|
#create_unless_exists! ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/twat/config.rb', line 19
def create_unless_exists!
begin
config
rescue NoConfigFile
@config = { accounts: {} }
end
end
|
#endpoint ⇒ Object
104
105
106
|
# File 'lib/twat/config.rb', line 104
def endpoint
@endpoint ||= Endpoint.new(endpoint_name)
end
|
#endpoint_name ⇒ Object
100
101
102
|
# File 'lib/twat/config.rb', line 100
def endpoint_name
account[:endpoint] || :twitter
end
|
#exists? ⇒ Boolean
10
11
12
|
# File 'lib/twat/config.rb', line 10
def exists?
File.exist?(config_path)
end
|
#polling_interval ⇒ Object
69
70
71
|
# File 'lib/twat/config.rb', line 69
def polling_interval
config[:polling_interval] || 60
end
|
#save! ⇒ Object
42
43
44
45
46
47
|
# File 'lib/twat/config.rb', line 42
def save!
File.open(config_path, 'w') do |conf|
conf.chmod(0600)
conf.puts(@config.to_yaml)
end
end
|
#show_mentions? ⇒ Boolean
65
66
67
|
# File 'lib/twat/config.rb', line 65
def show_mentions?
config[:show_mentions].nil? ? false : config[:show_mentions]
end
|
#update_config ⇒ Object
update! migrates an old config file to the current API it does this by calling a sequence of migration functions in order which rebuild the config in stages, saving and leaving it in a consistent step at each point
81
82
83
84
|
# File 'lib/twat/config.rb', line 81
def update_config
raise NoConfigFile unless exists?
Migrate.new.migrate!(config_path)
end
|
#validate!(conf) ⇒ Object
36
37
38
39
40
|
# File 'lib/twat/config.rb', line 36
def validate!(conf)
raise ConfigVersionIncorrect unless conf.include?(:accounts)
conf
end
|