Class: MongoDelta::Configuration
- Inherits:
-
Object
- Object
- MongoDelta::Configuration
- Defined in:
- lib/mongo_delta/configuration.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- DEFAULT_DB =
'mongo_delta'
- DEFAULT_SERVICE =
'mongo_delta'
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
- .connect_to_source(connection_or_uri) ⇒ Object
- .load_from_db(mongodb_uri, db = nil, service = nil) ⇒ Object
- .load_from_file(path) ⇒ Object
Instance Method Summary collapse
- #db ⇒ Object
- #db=(db) ⇒ Object
- #ignore_errors ⇒ Object
- #ignore_errors=(bool) ⇒ Object
-
#initialize(options = {}) ⇒ Configuration
constructor
A new instance of Configuration.
- #outlets ⇒ Object
- #service ⇒ Object
- #service=(service) ⇒ Object
- #targets ⇒ Object
- #validate! ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Configuration
Returns a new instance of Configuration.
42 43 44 45 46 |
# File 'lib/mongo_delta/configuration.rb', line 42 def initialize(={}) @options = @source = self.class.connect_to_source(['source']) validate! end |
Instance Attribute Details
#source ⇒ Object (readonly)
Returns the value of attribute source.
40 41 42 |
# File 'lib/mongo_delta/configuration.rb', line 40 def source @source end |
Class Method Details
.connect_to_source(connection_or_uri) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/mongo_delta/configuration.rb', line 32 def self.connect_to_source(connection_or_uri) if connection_or_uri.is_a? Mongo::MongoClient connection_or_uri else Mongo::MongoClient.from_uri(connection_or_uri) end end |
.load_from_db(mongodb_uri, db = nil, service = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/mongo_delta/configuration.rb', line 18 def self.load_from_db(mongodb_uri, db=nil, service=nil) db ||= DEFAULT_DB service ||= DEFAULT_SERVICE mongo = connect_to_source(mongodb_uri) collection = mongo.db(db).collection('config') unless = collection.find_one(:_id => service) raise Error, "There was no config in the database at #{mongodb_uri}/#{db} with id '#{service}'" end new(.merge({'source' => mongo, 'db' => db, 'service' => service})) end |
.load_from_file(path) ⇒ Object
13 14 15 16 |
# File 'lib/mongo_delta/configuration.rb', line 13 def self.load_from_file(path) = YAML.load(ERB.new(File.read(path)).result) new() end |
Instance Method Details
#db ⇒ Object
66 67 68 |
# File 'lib/mongo_delta/configuration.rb', line 66 def db @options['db'] || DEFAULT_DB end |
#db=(db) ⇒ Object
69 70 71 |
# File 'lib/mongo_delta/configuration.rb', line 69 def db=(db) @options['db'] = db end |
#ignore_errors ⇒ Object
80 81 82 |
# File 'lib/mongo_delta/configuration.rb', line 80 def ignore_errors !!@options['ignore_errors'] end |
#ignore_errors=(bool) ⇒ Object
83 84 85 |
# File 'lib/mongo_delta/configuration.rb', line 83 def ignore_errors=(bool) @options['ignore_errors'] = bool end |
#outlets ⇒ Object
52 53 54 |
# File 'lib/mongo_delta/configuration.rb', line 52 def outlets @options['outlets'] end |
#service ⇒ Object
73 74 75 |
# File 'lib/mongo_delta/configuration.rb', line 73 def service @options['service'] || DEFAULT_SERVICE end |
#service=(service) ⇒ Object
76 77 78 |
# File 'lib/mongo_delta/configuration.rb', line 76 def service=(service) @options['service'] = service end |
#targets ⇒ Object
48 49 50 |
# File 'lib/mongo_delta/configuration.rb', line 48 def targets @options['targets'] end |
#validate! ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/mongo_delta/configuration.rb', line 56 def validate! raise Error, "Missing source" unless source raise Error, "Missing outlets" unless outlets and not outlets.empty? outlets.each do |key, outlet| target_key = outlet['target'] target = (targets || {})[target_key] raise Error, "Missing target '#{target_key}'" unless target end end |