Class: Billy::Config
- Inherits:
-
Object
show all
- Defined in:
- lib/billy/config.rb
Constant Summary
collapse
- BILLYRC =
'.billyrc'
- SEPARATOR_PATTERN =
/:\s*/
- SEPARATOR =
': '
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
11
12
13
14
15
16
17
18
19
|
# File 'lib/billy/config.rb', line 11
def method_missing( m, *args, &block )
if m.to_s[ /=$/ ].nil?
self.storage[ m.to_s ]
else
key = ( m.to_s )[ /^([^=]+)/ ]
val = args.shift
( self.storage[ key ] = val ) unless key.nil? && key.empty?
end
end
|
Instance Attribute Details
#storage ⇒ Object
Returns the value of attribute storage.
8
9
10
|
# File 'lib/billy/config.rb', line 8
def storage
@storage
end
|
#storage_path ⇒ Object
Returns the value of attribute storage_path.
9
10
11
|
# File 'lib/billy/config.rb', line 9
def storage_path
@storage_path
end
|
Class Method Details
.instance ⇒ Object
103
104
105
|
# File 'lib/billy/config.rb', line 103
def instance
@@instance ||= self.new
end
|
.method_missing(m, *args, &block) ⇒ Object
111
112
113
|
# File 'lib/billy/config.rb', line 111
def method_missing( m, *args, &block )
instance.send( m, *args, &block ) if instance.respond_to?( m )
end
|
.settings ⇒ Object
107
108
109
|
# File 'lib/billy/config.rb', line 107
def settings
instance.storage
end
|
Instance Method Details
#clear ⇒ Object
53
54
55
|
# File 'lib/billy/config.rb', line 53
def clear
self.storage.clear
end
|
#config_exists?(dir) ⇒ Boolean
21
22
23
|
# File 'lib/billy/config.rb', line 21
def config_exists?( dir )
File.exists?( File.expand_path( dir + "/#{BILLYRC}" ) )
end
|
#eat_string_config(string_config) ⇒ Object
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/billy/config.rb', line 84
def eat_string_config( string_config )
clear
string_config.each_line do |line|
next unless !line.empty?
items = line.split( SEPARATOR_PATTERN )
k = items.shift
v = items.join( SEPARATOR ).strip
( self.storage[ k.to_s ] = v ) unless k.nil? || k.empty? || v.nil? || v.empty?
end
end
|
#load ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/billy/config.rb', line 33
def load
%w(. ~).each do |path|
begin
load!( File.expand_path( path ) )
return true
rescue
next
end
end
false
end
|
#load!(dir) ⇒ Object
46
47
48
49
50
51
|
# File 'lib/billy/config.rb', line 46
def load!( dir )
self.storage_path = File.expand_path( dir )
file_path = storage_path + "/#{BILLYRC}"
raise "Config was not found in #{path}" unless File.exists?( file_path )
eat_string_config( File.read( file_path ) )
end
|
#reload!(dir = nil) ⇒ Object
57
58
59
60
61
|
# File 'lib/billy/config.rb', line 57
def reload!( dir = nil )
dir ||= storage_path
clear
load!( dir )
end
|
#save(dir = nil) ⇒ Object
63
64
65
66
67
68
69
70
71
|
# File 'lib/billy/config.rb', line 63
def save( dir = nil )
dir ||= Dir.pwd
begin
save!( dir, true )
true
rescue
false
end
end
|
#save!(dir, force = false) ⇒ Object
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/billy/config.rb', line 73
def save!( dir, force = false )
raise 'Directory name should not be empty' unless !dir.empty?
raise "Directory #{dir.to_s} doesn't exist" unless File.exist?( File.expand_path( dir ) )
billyrc_path = File.expand_path( dir + "/#{BILLYRC}" )
raise "Config already exists in #{billyrc_path}" unless !File.exists?( billyrc_path ) || force
File.open( billyrc_path, 'w' ) { |file|
file.flush
file.write( self.to_s )
}
end
|
#to_s ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/billy/config.rb', line 25
def to_s
[].tap { |res|
self.storage.each_pair do |k, v|
res.push "#{k}#{SEPARATOR}#{v}"
end
}.push( '' ).join( "\n" )
end
|