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.



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

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



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

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



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

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)


97
98
99
# File 'lib/reliable-msg/queue-manager.rb', line 97

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

#load_no_createObject



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

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



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

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



101
102
103
# File 'lib/reliable-msg/queue-manager.rb', line 101

def path
    @file
end

#saveObject



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

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