traitor 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.travis.yml +3 -0
- data/LICENSE +22 -0
- data/README.md +104 -0
- data/Rakefile +10 -0
- data/lib/traitor.rb +7 -0
- data/lib/traitor/conflict_check.rb +28 -0
- data/lib/traitor/core_ext/class.rb +13 -0
- data/lib/traitor/core_ext/object.rb +15 -0
- data/lib/traitor/trait.rb +36 -0
- data/lib/traitor/version.rb +3 -0
- data/test/traits_test.rb +88 -0
- data/traitor.gemspec +17 -0
- metadata +59 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Josep M. Bach
|
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,104 @@
|
|
1
|
+
# traitor [](http://travis-ci.org/txus/traitor)
|
2
|
+
|
3
|
+
Traitor is a basic implementation of
|
4
|
+
[Traits](http://en.wikipedia.org/wiki/Trait_(computer_programming) (duh) for Ruby 2.0.
|
5
|
+
|
6
|
+
Also, it might be the only library in the world that **needs** refinements. `</troll>`
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
rvm install ruby-2.0.0-preview1
|
13
|
+
gem 'traitor'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install traitor
|
22
|
+
|
23
|
+
## Behavior reusability through Traits
|
24
|
+
|
25
|
+
Traits are like Ruby modules in the sense that they can be used to define
|
26
|
+
composable units of behavior, but they are not included hierarchically. They
|
27
|
+
are truly composable, meaning that are pieces that *must* either fit
|
28
|
+
perfectly or the host object must provide a way for them to do it, normally
|
29
|
+
resolving conflicts by explicitly redefining the conflicting methods.
|
30
|
+
|
31
|
+
Say we have a `Colorable` trait:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
Colorable = Trait.new do
|
35
|
+
attr_accessor :color
|
36
|
+
|
37
|
+
def ==(other)
|
38
|
+
other.color == color
|
39
|
+
end
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
And a `Shapeable` trait:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
Shapeable = Trait.new do
|
47
|
+
attr_accessor :sides
|
48
|
+
|
49
|
+
def ==(other)
|
50
|
+
other.sides == sides
|
51
|
+
end
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
Now we would like an object composed of those two traits:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
class Rectangle
|
59
|
+
uses Shapeable
|
60
|
+
uses Colorable
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
This obviously doesn't work -- if we try to call #== on a `Rectangle`, it
|
65
|
+
doesn't know which implementation should it call. `Colorable` or `Shapeable`?
|
66
|
+
See the error:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
Rectangle.new == Rectangle.new
|
70
|
+
# TraitConflict: Conflicting methods: #==
|
71
|
+
```
|
72
|
+
|
73
|
+
Traits have no hierarchy, so no one prevails over the others. The only way to
|
74
|
+
use both traits is to provide an explicit conflict resolution:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
class Rectangle
|
78
|
+
uses Shapeable
|
79
|
+
uses Colorable
|
80
|
+
|
81
|
+
def ==(other)
|
82
|
+
colorable_equal = trait_send(Colorable, :==, other)
|
83
|
+
shapeable_equal = trait_send(Shapeable, :==, other)
|
84
|
+
colorable_equal && shapeable_equal
|
85
|
+
end
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
Now we can use `#==` safely because we **control** how conflicts are resolved.
|
90
|
+
Note that we have access to either implementation via `trait_send`.
|
91
|
+
|
92
|
+
## Contributing
|
93
|
+
|
94
|
+
1. Fork it
|
95
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
96
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
97
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
98
|
+
5. Create new Pull Request
|
99
|
+
|
100
|
+
## Who's this
|
101
|
+
|
102
|
+
This was made by [Josep M. Bach (Txus)](http://txustice.me) under the MIT
|
103
|
+
license. I'm [@txustice](http://twitter.com/txustice) on twitter (where you
|
104
|
+
should probably follow me!).
|
data/Rakefile
ADDED
data/lib/traitor.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Traitor
|
2
|
+
module ConflictCheck
|
3
|
+
class TraitConflict < StandardError
|
4
|
+
end
|
5
|
+
|
6
|
+
def check(host_class)
|
7
|
+
conflicts = []
|
8
|
+
traits = host_class.traits
|
9
|
+
traits.each do |t|
|
10
|
+
traits.reject { |other| other == t }.each do |other|
|
11
|
+
t.trait_methods.each do |m|
|
12
|
+
conflicts << m if other.implements?(m)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
conflicts.delete_if do |c|
|
17
|
+
host_class.instance_methods(false).include?(c)
|
18
|
+
end
|
19
|
+
return false if conflicts.empty?
|
20
|
+
|
21
|
+
output = "Conflicting methods: #{conflicts.uniq.map {|c| "##{c}"}.join(', ')}"
|
22
|
+
raise TraitConflict, output
|
23
|
+
end
|
24
|
+
module_function :check
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
TraitConflict = Traitor::ConflictCheck::TraitConflict
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Traitor
|
2
|
+
module Object
|
3
|
+
def method_missing(m, *args, &block)
|
4
|
+
Trait.check_conflict(self.class)
|
5
|
+
super unless trait = self.class.traits.detect { |t| t.implements?(m) }
|
6
|
+
trait.call(self, m, *args, &block)
|
7
|
+
end
|
8
|
+
|
9
|
+
def trait_send(trait, m, *args, &block)
|
10
|
+
trait.call(self, m, *args, &block)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
Object.send :include, Traitor::Object
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'traitor/conflict_check'
|
2
|
+
|
3
|
+
module Traitor
|
4
|
+
class Trait
|
5
|
+
def initialize(&block)
|
6
|
+
@module_block = block
|
7
|
+
@container = Module.new(&block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def trait_methods
|
11
|
+
@container.instance_methods(false)
|
12
|
+
end
|
13
|
+
|
14
|
+
def implements?(m)
|
15
|
+
trait_methods.include?(m)
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(host, m, *args, &block)
|
19
|
+
metaclass = class << host; self; end
|
20
|
+
blk = @module_block
|
21
|
+
|
22
|
+
refinement = Module.new do
|
23
|
+
refine metaclass, &blk
|
24
|
+
end
|
25
|
+
|
26
|
+
host.send :using, refinement
|
27
|
+
host.send m, *args, &block
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.check_conflict(host_class)
|
31
|
+
ConflictCheck.check(host_class)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Trait = Traitor::Trait
|
data/test/traits_test.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'traitor'
|
3
|
+
|
4
|
+
Colorable = Trait.new do
|
5
|
+
attr_accessor :color
|
6
|
+
def ==(other)
|
7
|
+
other.color == color
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
Shapeable = Trait.new do
|
12
|
+
attr_accessor :sides
|
13
|
+
|
14
|
+
def side_length; 10; end
|
15
|
+
|
16
|
+
def perimeter
|
17
|
+
sides * side_length
|
18
|
+
end
|
19
|
+
|
20
|
+
def ==(other)
|
21
|
+
other.sides == sides
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class TraitsTest < MiniTest::Unit::TestCase
|
26
|
+
def test_call_a_trait_method
|
27
|
+
klass = Class.new do
|
28
|
+
uses Shapeable
|
29
|
+
end
|
30
|
+
|
31
|
+
foo = klass.new
|
32
|
+
foo.sides = 4
|
33
|
+
assert_equal 4, foo.sides
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_detects_conflicts
|
37
|
+
klass = Class.new do
|
38
|
+
uses Shapeable
|
39
|
+
uses Colorable
|
40
|
+
end
|
41
|
+
|
42
|
+
foo = klass.new
|
43
|
+
assert_raises TraitConflict do
|
44
|
+
foo.color
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_override_conflict_of_trait_method
|
49
|
+
klass = Class.new do
|
50
|
+
uses Colorable
|
51
|
+
uses Shapeable
|
52
|
+
|
53
|
+
def ==(other)
|
54
|
+
colorable_equal = trait_send(Colorable, :==, other)
|
55
|
+
shapeable_equal = trait_send(Shapeable, :==, other)
|
56
|
+
colorable_equal && shapeable_equal
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
square_one = klass.new
|
61
|
+
square_one.sides = 4
|
62
|
+
square_one.color = 0xFFF
|
63
|
+
|
64
|
+
square_two = klass.new
|
65
|
+
square_two.sides = 4
|
66
|
+
square_two.color = 0xFFF
|
67
|
+
|
68
|
+
square_three = klass.new
|
69
|
+
square_three.sides = 3
|
70
|
+
square_three.color = 0xFFF
|
71
|
+
|
72
|
+
assert_equal square_one, square_two
|
73
|
+
refute_equal square_one, square_three
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_call_specific_implementation
|
77
|
+
klass = Class.new do
|
78
|
+
uses Shapeable
|
79
|
+
def double_perimeter
|
80
|
+
trait_send(Shapeable, :perimeter) * 2
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
foo = klass.new
|
85
|
+
foo.sides = 4
|
86
|
+
assert_equal 80, foo.double_perimeter
|
87
|
+
end
|
88
|
+
end
|
data/traitor.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/traitor/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Josep M. Bach"]
|
6
|
+
gem.email = ["josep.m.bach@gmail.com"]
|
7
|
+
gem.description = %q{Traits for Ruby: like mixins, but better}
|
8
|
+
gem.summary = %q{Traits for Ruby: like mixins, but better}
|
9
|
+
gem.homepage = "https://github.com/txus/traitor"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "traitor"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Traitor::VERSION
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: traitor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Josep M. Bach
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-06 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! 'Traits for Ruby: like mixins, but better'
|
15
|
+
email:
|
16
|
+
- josep.m.bach@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .travis.yml
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/traitor.rb
|
27
|
+
- lib/traitor/conflict_check.rb
|
28
|
+
- lib/traitor/core_ext/class.rb
|
29
|
+
- lib/traitor/core_ext/object.rb
|
30
|
+
- lib/traitor/trait.rb
|
31
|
+
- lib/traitor/version.rb
|
32
|
+
- test/traits_test.rb
|
33
|
+
- traitor.gemspec
|
34
|
+
homepage: https://github.com/txus/traitor
|
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: ! 'Traits for Ruby: like mixins, but better'
|
58
|
+
test_files:
|
59
|
+
- test/traits_test.rb
|