Class: Mongo::Lock::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo-lock/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defaults, options) {|_self| ... } ⇒ Configuration

Returns a new instance of Configuration.

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mongo-lock/configuration.rb', line 14

def initialize defaults, options, &block
  options = defaults.merge(options)
  options[:collections] ||= {}
  if options[:collection]
    options[:collections][:default] = options[:collection]
  end
  options.each_pair do |key,value|
    self.send(:"#{key}=",value)
  end
  yield self if block_given?
end

Instance Attribute Details

#connectionsObject

Returns the value of attribute connections.



5
6
7
# File 'lib/mongo-lock/configuration.rb', line 5

def connections
  @connections
end

#driverObject

Returns the value of attribute driver.



12
13
14
# File 'lib/mongo-lock/configuration.rb', line 12

def driver
  @driver
end

#expire_inObject

Returns the value of attribute expire_in.



9
10
11
# File 'lib/mongo-lock/configuration.rb', line 9

def expire_in
  @expire_in
end

#frequencyObject

Returns the value of attribute frequency.



8
9
10
# File 'lib/mongo-lock/configuration.rb', line 8

def frequency
  @frequency
end

#limitObject

Returns the value of attribute limit.



6
7
8
# File 'lib/mongo-lock/configuration.rb', line 6

def limit
  @limit
end

#ownerObject

Returns the value of attribute owner.



10
11
12
# File 'lib/mongo-lock/configuration.rb', line 10

def owner
  @owner
end

#should_raiseObject

Returns the value of attribute should_raise.



11
12
13
# File 'lib/mongo-lock/configuration.rb', line 11

def should_raise
  @should_raise
end

#timeout_inObject

Returns the value of attribute timeout_in.



7
8
9
# File 'lib/mongo-lock/configuration.rb', line 7

def timeout_in
  @timeout_in
end

Instance Method Details

#add_single_collection_to_collections(options) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/mongo-lock/configuration.rb', line 117

def add_single_collection_to_collections options
  if options[:collection].is_a? Symbol
    options[:collections] << self.collection(options[:collection])
  elsif options[:collection]
    options[:collections] << options[:collection]
  end
  options
end

#array_of_collections(options) ⇒ Object



112
113
114
115
# File 'lib/mongo-lock/configuration.rb', line 112

def array_of_collections options
  options[:collections] = options[:collections].try(:values) || options[:collections] || []
  options
end

#choose_driver(provided_collections) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mongo-lock/configuration.rb', line 52

def choose_driver provided_collections
  collections = provided_collections.clone
  collections = collections.values if collections.is_a? Hash

  if collections.is_a? Array
    collection = collections.first
    collection_class = collections.map{ |x| x.class }.uniq
    raise MixedCollectionsError.new "Collections must be of the same class" if collection_class.size > 1
  else
    collection = collections
  end

  if collection.is_a? Moped::Collection
    require 'mongo-lock/drivers/moped'
    self.driver = Mongo::Lock::Drivers::Moped
  elsif collection.is_a?(Mongo::Collection) or collection.nil? or collection.is_a?(String) or collection.is_a?(Symbol)
    require 'mongo-lock/drivers/mongo'
    self.driver = Mongo::Lock::Drivers::Mongo
  else
    raise InvalidCollectionError.new "#{collection.class.name} is not a valid collection class"
  end

  provided_collections
end

#collection(collection = :default) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/mongo-lock/configuration.rb', line 30

def collection collection = :default
  collection = collection.to_sym if collection.is_a? String
  if collection.is_a? Symbol
    collections[collection]
  else
    collection
  end
end

#collection=(collection) ⇒ Object



26
27
28
# File 'lib/mongo-lock/configuration.rb', line 26

def collection= collection
  collections[:default] = choose_driver collection
end

#collectionsObject



48
49
50
# File 'lib/mongo-lock/configuration.rb', line 48

def collections
  @collections ||= {}
end

#collections=(collections) ⇒ Object



39
40
41
# File 'lib/mongo-lock/configuration.rb', line 39

def collections= collections
  @collections = choose_driver collections
end

#process_collection_options(options) ⇒ Object



105
106
107
108
109
110
# File 'lib/mongo-lock/configuration.rb', line 105

def process_collection_options options
  options = array_of_collections options
  options = add_single_collection_to_collections options
  options = use_registered_collections_if_empty options
  options
end

#set_collections_keep_default(collections) ⇒ Object



43
44
45
46
# File 'lib/mongo-lock/configuration.rb', line 43

def set_collections_keep_default collections
  collections[:default] = @collections[:default]
  @collections = collections
end

#to_hashObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/mongo-lock/configuration.rb', line 84

def to_hash
  {
    collections: collections,
    timeout_in: timeout_in,
    limit: limit,
    frequency: frequency,
    expire_in: expire_in,
    owner: owner,
    driver: driver,
    should_raise: should_raise
  }
end

#use_registered_collections_if_empty(options) ⇒ Object



126
127
128
129
130
131
# File 'lib/mongo-lock/configuration.rb', line 126

def use_registered_collections_if_empty options
  if options[:collections].empty?
    options[:collections] = self.collections.values
  end
  options
end