eyeballer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Adam Groves
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,7 @@
1
+ couch_loafer===========
2
+ Description goes here.
3
+
4
+ COPYRIGHT
5
+ =========
6
+
7
+ Copyright (c) 2008 Adam Groves. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rake'
2
+ require File.join(File.expand_path(File.dirname(__FILE__)),'lib','eyeballer')
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |s|
7
+ s.name = "eyeballer"
8
+ s.date = "2010-03-18"
9
+ s.summary = %Q{Foo}
10
+ s.email = "adam.groves@gmail.com"
11
+ s.homepage = "http://github.com/addywaddy/eyeballer"
12
+ s.description = "A simple observer library"
13
+ s.has_rdoc = true
14
+ s.authors = ["Adam Groves"]
15
+ s.files = %w( LICENSE README.md Rakefile ) + Dir["{lib,spec}/**/*"]
16
+ s.extra_rdoc_files = %w( README.md LICENSE )
17
+ s.require_path = "lib"
18
+ s.add_dependency("extlib", ">= 0.9.12")
19
+ end
20
+ rescue LoadError
21
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
22
+ end
23
+
24
+ require 'rake/rdoctask'
25
+ Rake::RDocTask.new do |rdoc|
26
+ rdoc.rdoc_dir = 'rdoc'
27
+ rdoc.title = 'eyeballer'
28
+ rdoc.options << '--line-numbers' << '--inline-source'
29
+ rdoc.rdoc_files.include('README*')
30
+ rdoc.rdoc_files.include('lib/**/*.rb')
31
+ end
32
+
33
+ require 'spec/rake/spectask'
34
+ Spec::Rake::SpecTask.new(:spec) do |t|
35
+ t.libs << 'lib' << 'spec'
36
+ t.spec_files = FileList['spec/**/*_spec.rb']
37
+ end
38
+
39
+ Spec::Rake::SpecTask.new(:rcov) do |t|
40
+ t.libs << 'lib' << 'spec'
41
+ t.spec_files = FileList['spec/**/*_spec.rb']
42
+ t.rcov = true
43
+ end
44
+
45
+ task :default => :spec
data/lib/eyeballer.rb ADDED
@@ -0,0 +1,56 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'rubygems'
5
+ require 'extlib'
6
+
7
+ module Eyeballer
8
+ module ClassMethods
9
+ def observe(name, actions = {})
10
+
11
+ observer_klass = self
12
+ class_name = ::Extlib::Inflection.camelize(name)
13
+ klass = ::Extlib::Inflection.constantize(class_name)
14
+
15
+ klass.class_eval do
16
+
17
+ cattr_accessor :eyeballed_methods
18
+ self.eyeballed_methods||= {}
19
+ self.eyeballed_methods.merge!(actions) {|key, oldval, newval| (Array(oldval).flatten << newval).uniq}
20
+
21
+ unless method_defined? :eyeball_executer
22
+ define_method :eyeball_executer do |name|
23
+ observer_klass_instance = observer_klass.new
24
+ Array(self.class.eyeballed_methods[name]).each do |hook|
25
+ if observer_klass_instance.method(hook).arity > 0
26
+ observer_klass_instance.send(hook, self)
27
+ else
28
+ observer_klass_instance.send(hook)
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ self.eyeballed_methods.keys.each do |method_name|
35
+ unless method_defined? :"#{method_name}_without_eyeball"
36
+ alias_method :"#{method_name}_without_eyeball", method_name
37
+ define_method method_name do
38
+ result = self.send("#{method_name}_without_eyeball")
39
+ eyeball_executer(method_name)
40
+ result
41
+ end
42
+ end
43
+ end
44
+
45
+ end
46
+ end
47
+ end
48
+
49
+ module InstanceMethods
50
+ end
51
+
52
+ def self.included(receiver)
53
+ receiver.extend ClassMethods
54
+ receiver.send :include, InstanceMethods
55
+ end
56
+ end
@@ -0,0 +1,70 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ class Foo
4
+ attr_accessor :greeting
5
+ def activity
6
+ "Busy busy ..."
7
+ end
8
+
9
+ def bad_activity
10
+ raise "This ain't gonna work ..."
11
+ end
12
+
13
+ def other_activity
14
+ "Busy again ..."
15
+ end
16
+ end
17
+
18
+ class Job
19
+ def self.number_one;end
20
+ def self.number_two;end
21
+ def self.number_three;end
22
+ end
23
+
24
+ class Observer
25
+ include Eyeballer
26
+
27
+ observe :foo, :activity => [:do_something, :do_something_else]
28
+ observe :foo, :activity => :do_something_else
29
+
30
+ observe :foo, :bad_activity => :do_something_entirely_different
31
+
32
+ observe :foo, :other_activity => :do_something_with_the_instance
33
+
34
+ def do_something
35
+ Job.number_one
36
+ end
37
+
38
+ def do_something_else
39
+ Job.number_two
40
+ end
41
+
42
+ def do_something_entirely_different
43
+ Job.number_three
44
+ end
45
+
46
+ def do_something_with_the_instance(instance)
47
+ instance.greeting = "The Observer says hello too!"
48
+ end
49
+ end
50
+
51
+
52
+ describe Eyeballer do
53
+ it "should do something" do
54
+ Job.should_receive(:number_one)
55
+ Job.should_receive(:number_two)
56
+ Foo.new.activity.should == "Busy busy ..."
57
+ end
58
+
59
+ it "should not do anything if an exception is raised" do
60
+ Job.should_not_receive(:number_three)
61
+ lambda{Foo.new.bad_activity}.should raise_error
62
+ end
63
+
64
+ it "should have access to the instance" do
65
+ foo = Foo.new
66
+ foo.greeting.should be_blank
67
+ foo.other_activity.should == "Busy again ..."
68
+ foo.greeting.should == "The Observer says hello too!"
69
+ end
70
+ end
@@ -0,0 +1,14 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
10
+
11
+ require 'eyeballer'
12
+
13
+ Spec::Runner.configure do |config|
14
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eyeballer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Groves
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-03-18 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: extlib
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.9.12
24
+ version:
25
+ description: A simple observer library
26
+ email: adam.groves@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.md
34
+ files:
35
+ - LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - lib/eyeballer.rb
39
+ - spec/eyeballer/eyeballer_spec.rb
40
+ - spec/spec_helper.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/addywaddy/eyeballer
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.5
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Foo
69
+ test_files:
70
+ - spec/eyeballer/eyeballer_spec.rb
71
+ - spec/spec_helper.rb