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"=>Queue::DRB_PORT,
    "acl"=>"allow 127.0.0.1"
}

Instance Method Summary collapse

Constructor Details

#initialize(file, logger = nil) ⇒ Config

Returns a new instance of Config.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/reliable-msg/queue-manager.rb', line 35

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



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

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



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

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)


95
96
97
# File 'lib/reliable-msg/queue-manager.rb', line 95

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

#load_no_createObject



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

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/reliable-msg/queue-manager.rb', line 63

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 "Loaded queues configuration from: #{@file}"
    else
        @config = {
            "store" => DEFAULT_STORE,
            "drb" => DEFAULT_DRB
        }
        save
        @logger.info "Created queues configuration file in: #{@file}"
    end
end

#pathObject



99
100
101
# File 'lib/reliable-msg/queue-manager.rb', line 99

def path
    @file
end

#saveObject



103
104
105
106
107
# File 'lib/reliable-msg/queue-manager.rb', line 103

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