class Everdone::Config

Public Class Methods

new(default_hash_file, user_settings_file) click to toggle source
# File lib/everdone/config.rb, line 10
def initialize(default_hash_file, user_settings_file)
    @defaults = unmarshall_from_file(default_hash_file)
    @user_settings_file = user_settings_file
    @users = unmarshall_from_file(@user_settings_file)
    @current = {}

    collate_settings()
end

Private Instance Methods

collate_settings() click to toggle source
# File lib/everdone/config.rb, line 48
def collate_settings
    # add all the settings (every setting has a default) as a member variable of this class
    @defaults.each { |name, val|  
        define_instance_var(name,val)
    }
    # combine the default settings and the settings from the user file into the current settings
    @current = @defaults
    @users.each { |name, val|  
        if @current.has_key?(name)
            @current[name] = val
            instance_variable_set("@#{name}", val)
        else
            puts "WARN: the settings file #{@user_settings_file} contained an unrecognized setting #{name} (value = #{val})"
        end
    }
end
define_instance_var(name, value) click to toggle source
# File lib/everdone/config.rb, line 21
def define_instance_var(name, value)
    self.class.module_eval { attr_accessor name.to_sym }
    instance_variable_set("@#{name}", value)
end
marshall_to_file(filename) click to toggle source
# File lib/everdone/config.rb, line 36
def marshall_to_file(filename)
end
marshall_to_string() click to toggle source

return a string of JSON for the current settings

# File lib/everdone/config.rb, line 27
def marshall_to_string()
    current = JSON.generate(@current)
    return current
end
unmarshall_from_file(filename) click to toggle source
# File lib/everdone/config.rb, line 39
def unmarshall_from_file(filename)
    users = {}
    return users if not File.exist?(filename)
    File.open(filename, "r") do |f|
        users = JSON.load(f)
    end
    return users
end
unmarshall_from_string(new_config) click to toggle source
# File lib/everdone/config.rb, line 32
def unmarshall_from_string(new_config)
    return JSON.parse(new_config)
end