becoming 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +126 -0
- data/Rakefile +12 -0
- data/becoming.gemspec +23 -0
- data/lib/becoming.rb +23 -0
- data/lib/becoming/version.rb +3 -0
- data/test/becoming_test.rb +22 -0
- data/test/test_helper.rb +2 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5131245d0863ec6e04f6c9cd257c0ff8c4631063
|
4
|
+
data.tar.gz: 983f92b02426dbbf97e2aba60389cb663b6db6c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 932436eef64fe6e05e3afa6ed06f4dc8e5430803c9f34fd4d55cadde6902f439ce67ddbcafa692f6c9104d81e357d72cc76536f1447b7999750293795a93939a
|
7
|
+
data.tar.gz: b27a51aae21642829857ec65f9ac26f058083066cf36c361ac6eaa9a6e76c1b374175f2186fa5a6cc090bd6f29d804c57ac02cd938a29817450b87e60d685ead
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Steve Klabnik
|
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,126 @@
|
|
1
|
+
# Becoming
|
2
|
+
|
3
|
+
Have you ever used delegation libraries, but found them a bit unsatisfactory?
|
4
|
+
Delegation is awesome, but many Ruby libraries love their metaprogramming, and
|
5
|
+
so they expect class names to match up. This often screws up delegation.
|
6
|
+
|
7
|
+
Becoming allows your objects to have 'becomings' that make them have extended
|
8
|
+
functionality. They still have the same class as they did before, but now
|
9
|
+
they're just... different.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'becoming'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install becoming
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
For example, let's imagine you're writing a Rails form:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
# a model that looks like this
|
31
|
+
User = Struct.new(:first_name, :last_name)
|
32
|
+
|
33
|
+
# in the controller
|
34
|
+
@user = User.new("Steve", "Klabnik")
|
35
|
+
|
36
|
+
# in the view
|
37
|
+
<%= form_for @user do |f| %>
|
38
|
+
```
|
39
|
+
|
40
|
+
This does reflection on the class of the `@user`, generating HTML like this:
|
41
|
+
|
42
|
+
```html
|
43
|
+
<form action="/users/1">
|
44
|
+
```
|
45
|
+
|
46
|
+
This is mega awesome. But, let's say that we want to add some new presentation
|
47
|
+
methods on our `User`. So we make a class:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
class FullNameUser
|
51
|
+
def initialize(user)
|
52
|
+
@user = user
|
53
|
+
end
|
54
|
+
|
55
|
+
def full_name
|
56
|
+
"#{@user.first_name} #{@user.last_name}"
|
57
|
+
end
|
58
|
+
|
59
|
+
def method_missing(m, *args, &blk)
|
60
|
+
@user.send(m, *args, &blk)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
We update our controller to use this new object:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
user = User.new("Steve", "Klabnik")
|
69
|
+
@user = FullNameUser.new(user)
|
70
|
+
```
|
71
|
+
|
72
|
+
Now, our form... does the wrong thing:
|
73
|
+
|
74
|
+
```html
|
75
|
+
<form action="/full_name_users/1">
|
76
|
+
```
|
77
|
+
|
78
|
+
Drat! So what do we do?
|
79
|
+
|
80
|
+
Answer: make your object have a becoming:
|
81
|
+
|
82
|
+
```
|
83
|
+
# in the model
|
84
|
+
User = Struct.new(:first_name, :last_name) do
|
85
|
+
include Becoming
|
86
|
+
end
|
87
|
+
|
88
|
+
# your 'decorator'
|
89
|
+
module FullNamed
|
90
|
+
def full_name
|
91
|
+
"#{first_name} #{last_name}"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# in the controller
|
96
|
+
@user = User.new("Steve", "Klabnik")
|
97
|
+
@user.becoming(FullNamed)
|
98
|
+
```
|
99
|
+
|
100
|
+
Now, your form will generate the same HTML as before, and everything is
|
101
|
+
just peachy.
|
102
|
+
|
103
|
+
### How does it work?
|
104
|
+
|
105
|
+
Magic.
|
106
|
+
|
107
|
+
### Is it any good?
|
108
|
+
|
109
|
+
Yes.
|
110
|
+
|
111
|
+
### What's the catch?
|
112
|
+
|
113
|
+
You can only use this with Ruby 2.0, sorry.
|
114
|
+
|
115
|
+
## Contributing
|
116
|
+
|
117
|
+
1. Fork it
|
118
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
119
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
120
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
121
|
+
5. Create new Pull Request
|
122
|
+
|
123
|
+
## Thanks
|
124
|
+
|
125
|
+
Shout-outs go to Avdi Grimm, Gilles Deleuze, and Felix Guattari. Without them,
|
126
|
+
this gem wouldn't exist.
|
data/Rakefile
ADDED
data/becoming.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'becoming/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "becoming"
|
8
|
+
spec.version = Becoming::VERSION
|
9
|
+
spec.authors = ["Steve Klabnik"]
|
10
|
+
spec.email = ["steve@steveklabnik.com"]
|
11
|
+
spec.description = %q{Better delegators for Ruby 2.0. Allow your objects to have 'becomings,' temporarily giving them different functionality.}
|
12
|
+
spec.summary = %q{A better way to do delegation.}
|
13
|
+
spec.homepage = "https://github.com/steveklabnik/becoming"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
data/lib/becoming.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require "becoming/version"
|
2
|
+
|
3
|
+
module Becoming
|
4
|
+
def becoming(mod)
|
5
|
+
@becoming = mod
|
6
|
+
end
|
7
|
+
|
8
|
+
def method_missing(m, *args, &blk)
|
9
|
+
if @becoming && @becoming.public_method_defined?(m)
|
10
|
+
@becoming.instance_method(m).bind(self).call(*args, &blk)
|
11
|
+
else
|
12
|
+
super
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def respond_to_missing?(m, include_all=false)
|
17
|
+
if @becoming && @becoming.public_method_defined?(m)
|
18
|
+
true
|
19
|
+
else
|
20
|
+
super
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
require 'becoming'
|
4
|
+
|
5
|
+
User = Struct.new(:first_name, :last_name) do
|
6
|
+
include Becoming
|
7
|
+
end
|
8
|
+
|
9
|
+
module FullNamed
|
10
|
+
def full_name
|
11
|
+
"#{first_name} #{last_name}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class BecomingTest < MiniTest::Unit::TestCase
|
16
|
+
def test_becomings
|
17
|
+
user = User.new("Steve", "Klabnik")
|
18
|
+
user.becoming(FullNamed)
|
19
|
+
|
20
|
+
assert_equal "Steve Klabnik", user.full_name
|
21
|
+
end
|
22
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: becoming
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve Klabnik
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Better delegators for Ruby 2.0. Allow your objects to have 'becomings,'
|
42
|
+
temporarily giving them different functionality.
|
43
|
+
email:
|
44
|
+
- steve@steveklabnik.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- becoming.gemspec
|
55
|
+
- lib/becoming.rb
|
56
|
+
- lib/becoming/version.rb
|
57
|
+
- test/becoming_test.rb
|
58
|
+
- test/test_helper.rb
|
59
|
+
homepage: https://github.com/steveklabnik/becoming
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.0.0
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: A better way to do delegation.
|
83
|
+
test_files:
|
84
|
+
- test/becoming_test.rb
|
85
|
+
- test/test_helper.rb
|
86
|
+
has_rdoc:
|