mar 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2013, Colin T.A. Gray http://colinta.com
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
+
24
+ The views and conclusions contained in the software and documentation are those
25
+ of the authors and should not be interpreted as representing official policies,
26
+ either expressed or implied, of the Mar Project.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ MAR
2
+ ===
3
+
4
+ Inspired by [this blog post][ruby-decorators], I started looking into what it
5
+ might actually take to implement it. Turns out, it can be done in just one
6
+ method, and it relies exclusively on Ruby Procs.
7
+
8
+ Examble
9
+ =======
10
+
11
+ ```ruby
12
+ require 'mar'
13
+
14
+ def decorator(*options)
15
+ lambda { |*args, &blk|
16
+ blk.call(*args.map{ |arg| "MAR #{arg}!" })
17
+ }
18
+ end
19
+
20
+ class Foo
21
+ _ decorator('foo')
22
+ def foo(one_arg)
23
+ one_arg + ' --from foo, with love'
24
+ end
25
+
26
+ def bar
27
+ 'I get no respect, I tell ya\', no respect.'
28
+ end
29
+ end
30
+
31
+ describe "Mar" do
32
+ before do
33
+ @foo = Foo.new
34
+ end
35
+
36
+ it 'should decorate Foo#foo' do
37
+ @foo.foo('is great').should == 'MAR is great! --from foo, with love'
38
+ end
39
+
40
+ it 'should NOT decorate Foo#bar' do
41
+ @foo.bar.should == 'I get no respect, I tell ya\', no respect.'
42
+ end
43
+ end
44
+ ```
45
+
46
+ [ruby-decorators]: http://colinta.com/thoughts/a_modest_proposal.html
data/lib/mar/mar.rb ADDED
@@ -0,0 +1,33 @@
1
+ class Class
2
+ attr_accessor :mar_decorators
3
+
4
+ def mar_decorators
5
+ @mar_decorators ||= []
6
+ end
7
+
8
+ def clear_mar_decorators!
9
+ @mar_decorators = []
10
+ end
11
+
12
+ def _(decorator)
13
+ mar_decorators << decorator
14
+ end
15
+
16
+ def method_added(name)
17
+ super
18
+ decorators = mar_decorators
19
+ clear_mar_decorators!
20
+ if decorators.length > 0
21
+ new_name = "marrrrrrrr_#{name}"
22
+ alias_method new_name, name
23
+ define_method(name) { |*args|
24
+ decorator = decorators[0]
25
+ here = lambda{ |*decorator_args, &blk|
26
+ self.send(new_name, *decorator_args, &blk)
27
+ }
28
+ decorator.call(*args, &here)
29
+ }
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,3 @@
1
+ module Mar
2
+ Version = '0.1'
3
+ end
data/lib/mar.rb ADDED
@@ -0,0 +1,22 @@
1
+ if defined?(Motion::Project::Config)
2
+ Motion::Project::App.setup do |app|
3
+ # scans app.files until it finds app/ (the default)
4
+ # if found, it inserts just before those files, otherwise it will insert to
5
+ # the end of the list
6
+ insert_point = 0
7
+ app.files.each_index do |index|
8
+ file = app.files[index]
9
+ if file =~ /^(?:\.\/)?app\//
10
+ # found app/, so stop looking
11
+ break
12
+ end
13
+ insert_point = index + 1
14
+ end
15
+
16
+ Dir.glob(File.join(File.dirname(__FILE__), 'mar/**/*.rb')).reverse.each do |file|
17
+ app.files.insert(insert_point, file)
18
+ end
19
+ end
20
+ else
21
+ require 'mar/mar'
22
+ end
data/mar.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/mar/version.rb', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = 'mar'
6
+ gem.version = Mar::Version
7
+
8
+ gem.authors = ['Colin T.A. Gray']
9
+ gem.email = ['colinta@gmail.com']
10
+ gem.summary = %{A way of adding python-style decorators to a Ruby method. And it doesn't completely suck!}
11
+ gem.description = <<-DESC
12
+ == Description
13
+ You can either like python-style decorators, or you can hate them. Frankly, I
14
+ don't care which you choose. Me? I LIKE EM! So here ya go.
15
+ DESC
16
+
17
+ gem.homepage = 'https://github.com/rubymotion/mar'
18
+
19
+ gem.files = `git ls-files`.split($\)
20
+ gem.test_files = gem.files.grep(%r{^spec/})
21
+
22
+ gem.require_paths = ['lib']
23
+ end
data/spec/test_mar.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'mar'
2
+
3
+ def decorator(*options)
4
+ lambda { |*args, &blk|
5
+ blk.call(*args.map{ |arg| "MAR #{arg}!" })
6
+ }
7
+ end
8
+
9
+ class Foo
10
+ _ decorator('foo')
11
+ def foo(one_arg)
12
+ one_arg + ' --from foo, with love'
13
+ end
14
+
15
+ def bar
16
+ 'I get no respect, I tell ya\', no respect.'
17
+ end
18
+ end
19
+
20
+ describe "Mar" do
21
+ before do
22
+ @foo = Foo.new
23
+ end
24
+
25
+ it 'should decorate Foo#foo' do
26
+ @foo.foo('is great').should == 'MAR is great! --from foo, with love'
27
+ end
28
+
29
+ it 'should NOT decorate Foo#bar' do
30
+ @foo.bar.should == 'I get no respect, I tell ya\', no respect.'
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mar
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Colin T.A. Gray
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-09 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! '== Description
15
+
16
+ You can either like python-style decorators, or you can hate them. Frankly, I
17
+
18
+ don''t care which you choose. Me? I LIKE EM! So here ya go.
19
+
20
+ '
21
+ email:
22
+ - colinta@gmail.com
23
+ executables: []
24
+ extensions: []
25
+ extra_rdoc_files: []
26
+ files:
27
+ - LICENSE
28
+ - README.md
29
+ - lib/mar.rb
30
+ - lib/mar/mar.rb
31
+ - lib/mar/version.rb
32
+ - mar.gemspec
33
+ - spec/test_mar.rb
34
+ homepage: https://github.com/rubymotion/mar
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.24
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: A way of adding python-style decorators to a Ruby method. And it doesn't
58
+ completely suck!
59
+ test_files:
60
+ - spec/test_mar.rb
61
+ has_rdoc: