lockistics 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -138,6 +138,34 @@ It works exactly like the above, but the method is `meterlock`.
138
138
  end
139
139
  ```
140
140
 
141
+ #### Wrapping instance methods of a class
142
+
143
+ This is still experimental and I'm not quite happy with the implementation.
144
+
145
+ ```ruby
146
+ class SomeClass
147
+ include Lockistics::Meterable
148
+ meter :some_instance_method
149
+ # or:
150
+ meter :all, :except => :not_this_method
151
+
152
+ def some_instance_method
153
+ do_something
154
+ end
155
+
156
+ def not_this_method
157
+ do_something
158
+ end
159
+ end
160
+ ```
161
+
162
+ Now each call to `some_instance_method` should be wrapped inside a meter
163
+ block and the key name is "someclass_some_instance_method".
164
+
165
+ The include and meter commands should be placed above any method
166
+ definitions in the file. Prettier implementation would be appreciated,
167
+ preferably one that would work with class methods also.
168
+
141
169
  #### Getting the statistics out
142
170
 
143
171
  You can query statistics for locking/metering keys.
@@ -0,0 +1,54 @@
1
+ module Lockistics
2
+ module Meterable
3
+ def self.included(where)
4
+ where.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ @@_metered_methods = {}
9
+ @@_meter_all = false
10
+
11
+ def meter_wrap(meth_name)
12
+ @@_metered_methods[meth_name][:method] = instance_method(meth_name)
13
+ define_method(meth_name) do |*args, &block|
14
+ Lockistics.meter("#{@@_metered_methods[meth_name][:options][:prefix]}_#{meth_name}") do
15
+ @@_metered_methods[meth_name][:method].bind(self).call *args, &block
16
+ end
17
+ end
18
+ end
19
+
20
+ def meter(*args)
21
+ if args.last.kind_of?(Hash)
22
+ options = args.pop
23
+ else
24
+ options = {}
25
+ end
26
+ options = {
27
+ :prefix => self.name.downcase
28
+ }.merge(options)
29
+
30
+ if args.first.eql?(:all)
31
+ @@_meter_all = true
32
+ @@_meter_all_options = {:except => []}.merge(options)
33
+ end
34
+
35
+ Array(args).each do |meth_name|
36
+ @@_metered_methods[meth_name] = {}
37
+ @@_metered_methods[meth_name][:options] = options
38
+ end
39
+ end
40
+
41
+ def method_added(meth_name)
42
+ super
43
+ if @@_metered_methods && @@_metered_methods[meth_name] && @@_metered_methods[meth_name][:method].nil?
44
+ meter_wrap meth_name
45
+ elsif @@_meter_all && !Array(@@_meter_all_options[:except]).include?(meth_name) && @@_metered_methods[meth_name].nil?
46
+ @@_metered_methods[meth_name] = {}
47
+ @@_metered_methods[meth_name][:options] = @@_meter_all_options
48
+ meter_wrap meth_name
49
+ end
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -1,3 +1,3 @@
1
1
  module Lockistics
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/lockistics.rb CHANGED
@@ -3,6 +3,7 @@ require "lockistics/configuration"
3
3
  require "lockistics/lock"
4
4
  require "lockistics/meter"
5
5
  require "lockistics/statistics"
6
+ require "lockistics/meterable"
6
7
 
7
8
  # Lockistics is basically a distributed mutex on Redis.
8
9
  #
@@ -19,6 +20,19 @@ require "lockistics/statistics"
19
20
  # - Minimum and maximum duration
20
21
  # - Minimum and maximum memory growth (using OS gem)
21
22
  # - Arbitary metrics you add during execution
23
+ #
24
+ # Theres also an experimental module you can use to wrap any instance
25
+ # methods inside a meter block.
26
+ #
27
+ # @example
28
+ # class SomeClass
29
+ # include Lockistics::Meterable
30
+ # meter :instance_method_name
31
+ #
32
+ # def instance_method_name
33
+ # do_something
34
+ # end
35
+ # end
22
36
  module Lockistics
23
37
 
24
38
  # Configure the gem
@@ -0,0 +1,43 @@
1
+ require File.expand_path('../spec_helper.rb', __FILE__)
2
+
3
+ class MeterableTest
4
+
5
+ include Lockistics::Meterable
6
+
7
+ meter :all, :except => [
8
+ :do_not_meter_this_instance_method,
9
+ :meter_this_instance_method_too
10
+ ]
11
+ meter :meter_this_instance_method_too
12
+
13
+ def self.meter_this_class
14
+ "ok_class"
15
+ end
16
+
17
+ def meter_this_instance_method
18
+ "ok"
19
+ end
20
+
21
+ def meter_this_instance_method_too
22
+ "ok"
23
+ end
24
+
25
+ def do_not_meter_this_instance_method
26
+ "ok"
27
+ end
28
+
29
+ end
30
+
31
+ describe "Lockistics.meterable" do
32
+ it "should wrap desired methods into meters" do
33
+ MeterableTest.new.meter_this_instance_method.should eq "ok"
34
+ last_total = Lockistics.redis.hgetall("lockistics.meterabletest_meter_this_instance_method.total")
35
+ last_total.should_not be_empty
36
+ MeterableTest.new.meter_this_instance_method_too.should eq "ok"
37
+ last_total = Lockistics.redis.hgetall("lockistics.meterabletest_meter_this_instance_method_too.total")
38
+ last_total.should_not be_empty
39
+ MeterableTest.new.do_not_meter_this_instance_method.should eq "ok"
40
+ last_total = Lockistics.redis.hgetall("lockistics.meterabletest_do_not_meter_this_instance_method.total")
41
+ last_total.should be_empty
42
+ end
43
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lockistics
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kimmo Lehto
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2014-05-20 00:00:00 Z
18
+ date: 2014-06-03 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  version_requirements: &id001 !ruby/object:Gem::Requirement
@@ -122,11 +122,13 @@ files:
122
122
  - lib/lockistics/configuration.rb
123
123
  - lib/lockistics/lock.rb
124
124
  - lib/lockistics/meter.rb
125
+ - lib/lockistics/meterable.rb
125
126
  - lib/lockistics/statistics.rb
126
127
  - lib/lockistics/version.rb
127
128
  - lockistics.gemspec
128
129
  - spec/locking_spec.rb
129
130
  - spec/meter_spec.rb
131
+ - spec/meterable_spec.rb
130
132
  - spec/spec_helper.rb
131
133
  - spec/statistics_spec.rb
132
134
  homepage: https://github.com/kke/lockistics
@@ -165,5 +167,6 @@ summary: With lockistics you can use Redis to create distributed locks and colle
165
167
  test_files:
166
168
  - spec/locking_spec.rb
167
169
  - spec/meter_spec.rb
170
+ - spec/meterable_spec.rb
168
171
  - spec/spec_helper.rb
169
172
  - spec/statistics_spec.rb