Class: Rgtk::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/rgtk/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_name = 'config.yml') ⇒ Config

Returns a new instance of Config.



3
4
5
6
7
8
9
10
11
# File 'lib/rgtk/config.rb', line 3

def initialize(file_name = 'config.yml')
  @file_name = File.join(::App.config_dir, file_name)
  @config = if File.exists?(@file_name)
              YAML.load_file(@file_name) || {}
            else
              `touch #{@file_name}`
              {}
            end.to_hash.with_indifferent_access
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/rgtk/config.rb', line 13

def method_missing(method_name, *args)
  if method_name.to_s =~ /^([a-z0-9_]+)=$/
    @config[$1] = args.first
  else
    @config[method_name]
  end
end

Instance Method Details

#[](name) ⇒ Object



21
22
23
# File 'lib/rgtk/config.rb', line 21

def [](name)
  @config[name]
end

#[]=(name, value) ⇒ Object



25
26
27
# File 'lib/rgtk/config.rb', line 25

def []=(name, value)
  @config[name] = value
end

#save!Object



29
30
31
# File 'lib/rgtk/config.rb', line 29

def save!
  File.open(@file_name, 'w') { |f| f.write(@config.to_yaml) }
end