surround 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+
2
+ 0.0.1 / 2012-02-14
3
+ ==================
4
+
5
+ * 1st.
@@ -0,0 +1,9 @@
1
+ History.md
2
+ Manifest
3
+ Rakefile
4
+ Readme.md
5
+ lib/surround.rb
6
+ lib/surround/base.rb
7
+ spec/spec_helper.rb
8
+ spec/surround/base_spec.rb
9
+ spec/surround_spec.rb
@@ -0,0 +1,21 @@
1
+ #--
2
+ # Surround.
3
+ #
4
+ # Veselin Todorov <hi@vesln.com>
5
+ # MIT License.
6
+ #++
7
+
8
+ require 'rspec/core/rake_task'
9
+ require 'rake'
10
+ require 'echoe'
11
+
12
+ Echoe.new('surround', '0.0.1') do |p|
13
+ p.description = "Surround a method, save a polar bear."
14
+ p.url = "http://github.com/vesln/surround.rb"
15
+ p.author = "Veselin Todorov"
16
+ p.email = "hi@vesln.com"
17
+ p.ignore_pattern = ["tmp/*"]
18
+ p.development_dependencies = ['rspec', 'rake']
19
+ end
20
+
21
+ RSpec::Core::RakeTask.new('spec')
@@ -0,0 +1,66 @@
1
+ # Surround.rb
2
+
3
+ Surround.rb port of my surround module for Node.js. The gem provides an easy way to surround a method with before and after functionality.
4
+
5
+ ## Synopsis
6
+
7
+ ```ruby
8
+
9
+ class SuperHero
10
+ include Surround::Base
11
+
12
+ def magic(param)
13
+ # Do magic here.
14
+ end
15
+ end
16
+
17
+ SuperHero.surround :magic, :before do |param|
18
+ # Win!
19
+ end
20
+
21
+ hero = SuperHero.new
22
+ hero.magic('foo')
23
+
24
+ SuperHero.restore(:magic) # Clears added surrounders
25
+
26
+ SuperHero.surrounded?(:magic) # Is it surrounded?
27
+
28
+ ```
29
+
30
+ ## Install
31
+
32
+ Gemfile:
33
+
34
+ ```ruby
35
+ gem 'surround'
36
+ ```
37
+
38
+ or
39
+
40
+ ```
41
+ gem install surround
42
+ ````
43
+
44
+ ## License
45
+
46
+ MIT License
47
+
48
+ Copyright (C) 2012 Veselin Todorov
49
+
50
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
51
+ this software and associated documentation files (the "Software"), to deal in
52
+ the Software without restriction, including without limitation the rights to
53
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
54
+ of the Software, and to permit persons to whom the Software is furnished to do
55
+ so, subject to the following conditions:
56
+
57
+ The above copyright notice and this permission notice shall be included in all
58
+ copies or substantial portions of the Software.
59
+
60
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
61
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
62
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
63
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
64
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
65
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
66
+ SOFTWARE.
@@ -0,0 +1,12 @@
1
+ #--
2
+ # Surround.
3
+ #
4
+ # Veselin Todorov <hi@vesln.com>
5
+ # MIT License.
6
+ #++
7
+
8
+ module Surround
9
+ VERSION = '0.0.1'
10
+
11
+ autoload :Base, 'surround/base'
12
+ end
@@ -0,0 +1,53 @@
1
+ #--
2
+ # Surround.
3
+ #
4
+ # Veselin Todorov <hi@vesln.com>
5
+ # MIT License.
6
+ #++
7
+
8
+ module Surround
9
+ module Base
10
+ def self.included(base)
11
+ base.extend self
12
+ end
13
+
14
+ def surround(method, position, &block)
15
+ surround_method(method, position, &block) unless surrounded?(method)
16
+ register_surrounder(method, position, &block)
17
+ end
18
+
19
+ def surrounders(method)
20
+ @__surround[method] if @__surround.has_key?(method)
21
+ end
22
+
23
+ def surrounded?(method)
24
+ !@__surround.nil? && @__surround.has_key?(method)
25
+ end
26
+
27
+ def restore(method)
28
+ return false unless (surrounded? method)
29
+ @__surround.delete(method)
30
+ alias_method method, "original_#{method}"
31
+ end
32
+
33
+ private
34
+
35
+ def register_surrounder(method, position, &block)
36
+ @__surround[method][position].push block
37
+ end
38
+
39
+ def surround_method(method, position, &block)
40
+ @__surround = {} if @__surround.nil?
41
+ @__surround[method] = {:before => [], :after => []}
42
+
43
+ alias_method "original_#{method}", method unless self.respond_to?("original_#{method}")
44
+
45
+ define_method method do |*args|
46
+ surrounders = self.class.surrounders(method)
47
+ surrounders[:before].each { |b| b.call(*args) }
48
+ self.send("original_#{method}", *args)
49
+ surrounders[:after].each { |b| b.call(*args) }
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,12 @@
1
+ #--
2
+ # Surround.
3
+ #
4
+ # Veselin Todorov <hi@vesln.com>
5
+ # MIT License.
6
+ #++
7
+
8
+ require 'surround'
9
+
10
+ RSpec.configure do |config|
11
+ config.color_enabled = true
12
+ end
@@ -0,0 +1,91 @@
1
+ #--
2
+ # Surround.
3
+ #
4
+ # Veselin Todorov <hi@vesln.com>
5
+ # MIT License.
6
+ #++
7
+
8
+ require 'spec_helper'
9
+
10
+ class Dummy
11
+ include Surround::Base
12
+ attr_accessor :called
13
+ attr_accessor :result
14
+
15
+ def foo
16
+ @called = true
17
+ end
18
+
19
+ def bar(foo)
20
+ @result = foo
21
+ end
22
+ end
23
+
24
+ describe 'Surround::Base' do
25
+ before(:each) do
26
+ @dummy = Dummy.new
27
+ end
28
+
29
+ after(:each) do
30
+ Dummy.restore(:foo)
31
+ Dummy.restore(:bar)
32
+ end
33
+
34
+ describe 'surround' do
35
+ context 'before' do
36
+ it 'should add and call before surrounder' do
37
+ once = false
38
+ twice = false
39
+ Dummy.surround :foo, :before do
40
+ once = true
41
+ end
42
+ Dummy.surround :foo, :before do
43
+ twice = true
44
+ end
45
+ @dummy.foo
46
+ once.should === true
47
+ twice.should === true
48
+ @dummy.called.should === true
49
+ end
50
+ end
51
+ context 'after' do
52
+ it 'should add and call after surrounder' do
53
+ called = false
54
+ Dummy.surround :foo, :after do
55
+ called = true
56
+ end
57
+ @dummy.foo
58
+ called.should === true
59
+ @dummy.called.should === true
60
+ end
61
+ end
62
+
63
+ it 'should pass arguments to blocks and to the original method' do
64
+ result = nil
65
+ Dummy.surround :bar, :before do |r|
66
+ result = r
67
+ end
68
+ @dummy.bar('foo')
69
+ result.should === 'foo'
70
+ @dummy.result.should === 'foo'
71
+ end
72
+ end
73
+
74
+ describe 'surrounded?' do
75
+ it 'should the sorrund state for a method' do
76
+ Dummy.surrounded?(:foo).should === false
77
+ Dummy.surround :foo, :after do
78
+ end
79
+ Dummy.surrounded?(:foo).should === true
80
+ end
81
+ end
82
+
83
+ describe 'restore' do
84
+ it 'should restore surrounded method' do
85
+ Dummy.surround :foo, :after do
86
+ end
87
+ Dummy.restore(:foo)
88
+ Dummy.surrounded?(:foo).should === false
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Surround do
4
+ it 'should have sane version' do
5
+ Surround::VERSION.should match /\d\.\d.\d/
6
+ end
7
+ end
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "surround"
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Veselin Todorov"]
9
+ s.date = "2012-02-14"
10
+ s.description = "Surround a method, save a polar bear."
11
+ s.email = "hi@vesln.com"
12
+ s.extra_rdoc_files = ["lib/surround.rb", "lib/surround/base.rb"]
13
+ s.files = ["History.md", "Manifest", "Rakefile", "Readme.md", "lib/surround.rb", "lib/surround/base.rb", "spec/spec_helper.rb", "spec/surround/base_spec.rb", "spec/surround_spec.rb", "surround.gemspec"]
14
+ s.homepage = "http://github.com/vesln/surround.rb"
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Surround", "--main", "Readme.md"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = "surround"
18
+ s.rubygems_version = "1.8.15"
19
+ s.summary = "Surround a method, save a polar bear."
20
+
21
+ if s.respond_to? :specification_version then
22
+ s.specification_version = 3
23
+
24
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
+ s.add_development_dependency(%q<rspec>, [">= 0"])
26
+ s.add_development_dependency(%q<rake>, [">= 0"])
27
+ else
28
+ s.add_dependency(%q<rspec>, [">= 0"])
29
+ s.add_dependency(%q<rake>, [">= 0"])
30
+ end
31
+ else
32
+ s.add_dependency(%q<rspec>, [">= 0"])
33
+ s.add_dependency(%q<rake>, [">= 0"])
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: surround
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Veselin Todorov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-14 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70335963005800 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70335963005800
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70335963005340 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70335963005340
36
+ description: Surround a method, save a polar bear.
37
+ email: hi@vesln.com
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files:
41
+ - lib/surround.rb
42
+ - lib/surround/base.rb
43
+ files:
44
+ - History.md
45
+ - Manifest
46
+ - Rakefile
47
+ - Readme.md
48
+ - lib/surround.rb
49
+ - lib/surround/base.rb
50
+ - spec/spec_helper.rb
51
+ - spec/surround/base_spec.rb
52
+ - spec/surround_spec.rb
53
+ - surround.gemspec
54
+ homepage: http://github.com/vesln/surround.rb
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --line-numbers
59
+ - --inline-source
60
+ - --title
61
+ - Surround
62
+ - --main
63
+ - Readme.md
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '1.2'
78
+ requirements: []
79
+ rubyforge_project: surround
80
+ rubygems_version: 1.8.15
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Surround a method, save a polar bear.
84
+ test_files: []