Class: ConfigStruct
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- ConfigStruct
- Defined in:
- lib/configstruct.rb
Instance Method Summary collapse
- #addvalues ⇒ Object
- #default(var, value) ⇒ Object
- #gets(*args) ⇒ Object
-
#initialize(options = nil, input = STDIN, output = STDOUT) ⇒ ConfigStruct
constructor
A new instance of ConfigStruct.
- #prepare_dirs ⇒ Object
- #print(*string) ⇒ Object
- #printf(string, *args) ⇒ Object
- #puts(*string) ⇒ Object
- #set_defaults ⇒ Object
- #setup ⇒ Object
- #write(values) ⇒ Object
Constructor Details
#initialize(options = nil, input = STDIN, output = STDOUT) ⇒ ConfigStruct
Returns a new instance of ConfigStruct.
6 7 8 9 10 11 12 13 |
# File 'lib/configstruct.rb', line 6 def initialize( = nil, input = STDIN, output = STDOUT) super() @input = input @output = output set_defaults prepare_dirs addvalues end |
Instance Method Details
#addvalues ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/configstruct.rb', line 24 def addvalues setup unless File.exist? self.basefile YAML.load_file(self.basefile).each do |k, v| new_ostruct_member(k) send("#{k}=", v) end end |
#default(var, value) ⇒ Object
36 37 38 |
# File 'lib/configstruct.rb', line 36 def default(var, value) send(var).nil? && send("#{var}=", value) end |
#gets(*args) ⇒ Object
58 59 60 |
# File 'lib/configstruct.rb', line 58 def gets(*args) @input.gets *args end |
#prepare_dirs ⇒ Object
20 21 22 |
# File 'lib/configstruct.rb', line 20 def prepare_dirs FileUtils.mkdir_p self.basedir unless Dir.exist? self.basedir end |
#print(*string) ⇒ Object
50 51 52 |
# File 'lib/configstruct.rb', line 50 def print(*string) @output.print *string end |
#printf(string, *args) ⇒ Object
54 55 56 |
# File 'lib/configstruct.rb', line 54 def printf(string, *args) @output.printf string, *args end |
#puts(*string) ⇒ Object
46 47 48 |
# File 'lib/configstruct.rb', line 46 def puts(*string) @output.puts *string end |
#set_defaults ⇒ Object
15 16 17 18 |
# File 'lib/configstruct.rb', line 15 def set_defaults default :basedir, '/tmp' default :basefile, File.join(self.basedir, 'config.yml') end |
#setup ⇒ Object
32 33 34 |
# File 'lib/configstruct.rb', line 32 def setup write Hash.new end |
#write(values) ⇒ Object
40 41 42 43 44 |
# File 'lib/configstruct.rb', line 40 def write(values) File.open(self.basefile, 'w') do |f| f.write YAML.dump(values) end end |