instrumentable 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in instrumentable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 David Tomberlin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # Instrumentable
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'instrumentable'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install instrumentable
18
+
19
+ ## Usage
20
+ ```ruby
21
+ require "instrumentable"
22
+
23
+ class WidgetRenderer
24
+ include Instrumentable
25
+
26
+ attr_reader :id
27
+ attr_accessor :name
28
+
29
+ def render
30
+ # do crazy render here
31
+ end
32
+
33
+ instrument_for :render, 'load.widget', :widget_id => :id, :widget_name => :name
34
+ end
35
+ ```
36
+
37
+ ## Requirements
38
+ * ActiveSupport
39
+
40
+ ## Running Tests
41
+
42
+ ruby -Itest test/instrumentable_test.rb
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ t.test_files = Dir['test/**/*_test.rb']
8
+ t.verbose = true
9
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'instrumentable/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "instrumentable"
8
+ gem.version = Instrumentable::VERSION
9
+ gem.authors = ["David Tomberlin"]
10
+ gem.email = ["siyegen@gmail.com"]
11
+ gem.description = %q{Gem for decorating methods to use with ActiveSupport::Notifications}
12
+ gem.summary = %q{Gem for decorating methods to use with ActiveSupport::Notifications}
13
+ gem.homepage = ""
14
+
15
+ gem.add_dependency 'activesupport', "~> 3.2"
16
+ gem.add_development_dependency 'minitest'
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,39 @@
1
+ require "securerandom"
2
+ require "active_support/concern"
3
+ require "active_support/notifications"
4
+ require "instrumentable/version"
5
+
6
+ # Includes +instrument_for+ into the class. The class uses it by adding
7
+ # the instrument_for method to the end of the class specifying
8
+ # what method to apply it to.
9
+ module Instrumentable
10
+ extend ActiveSupport::Concern
11
+
12
+ module ClassMethods
13
+ # Internal: Decorates :method_to_instrument with AS::N.instrument
14
+ # firing with :event_name to the matching AS::N.subscribe
15
+ #
16
+ # Example:
17
+ #
18
+ # # Decorates render method with AS:N:instrument 'model.render' and passes
19
+ # # a payload of :model_name and :id to the subscribe method
20
+ # instrument_for :render, 'model.render', {:model_name => :model_name, :id => :id
21
+ def instrument_for(method_to_instrument, event_name, payload={})
22
+ instrument_method = :"instrument_for_#{method_to_instrument}"
23
+
24
+ # Hide original method under new method
25
+ alias_method instrument_method, method_to_instrument
26
+
27
+ # Redefine method_to_instrument to call inside the Notification
28
+ define_method(method_to_instrument) do |*args, &block|
29
+ callable_payload = payload.inject({}) do |result, element|
30
+ value = (__send__(element.last) if respond_to?(element.last)) || element.last
31
+ result.tap { |r| r[element.first] = value }
32
+ end
33
+ ActiveSupport::Notifications.instrument event_name, callable_payload do
34
+ __send__(instrument_method, *args, &block)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module Instrumentable
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,40 @@
1
+ require "minitest_helper"
2
+ require_relative "../lib/instrumentable"
3
+
4
+ class FakeModel
5
+ include Instrumentable
6
+ def simple_event; end
7
+ def payload_event; end
8
+ instrument_for :simple_event, 'event.name', :my_payload => :id
9
+ instrument_for :payload_event, 'payload.name', :my_payload => 'megaman'
10
+ end
11
+
12
+ describe Instrumentable do
13
+
14
+ it "must instrument simple_event" do
15
+ fm = FakeModel.new
16
+ def fm.id; 1; end
17
+ expected = ['event.name-1']
18
+ events = []
19
+
20
+ callback = lambda { |*_| events << "#{_.first}-#{_.last[:my_payload]}" }
21
+ ActiveSupport::Notifications.subscribed(callback, 'event.name') do
22
+ fm.simple_event
23
+ ActiveSupport::Notifications.instrument('other.event')
24
+ end
25
+ events.must_equal expected
26
+ end
27
+
28
+ it "must handle non-callable payloads" do
29
+ fm = FakeModel.new
30
+ expected = ['payload.name-megaman']
31
+ events = []
32
+
33
+ callback = lambda { |*_| events << "#{_.first}-#{_.last[:my_payload]}" }
34
+ ActiveSupport::Notifications.subscribed(callback, 'payload.name') do
35
+ fm.payload_event
36
+ ActiveSupport::Notifications.instrument('other.event')
37
+ end
38
+ events.must_equal expected
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ $:.push File.expand_path("../../lib", __FILE__)
2
+
3
+ require 'instrumentable'
4
+ require 'minitest/autorun'
5
+ require 'minitest/pride'
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: instrumentable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Tomberlin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: minitest
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Gem for decorating methods to use with ActiveSupport::Notifications
47
+ email:
48
+ - siyegen@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - instrumentable.gemspec
59
+ - lib/instrumentable.rb
60
+ - lib/instrumentable/version.rb
61
+ - test/instrumentable_test.rb
62
+ - test/minitest_helper.rb
63
+ homepage: ''
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.23
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Gem for decorating methods to use with ActiveSupport::Notifications
87
+ test_files:
88
+ - test/instrumentable_test.rb
89
+ - test/minitest_helper.rb
90
+ has_rdoc: