emipair-delayed_job 2.0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -0
- data/MIT-LICENSE +20 -0
- data/README.textile +213 -0
- data/Rakefile +33 -0
- data/VERSION +1 -0
- data/benchmarks.rb +33 -0
- data/contrib/delayed_job.monitrc +14 -0
- data/contrib/delayed_job_multiple.monitrc +23 -0
- data/emipair-delayed_job.gemspec +25 -0
- data/generators/delayed_job/delayed_job_generator.rb +22 -0
- data/generators/delayed_job/templates/migration.rb +21 -0
- data/generators/delayed_job/templates/script +5 -0
- data/init.rb +1 -0
- data/lib/delayed/backend/active_record.rb +90 -0
- data/lib/delayed/backend/base.rb +111 -0
- data/lib/delayed/backend/data_mapper.rb +147 -0
- data/lib/delayed/backend/mongo_mapper.rb +110 -0
- data/lib/delayed/command.rb +109 -0
- data/lib/delayed/message_sending.rb +22 -0
- data/lib/delayed/performable_method.rb +62 -0
- data/lib/delayed/railtie.rb +10 -0
- data/lib/delayed/recipes.rb +31 -0
- data/lib/delayed/tasks.rb +15 -0
- data/lib/delayed/worker.rb +214 -0
- data/lib/delayed_job.rb +15 -0
- data/lib/passive_support.rb +4 -0
- data/lib/passive_support/basic_object.rb +16 -0
- data/lib/passive_support/core_ext.rb +8 -0
- data/lib/passive_support/core_ext/class.rb +1 -0
- data/lib/passive_support/core_ext/class/attribute_accessors.rb +57 -0
- data/lib/passive_support/core_ext/date.rb +10 -0
- data/lib/passive_support/core_ext/date/behavior.rb +42 -0
- data/lib/passive_support/core_ext/date/calculations.rb +241 -0
- data/lib/passive_support/core_ext/date/conversions.rb +107 -0
- data/lib/passive_support/core_ext/date_time.rb +12 -0
- data/lib/passive_support/core_ext/date_time/calculations.rb +126 -0
- data/lib/passive_support/core_ext/date_time/conversions.rb +107 -0
- data/lib/passive_support/core_ext/enumerable.rb +120 -0
- data/lib/passive_support/core_ext/kernel.rb +5 -0
- data/lib/passive_support/core_ext/kernel/agnostics.rb +11 -0
- data/lib/passive_support/core_ext/kernel/daemonizing.rb +7 -0
- data/lib/passive_support/core_ext/kernel/debugger.rb +16 -0
- data/lib/passive_support/core_ext/kernel/reporting.rb +59 -0
- data/lib/passive_support/core_ext/kernel/requires.rb +24 -0
- data/lib/passive_support/core_ext/module.rb +20 -0
- data/lib/passive_support/core_ext/module/aliasing.rb +74 -0
- data/lib/passive_support/core_ext/module/attr_accessor_with_default.rb +31 -0
- data/lib/passive_support/core_ext/module/attr_internal.rb +32 -0
- data/lib/passive_support/core_ext/module/delegation.rb +135 -0
- data/lib/passive_support/core_ext/module/inclusion.rb +30 -0
- data/lib/passive_support/core_ext/module/introspection.rb +90 -0
- data/lib/passive_support/core_ext/module/loading.rb +23 -0
- data/lib/passive_support/core_ext/module/model_naming.rb +25 -0
- data/lib/passive_support/core_ext/module/synchronization.rb +39 -0
- data/lib/passive_support/core_ext/numeric.rb +9 -0
- data/lib/passive_support/core_ext/numeric/bytes.rb +50 -0
- data/lib/passive_support/core_ext/numeric/conversions.rb +19 -0
- data/lib/passive_support/core_ext/numeric/time.rb +81 -0
- data/lib/passive_support/core_ext/object.rb +6 -0
- data/lib/passive_support/core_ext/object/blank.rb +76 -0
- data/lib/passive_support/core_ext/object/conversions.rb +15 -0
- data/lib/passive_support/core_ext/object/extending.rb +80 -0
- data/lib/passive_support/core_ext/object/instance_variables.rb +74 -0
- data/lib/passive_support/core_ext/object/misc.rb +90 -0
- data/lib/passive_support/core_ext/object/singleton_class.rb +13 -0
- data/lib/passive_support/core_ext/string.rb +1 -0
- data/lib/passive_support/core_ext/string/constantize.rb +7 -0
- data/lib/passive_support/core_ext/time.rb +46 -0
- data/lib/passive_support/core_ext/time/behavior.rb +13 -0
- data/lib/passive_support/core_ext/time/calculations.rb +313 -0
- data/lib/passive_support/core_ext/time/conversions.rb +90 -0
- data/lib/passive_support/core_ext/time/zones.rb +86 -0
- data/lib/passive_support/duration.rb +100 -0
- data/lib/passive_support/ordered_hash.rb +158 -0
- data/rails/init.rb +5 -0
- data/recipes/delayed_job.rb +1 -0
- data/spec/backend/active_record_job_spec.rb +46 -0
- data/spec/backend/data_mapper_job_spec.rb +16 -0
- data/spec/backend/mongo_mapper_job_spec.rb +94 -0
- data/spec/backend/shared_backend_spec.rb +268 -0
- data/spec/delayed_method_spec.rb +58 -0
- data/spec/performable_method_spec.rb +42 -0
- data/spec/sample_jobs.rb +25 -0
- data/spec/setup/active_record.rb +33 -0
- data/spec/setup/data_mapper.rb +24 -0
- data/spec/setup/mongo_mapper.rb +17 -0
- data/spec/spec_helper.rb +19 -0
- data/spec/story_spec.rb +17 -0
- data/spec/worker_spec.rb +225 -0
- data/tasks/jobs.rake +1 -0
- metadata +323 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
class Module
|
|
2
|
+
# Declare an attribute accessor with an initial default return value.
|
|
3
|
+
#
|
|
4
|
+
# To give attribute <tt>:age</tt> the initial value <tt>25</tt>:
|
|
5
|
+
#
|
|
6
|
+
# class Person
|
|
7
|
+
# attr_accessor_with_default :age, 25
|
|
8
|
+
# end
|
|
9
|
+
#
|
|
10
|
+
# some_person.age
|
|
11
|
+
# => 25
|
|
12
|
+
# some_person.age = 26
|
|
13
|
+
# some_person.age
|
|
14
|
+
# => 26
|
|
15
|
+
#
|
|
16
|
+
# To give attribute <tt>:element_name</tt> a dynamic default value, evaluated
|
|
17
|
+
# in scope of self:
|
|
18
|
+
#
|
|
19
|
+
# attr_accessor_with_default(:element_name) { name.underscore }
|
|
20
|
+
#
|
|
21
|
+
def attr_accessor_with_default(sym, default = nil, &block)
|
|
22
|
+
raise 'Default value or block required' unless !default.nil? || block
|
|
23
|
+
define_method(sym, block_given? ? block : Proc.new { default })
|
|
24
|
+
module_eval(<<-EVAL, __FILE__, __LINE__ + 1)
|
|
25
|
+
def #{sym}=(value) # def age=(value)
|
|
26
|
+
class << self; attr_reader :#{sym} end # class << self; attr_reader :age end
|
|
27
|
+
@#{sym} = value # @age = value
|
|
28
|
+
end # end
|
|
29
|
+
EVAL
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
class Module
|
|
2
|
+
# Declares an attribute reader backed by an internally-named instance variable.
|
|
3
|
+
def attr_internal_reader(*attrs)
|
|
4
|
+
attrs.each do |attr|
|
|
5
|
+
module_eval "def #{attr}() #{attr_internal_ivar_name(attr)} end"
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
# Declares an attribute writer backed by an internally-named instance variable.
|
|
10
|
+
def attr_internal_writer(*attrs)
|
|
11
|
+
attrs.each do |attr|
|
|
12
|
+
module_eval "def #{attr}=(v) #{attr_internal_ivar_name(attr)} = v end"
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Declares an attribute reader and writer backed by an internally-named instance
|
|
17
|
+
# variable.
|
|
18
|
+
def attr_internal_accessor(*attrs)
|
|
19
|
+
attr_internal_reader(*attrs)
|
|
20
|
+
attr_internal_writer(*attrs)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
alias_method :attr_internal, :attr_internal_accessor
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
mattr_accessor :attr_internal_naming_format
|
|
27
|
+
self.attr_internal_naming_format = '@_%s'
|
|
28
|
+
|
|
29
|
+
def attr_internal_ivar_name(attr)
|
|
30
|
+
attr_internal_naming_format % attr
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
class Module
|
|
2
|
+
# Provides a delegate class method to easily expose contained objects' methods
|
|
3
|
+
# as your own. Pass one or more methods (specified as symbols or strings)
|
|
4
|
+
# and the name of the target object as the final <tt>:to</tt> option (also a symbol
|
|
5
|
+
# or string). At least one method and the <tt>:to</tt> option are required.
|
|
6
|
+
#
|
|
7
|
+
# Delegation is particularly useful with Active Record associations:
|
|
8
|
+
#
|
|
9
|
+
# class Greeter < ActiveRecord::Base
|
|
10
|
+
# def hello() "hello" end
|
|
11
|
+
# def goodbye() "goodbye" end
|
|
12
|
+
# end
|
|
13
|
+
#
|
|
14
|
+
# class Foo < ActiveRecord::Base
|
|
15
|
+
# belongs_to :greeter
|
|
16
|
+
# delegate :hello, :to => :greeter
|
|
17
|
+
# end
|
|
18
|
+
#
|
|
19
|
+
# Foo.new.hello # => "hello"
|
|
20
|
+
# Foo.new.goodbye # => NoMethodError: undefined method `goodbye' for #<Foo:0x1af30c>
|
|
21
|
+
#
|
|
22
|
+
# Multiple delegates to the same target are allowed:
|
|
23
|
+
#
|
|
24
|
+
# class Foo < ActiveRecord::Base
|
|
25
|
+
# belongs_to :greeter
|
|
26
|
+
# delegate :hello, :goodbye, :to => :greeter
|
|
27
|
+
# end
|
|
28
|
+
#
|
|
29
|
+
# Foo.new.goodbye # => "goodbye"
|
|
30
|
+
#
|
|
31
|
+
# Methods can be delegated to instance variables, class variables, or constants
|
|
32
|
+
# by providing them as a symbols:
|
|
33
|
+
#
|
|
34
|
+
# class Foo
|
|
35
|
+
# CONSTANT_ARRAY = [0,1,2,3]
|
|
36
|
+
# @@class_array = [4,5,6,7]
|
|
37
|
+
#
|
|
38
|
+
# def initialize
|
|
39
|
+
# @instance_array = [8,9,10,11]
|
|
40
|
+
# end
|
|
41
|
+
# delegate :sum, :to => :CONSTANT_ARRAY
|
|
42
|
+
# delegate :min, :to => :@@class_array
|
|
43
|
+
# delegate :max, :to => :@instance_array
|
|
44
|
+
# end
|
|
45
|
+
#
|
|
46
|
+
# Foo.new.sum # => 6
|
|
47
|
+
# Foo.new.min # => 4
|
|
48
|
+
# Foo.new.max # => 11
|
|
49
|
+
#
|
|
50
|
+
# Delegates can optionally be prefixed using the <tt>:prefix</tt> option. If the value
|
|
51
|
+
# is <tt>true</tt>, the delegate methods are prefixed with the name of the object being
|
|
52
|
+
# delegated to.
|
|
53
|
+
#
|
|
54
|
+
# Person = Struct.new(:name, :address)
|
|
55
|
+
#
|
|
56
|
+
# class Invoice < Struct.new(:client)
|
|
57
|
+
# delegate :name, :address, :to => :client, :prefix => true
|
|
58
|
+
# end
|
|
59
|
+
#
|
|
60
|
+
# john_doe = Person.new("John Doe", "Vimmersvej 13")
|
|
61
|
+
# invoice = Invoice.new(john_doe)
|
|
62
|
+
# invoice.client_name # => "John Doe"
|
|
63
|
+
# invoice.client_address # => "Vimmersvej 13"
|
|
64
|
+
#
|
|
65
|
+
# It is also possible to supply a custom prefix.
|
|
66
|
+
#
|
|
67
|
+
# class Invoice < Struct.new(:client)
|
|
68
|
+
# delegate :name, :address, :to => :client, :prefix => :customer
|
|
69
|
+
# end
|
|
70
|
+
#
|
|
71
|
+
# invoice = Invoice.new(john_doe)
|
|
72
|
+
# invoice.customer_name # => "John Doe"
|
|
73
|
+
# invoice.customer_address # => "Vimmersvej 13"
|
|
74
|
+
#
|
|
75
|
+
# If the object to which you delegate can be nil, you may want to use the
|
|
76
|
+
# :allow_nil option. In that case, it returns nil instead of raising a
|
|
77
|
+
# NoMethodError exception:
|
|
78
|
+
#
|
|
79
|
+
# class Foo
|
|
80
|
+
# attr_accessor :bar
|
|
81
|
+
# def initialize(bar = nil)
|
|
82
|
+
# @bar = bar
|
|
83
|
+
# end
|
|
84
|
+
# delegate :zoo, :to => :bar
|
|
85
|
+
# end
|
|
86
|
+
#
|
|
87
|
+
# Foo.new.zoo # raises NoMethodError exception (you called nil.zoo)
|
|
88
|
+
#
|
|
89
|
+
# class Foo
|
|
90
|
+
# attr_accessor :bar
|
|
91
|
+
# def initialize(bar = nil)
|
|
92
|
+
# @bar = bar
|
|
93
|
+
# end
|
|
94
|
+
# delegate :zoo, :to => :bar, :allow_nil => true
|
|
95
|
+
# end
|
|
96
|
+
#
|
|
97
|
+
# Foo.new.zoo # returns nil
|
|
98
|
+
#
|
|
99
|
+
def delegate(*methods)
|
|
100
|
+
options = methods.pop
|
|
101
|
+
unless options.is_a?(Hash) && to = options[:to]
|
|
102
|
+
raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/
|
|
106
|
+
raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_"
|
|
110
|
+
|
|
111
|
+
file, line = caller.first.split(':', 2)
|
|
112
|
+
line = line.to_i
|
|
113
|
+
|
|
114
|
+
methods.each do |method|
|
|
115
|
+
on_nil =
|
|
116
|
+
if options[:allow_nil]
|
|
117
|
+
'return'
|
|
118
|
+
else
|
|
119
|
+
%(raise "#{prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
module_eval(<<-EOS, file, line)
|
|
123
|
+
def #{prefix}#{method}(*args, &block) # def customer_name(*args, &block)
|
|
124
|
+
#{to}.__send__(#{method.inspect}, *args, &block) # client.__send__(:name, *args, &block)
|
|
125
|
+
rescue NoMethodError # rescue NoMethodError
|
|
126
|
+
if #{to}.nil? # if client.nil?
|
|
127
|
+
#{on_nil}
|
|
128
|
+
else # else
|
|
129
|
+
raise # raise
|
|
130
|
+
end # end
|
|
131
|
+
end # end
|
|
132
|
+
EOS
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
class Module
|
|
2
|
+
# Returns the classes in the current ObjectSpace where this module has been
|
|
3
|
+
# mixed in according to Module#included_modules.
|
|
4
|
+
#
|
|
5
|
+
# module M
|
|
6
|
+
# end
|
|
7
|
+
#
|
|
8
|
+
# module N
|
|
9
|
+
# include M
|
|
10
|
+
# end
|
|
11
|
+
#
|
|
12
|
+
# class C
|
|
13
|
+
# include M
|
|
14
|
+
# end
|
|
15
|
+
#
|
|
16
|
+
# class D < C
|
|
17
|
+
# end
|
|
18
|
+
#
|
|
19
|
+
# p M.included_in_classes # => [C, D]
|
|
20
|
+
#
|
|
21
|
+
def included_in_classes
|
|
22
|
+
classes = []
|
|
23
|
+
ObjectSpace.each_object(Class) { |k| classes << k if k.included_modules.include?(self) }
|
|
24
|
+
|
|
25
|
+
classes.reverse.inject([]) do |unique_classes, klass|
|
|
26
|
+
unique_classes << klass unless unique_classes.collect { |k| k.to_s }.include?(klass.to_s)
|
|
27
|
+
unique_classes
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
module PassiveSupport
|
|
2
|
+
module CoreExtensions
|
|
3
|
+
module Module
|
|
4
|
+
# Returns the name of the module containing this one.
|
|
5
|
+
#
|
|
6
|
+
# p M::N.parent_name # => "M"
|
|
7
|
+
def parent_name
|
|
8
|
+
unless defined? @parent_name
|
|
9
|
+
@parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
|
|
10
|
+
end
|
|
11
|
+
@parent_name
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Returns the module which contains this one according to its name.
|
|
15
|
+
#
|
|
16
|
+
# module M
|
|
17
|
+
# module N
|
|
18
|
+
# end
|
|
19
|
+
# end
|
|
20
|
+
# X = M::N
|
|
21
|
+
#
|
|
22
|
+
# p M::N.parent # => M
|
|
23
|
+
# p X.parent # => M
|
|
24
|
+
#
|
|
25
|
+
# The parent of top-level and anonymous modules is Object.
|
|
26
|
+
#
|
|
27
|
+
# p M.parent # => Object
|
|
28
|
+
# p Module.new.parent # => Object
|
|
29
|
+
#
|
|
30
|
+
def parent
|
|
31
|
+
parent_name ? parent_name.constantize : Object
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Returns all the parents of this module according to its name, ordered from
|
|
35
|
+
# nested outwards. The receiver is not contained within the result.
|
|
36
|
+
#
|
|
37
|
+
# module M
|
|
38
|
+
# module N
|
|
39
|
+
# end
|
|
40
|
+
# end
|
|
41
|
+
# X = M::N
|
|
42
|
+
#
|
|
43
|
+
# p M.parents # => [Object]
|
|
44
|
+
# p M::N.parents # => [M, Object]
|
|
45
|
+
# p X.parents # => [M, Object]
|
|
46
|
+
#
|
|
47
|
+
def parents
|
|
48
|
+
parents = []
|
|
49
|
+
if parent_name
|
|
50
|
+
parts = parent_name.split('::')
|
|
51
|
+
until parts.empty?
|
|
52
|
+
parents << (parts * '::').constantize
|
|
53
|
+
parts.pop
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
parents << Object unless parents.include? Object
|
|
57
|
+
parents
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
if RUBY_VERSION < '1.9'
|
|
61
|
+
# Returns the constants that have been defined locally by this object and
|
|
62
|
+
# not in an ancestor. This method is exact if running under Ruby 1.9. In
|
|
63
|
+
# previous versions it may miss some constants if their definition in some
|
|
64
|
+
# ancestor is identical to their definition in the receiver.
|
|
65
|
+
def local_constants
|
|
66
|
+
inherited = {}
|
|
67
|
+
|
|
68
|
+
ancestors.each do |anc|
|
|
69
|
+
next if anc == self
|
|
70
|
+
anc.constants.each { |const| inherited[const] = anc.const_get(const) }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
constants.select do |const|
|
|
74
|
+
!inherited.key?(const) || inherited[const].object_id != const_get(const).object_id
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
else
|
|
78
|
+
def local_constants #:nodoc:
|
|
79
|
+
constants(false)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Returns the names of the constants defined locally rather than the
|
|
84
|
+
# constants themselves. See <tt>local_constants</tt>.
|
|
85
|
+
def local_constant_names
|
|
86
|
+
local_constants.map { |c| c.to_s }
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
class Module
|
|
2
|
+
# Returns String#underscore applied to the module name minus trailing classes.
|
|
3
|
+
#
|
|
4
|
+
# ActiveRecord.as_load_path # => "active_record"
|
|
5
|
+
# ActiveRecord::Associations.as_load_path # => "active_record/associations"
|
|
6
|
+
# ActiveRecord::Base.as_load_path # => "active_record" (Base is a class)
|
|
7
|
+
#
|
|
8
|
+
# The Kernel module gives an empty string by definition.
|
|
9
|
+
#
|
|
10
|
+
# Kernel.as_load_path # => ""
|
|
11
|
+
# Math.as_load_path # => "math"
|
|
12
|
+
def as_load_path
|
|
13
|
+
if self == Object || self == Kernel
|
|
14
|
+
''
|
|
15
|
+
elsif is_a? Class
|
|
16
|
+
parent == self ? '' : parent.as_load_path
|
|
17
|
+
else
|
|
18
|
+
name.split('::').collect do |word|
|
|
19
|
+
word.underscore
|
|
20
|
+
end * '/'
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module PassiveSupport
|
|
2
|
+
class ModelName < String
|
|
3
|
+
attr_reader :singular, :plural, :element, :collection, :partial_path
|
|
4
|
+
alias_method :cache_key, :collection
|
|
5
|
+
|
|
6
|
+
def initialize(name)
|
|
7
|
+
super
|
|
8
|
+
@singular = PassiveSupport::Inflector.underscore(self).tr('/', '_').freeze
|
|
9
|
+
@plural = PassiveSupport::Inflector.pluralize(@singular).freeze
|
|
10
|
+
@element = PassiveSupport::Inflector.underscore(PassiveSupport::Inflector.demodulize(self)).freeze
|
|
11
|
+
@collection = PassiveSupport::Inflector.tableize(self).freeze
|
|
12
|
+
@partial_path = "#{@collection}/#{@element}".freeze
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
module CoreExtensions
|
|
17
|
+
module Module
|
|
18
|
+
# Returns an PassiveSupport::ModelName object for module. It can be
|
|
19
|
+
# used to retrieve all kinds of naming-related information.
|
|
20
|
+
def model_name
|
|
21
|
+
@model_name ||= ::PassiveSupport::ModelName.new(name)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
class Module
|
|
2
|
+
# Synchronize access around a method, delegating synchronization to a
|
|
3
|
+
# particular mutex. A mutex (either a Mutex, or any object that responds to
|
|
4
|
+
# #synchronize and yields to a block) must be provided as a final :with option.
|
|
5
|
+
# The :with option should be a symbol or string, and can represent a method,
|
|
6
|
+
# constant, or instance or class variable.
|
|
7
|
+
# Example:
|
|
8
|
+
# class SharedCache
|
|
9
|
+
# @@lock = Mutex.new
|
|
10
|
+
# def expire
|
|
11
|
+
# ...
|
|
12
|
+
# end
|
|
13
|
+
# synchronize :expire, :with => :@@lock
|
|
14
|
+
# end
|
|
15
|
+
def synchronize(*methods)
|
|
16
|
+
options = methods.extract_options!
|
|
17
|
+
unless options.is_a?(Hash) && with = options[:with]
|
|
18
|
+
raise ArgumentError, "Synchronization needs a mutex. Supply an options hash with a :with key as the last argument (e.g. synchronize :hello, :with => :@mutex)."
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
methods.each do |method|
|
|
22
|
+
aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
|
|
23
|
+
|
|
24
|
+
if method_defined?("#{aliased_method}_without_synchronization#{punctuation}")
|
|
25
|
+
raise ArgumentError, "#{method} is already synchronized. Double synchronization is not currently supported."
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
module_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
|
29
|
+
def #{aliased_method}_with_synchronization#{punctuation}(*args, &block) # def expire_with_synchronization(*args, &block)
|
|
30
|
+
#{with}.synchronize do # @@lock.synchronize do
|
|
31
|
+
#{aliased_method}_without_synchronization#{punctuation}(*args, &block) # expire_without_synchronization(*args, &block)
|
|
32
|
+
end # end
|
|
33
|
+
end # end
|
|
34
|
+
EOS
|
|
35
|
+
|
|
36
|
+
alias_method_chain method, :synchronization
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
require 'passive_support/core_ext/numeric/time'
|
|
2
|
+
require 'passive_support/core_ext/numeric/bytes'
|
|
3
|
+
require 'passive_support/core_ext/numeric/conversions'
|
|
4
|
+
|
|
5
|
+
class Numeric #:nodoc:
|
|
6
|
+
include PassiveSupport::CoreExtensions::Numeric::Time
|
|
7
|
+
include PassiveSupport::CoreExtensions::Numeric::Bytes
|
|
8
|
+
include PassiveSupport::CoreExtensions::Numeric::Conversions
|
|
9
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module PassiveSupport #:nodoc:
|
|
2
|
+
module CoreExtensions #:nodoc:
|
|
3
|
+
module Numeric #:nodoc:
|
|
4
|
+
# Enables the use of byte calculations and declarations, like 45.bytes + 2.6.megabytes
|
|
5
|
+
module Bytes
|
|
6
|
+
KILOBYTE = 1024
|
|
7
|
+
MEGABYTE = KILOBYTE * 1024
|
|
8
|
+
GIGABYTE = MEGABYTE * 1024
|
|
9
|
+
TERABYTE = GIGABYTE * 1024
|
|
10
|
+
PETABYTE = TERABYTE * 1024
|
|
11
|
+
EXABYTE = PETABYTE * 1024
|
|
12
|
+
|
|
13
|
+
def bytes
|
|
14
|
+
self
|
|
15
|
+
end
|
|
16
|
+
alias :byte :bytes
|
|
17
|
+
|
|
18
|
+
def kilobytes
|
|
19
|
+
self * KILOBYTE
|
|
20
|
+
end
|
|
21
|
+
alias :kilobyte :kilobytes
|
|
22
|
+
|
|
23
|
+
def megabytes
|
|
24
|
+
self * MEGABYTE
|
|
25
|
+
end
|
|
26
|
+
alias :megabyte :megabytes
|
|
27
|
+
|
|
28
|
+
def gigabytes
|
|
29
|
+
self * GIGABYTE
|
|
30
|
+
end
|
|
31
|
+
alias :gigabyte :gigabytes
|
|
32
|
+
|
|
33
|
+
def terabytes
|
|
34
|
+
self * TERABYTE
|
|
35
|
+
end
|
|
36
|
+
alias :terabyte :terabytes
|
|
37
|
+
|
|
38
|
+
def petabytes
|
|
39
|
+
self * PETABYTE
|
|
40
|
+
end
|
|
41
|
+
alias :petabyte :petabytes
|
|
42
|
+
|
|
43
|
+
def exabytes
|
|
44
|
+
self * EXABYTE
|
|
45
|
+
end
|
|
46
|
+
alias :exabyte :exabytes
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|