Class: Core::Config
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Instance Method Summary collapse
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #load(file = Core::DEFAULT_CONFIG) ⇒ Object
- #save(file = Core::DEFAULT_CONFIG) ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/config.rb', line 8 def initialize @hash = { :opengl => true, :language => "de", :volume => 0.5, # 0 = normal, 1 = twice as loud :contrast => 1.0, # 1.0 = off :log => true, # whether to log to file or print everything on stdout :text_speed => 5, # higher = slower } end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
6 7 8 |
# File 'lib/config.rb', line 6 def hash @hash end |
Instance Method Details
#load(file = Core::DEFAULT_CONFIG) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/config.rb', line 19 def load(file=Core::DEFAULT_CONFIG) if !File.exists?(Core::HOME_PATH + file) File.new(Core::HOME_PATH + file, "w") else if File.size(Core::HOME_PATH + file) > 0 begin @hash = Marshal.load(File.open(Core::HOME_PATH + file, "r")) validate rescue TypeError puts("WARNING: Corrupted config file (#{file}), reverting to default") rescue NoMethodError # this is likely caused by outdated configs warn("WARNING: Config file #{file} is outdated, deleting") File.delete(Core::HOME_PATH + file) end end end end |
#save(file = Core::DEFAULT_CONFIG) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/config.rb', line 37 def save(file=Core::DEFAULT_CONFIG) validate Marshal.dump(@hash, File.open(Core::HOME_PATH + file, "w")) rescue NoMethodError # this is likely caused by outdated configs warn("WARNING: Config file #{file} is outdated, deleting") File.delete(Core::HOME_PATH + file) end |
#validate ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/config.rb', line 45 def validate if !["en", "de"].include?(@hash[:language]) @hash[:language] = "de" end vol = @hash[:volume] if vol < 0 @hash[:volume] = 0.0 elsif vol > 1 @hash[:volume] = 1.0 end speed = @hash[:text_speed] if speed < 1 @hash[:text_speed] = 1 elsif speed > 10 @hash[:text_speed] = 10 end end |