fabrik 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 128e5fa1f485e41afa0d92c2c788695b034b91fa
4
+ data.tar.gz: abb9712fdd16527795762b1237bc05078f75a971
5
+ SHA512:
6
+ metadata.gz: 1e92bd02555a20d05f792fed914829ac10a342abafd4878ccaacab0a7141d3b07f35b7aa2c84e9c84837b7fc9340d2852266935cb58369e740aac54750d06e04
7
+ data.tar.gz: de15a22c2c5854a31a132723f36c9e2355c87f4193312b211dff79f6bfde4803c7d297dd425a00b764e04ba23afb488161a9a4afbcb178487f0f61ea07c9452a
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 Joe Corcoran
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,98 @@
1
+ # Fabrik
2
+
3
+ [![Build Status](https://travis-ci.org/joecorcoran/fabrik.svg?branch=master)](https://travis-ci.org/joecorcoran/fabrik)
4
+
5
+ Traits for Ruby 2, as described in [Traits: A Mechanism for
6
+ Fine-grained Reuse][paper] by Ducasse, Nierstrasz, Schärli, Wuyts and Black.
7
+
8
+ ## Usage
9
+
10
+ A class becomes a trait when it extends `Fabrik::Trait`.
11
+
12
+ ```ruby
13
+ class Tiger
14
+ extend Fabrik::Trait
15
+ end
16
+ ```
17
+
18
+ Traits provide methods within a `provides` block.
19
+
20
+ ```ruby
21
+ class Panthera
22
+ extend Fabrik::Trait
23
+
24
+ provides do
25
+ def roar; :roar! end
26
+ end
27
+ end
28
+ ```
29
+
30
+ ```ruby
31
+ class Tiger
32
+ extend Fabrik::Trait
33
+
34
+ provides do
35
+ def mother; :tigress end
36
+ def father; :tiger end
37
+ end
38
+ end
39
+ ```
40
+
41
+ ```ruby
42
+ class Lion
43
+ extend Fabrik::Trait
44
+
45
+ provides do
46
+ def mother; :lioness end
47
+ def father; :lion end
48
+ end
49
+ end
50
+ ```
51
+
52
+ A class uses traits by extending `Fabrik::Composer` and composing the traits.
53
+
54
+ ```ruby
55
+ class Tigon
56
+ extend Fabrik::Composer
57
+ compose Panthera, Tiger, Lion
58
+ end
59
+ ```
60
+
61
+ ```ruby
62
+ tigon = Tigon.new
63
+ tigon.roar # => :roar!
64
+ tigon.mother # => raises ConflictingMethods error
65
+ ```
66
+
67
+ Conflicting methods must be resolved, by exclusion or aliasing during
68
+ composition, or by overriding in the composing class.
69
+
70
+ ```ruby
71
+ class Tigon
72
+ extend Fabrik::Composer
73
+ compose Panthera,
74
+ Tiger.methods(exclude: :mother),
75
+ Lion.methods(exclude: :father)
76
+ end
77
+ ```
78
+
79
+ ```ruby
80
+ tigon = Tigon.new
81
+ tigon.roar # => :roar!
82
+ tigon.mother # => :lioness
83
+ tigon.father # => :tiger
84
+ ```
85
+
86
+ View the specs for further examples, including composable traits and traits
87
+ that provide methods from shared modules.
88
+
89
+ ## Credits
90
+
91
+ Built by [Joe Corcoran][me].
92
+
93
+ ## License
94
+
95
+ MIT.
96
+
97
+ [paper]: http://scg.unibe.ch/archive/papers/Duca06bTOPLASTraits.pdf
98
+ [me]: https://corcoran.io
@@ -0,0 +1,17 @@
1
+ require 'fabrik/resolver'
2
+
3
+ module Fabrik
4
+ module Composer
5
+
6
+ def compose(*method_maps)
7
+ method_maps.map! { |m| Hash === m ? m : m.methods }
8
+ resolved_method_map = Resolver.new(method_maps).resolved_method_map
9
+ resolved_method_map.each do |name, method|
10
+ unless self.instance_methods(false).include?(name)
11
+ self.send(:define_method, name, method)
12
+ end
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,43 @@
1
+ require 'set'
2
+
3
+ module Fabrik
4
+ class Dictionary
5
+
6
+ def initialize
7
+ @pairs = Set.new
8
+ end
9
+
10
+ def add(pair)
11
+ @pairs.add(pair)
12
+ end
13
+
14
+ def method_map(opts = {})
15
+ exclude = Array(opts[:exclude])
16
+ aliases = opts.fetch(:aliases, {})
17
+
18
+ methods = Hash[
19
+ @pairs.map do |pair|
20
+ mod, name = pair
21
+ [name, mod.instance_method(name)]
22
+ end
23
+ ]
24
+ methods = apply_exclusions(methods, exclude)
25
+ methods = apply_aliases(methods, aliases)
26
+ end
27
+
28
+ def apply_exclusions(methods, exclusions)
29
+ methods.select do |name, _|
30
+ !exclusions.include?(name)
31
+ end
32
+ end
33
+
34
+ def apply_aliases(methods, aliases)
35
+ Hash[
36
+ methods.map do |name, _|
37
+ [(aliases.include?(name) ? aliases[name] : name), _]
38
+ end
39
+ ]
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Fabrik
2
+ class ConflictingMethods < NotImplementedError; end
3
+ end
@@ -0,0 +1,36 @@
1
+ require 'fabrik/errors'
2
+
3
+ module Fabrik
4
+ class Resolver
5
+ attr_reader :conflicts
6
+
7
+ def initialize(method_maps)
8
+ @method_maps = method_maps
9
+ @conflicts = {}
10
+ end
11
+
12
+ def resolved_method_map
13
+ @method_maps.reduce({}) do |result, method_map|
14
+ result.merge!(method_map) do |name, m1, m2|
15
+ m1 == m2 ? m1 : conflict_method(name, m1, m2)
16
+ end
17
+ end
18
+ end
19
+
20
+ def conflict_method(name, m1, m2)
21
+ resolver, @conflicts[name] = self, [m1, m2]
22
+ own.send(:define_method, name) do
23
+ m1, m2 = resolver.conflicts[name]
24
+ raise ConflictingMethods.new(
25
+ "#{m1.owner} and #{m2.owner} both provide methods named :#{name}"
26
+ )
27
+ end
28
+ own.instance_method(name)
29
+ end
30
+
31
+ def own
32
+ @own ||= Module.new
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ require 'fabrik/composer'
2
+ require 'fabrik/dictionary'
3
+ require 'forwardable'
4
+
5
+ module Fabrik
6
+ module Trait
7
+ include Composer
8
+
9
+ extend Forwardable
10
+ def_delegators :own, :instance_methods, :send
11
+
12
+ def methods(opts = {})
13
+ provides_from(own, *own.instance_methods)
14
+ dictionary.method_map(opts)
15
+ end
16
+
17
+ def provides(&own_definition)
18
+ own.module_eval(&own_definition)
19
+ end
20
+
21
+ def provides_from(mod, *names)
22
+ names.each do |name|
23
+ dictionary.add([mod, name])
24
+ end
25
+ end
26
+
27
+ def own
28
+ @own ||= Module.new
29
+ end
30
+
31
+ def dictionary
32
+ @dictionary ||= Dictionary.new
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Fabrik
2
+ VERSION = '0.1.0'
3
+ end
data/lib/fabrik.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'fabrik/composer'
2
+ require 'fabrik/trait'
3
+ require 'fabrik/version'
4
+
5
+ module Fabrik; end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fabrik
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joe Corcoran
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ description: |
28
+ Traits for Ruby 2, as described in the paper "Traits: A Mechanism for
29
+ Fine-grained Reuse" by Ducasse, Nierstrasz, Schärli, Wuyts and Black.
30
+ email:
31
+ - joecorcoran@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - LICENSE.txt
37
+ - README.md
38
+ - lib/fabrik.rb
39
+ - lib/fabrik/composer.rb
40
+ - lib/fabrik/dictionary.rb
41
+ - lib/fabrik/errors.rb
42
+ - lib/fabrik/resolver.rb
43
+ - lib/fabrik/trait.rb
44
+ - lib/fabrik/version.rb
45
+ homepage: http://github.com/joecorcoran/fabrik
46
+ licenses:
47
+ - MIT
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.2.2
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Traits for Ruby 2
69
+ test_files: []