Class: ReliableMsg::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/reliable-msg/queue-manager.rb

Overview

:nodoc:

Constant Summary collapse

CONFIG_FILE =
"queues.cfg"
DEFAULT_STORE =
MessageStore::Disk::DEFAULT_CONFIG
DEFAULT_DRB =
{
    "port"=>Client::DRB_PORT,
    "acl"=>"allow 127.0.0.1"
}
INFO_LOADED_CONFIG =

:nodoc:

"Loaded queues configuration from: %s"
INFO_CREATED_CONFIG =

:nodoc:

"Created queues configuration file in: %s"

Instance Method Summary collapse

Constructor Details

#initialize(file, logger = nil) ⇒ Config

Returns a new instance of Config.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/reliable-msg/queue-manager.rb', line 46

def initialize file, logger = nil
    @logger = logger
    # If no file specified, attempt to look for file in current directory.
    # If not found in current directory, look for file in Gem directory.
    unless file
        file = if File.exist?(CONFIG_FILE)
            CONFIG_FILE
        else
            file = File.expand_path(File.join(File.dirname(__FILE__), '..'))
            File.basename(file) == 'lib' ? File.join(file, '..', CONFIG_FILE) : File.join(file, CONFIG_FILE)
        end
    end
    @file = File.expand_path(file)
    @config = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/reliable-msg/queue-manager.rb', line 127

def method_missing symbol, *args
    if symbol.to_s[-1] == ?=
        @config[symbol.to_s[0...-1]] = *args
    else
        @config[symbol.to_s]
    end
end

Instance Method Details

#create_if_noneObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/reliable-msg/queue-manager.rb', line 96

def create_if_none
    if File.exist?(@file)
        false
    else
        @config = {
            "store" => DEFAULT_STORE,
            "drb" => DEFAULT_DRB
        }.merge(@config)
        save
        true
    end
end

#exist?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/reliable-msg/queue-manager.rb', line 110

def exist?
    File.exist?(@file)
end

#load_no_createObject



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/reliable-msg/queue-manager.rb', line 63

def load_no_create
    if File.exist?(@file)
        @config= {}
        File.open @file, "r" do |input|
            YAML.load_documents input do |doc|
                @config.merge! doc
            end
        end
        true
    end
end

#load_or_createObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/reliable-msg/queue-manager.rb', line 76

def load_or_create
    if File.exist?(@file)
        @config= {}
        File.open @file, "r" do |input|
            YAML.load_documents input do |doc|
                @config.merge! doc
            end
        end
        @logger.info format(INFO_LOADED_CONFIG, @file)
    else
        @config = {
            "store" => DEFAULT_STORE,
            "drb" => DEFAULT_DRB
        }
        save
        @logger.info format(INFO_CREATED_CONFIG, @file)
    end
end

#pathObject



115
116
117
# File 'lib/reliable-msg/queue-manager.rb', line 115

def path
    @file
end

#saveObject



120
121
122
123
124
# File 'lib/reliable-msg/queue-manager.rb', line 120

def save
    File.open @file, "w" do |output|
        YAML::dump @config, output
    end
end