strong_concerns 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +77 -0
- data/Rakefile +1 -0
- data/lib/strong_concerns/version.rb +3 -0
- data/lib/strong_concerns.rb +31 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/strong_concerns_spec.rb +57 -0
- data/strong_concerns.gemspec +23 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 75a98bd71d16b48974db53e3bc230af15cc81d33
|
4
|
+
data.tar.gz: b1522672e6b6f715183a2aa73a277b154a53b5ff
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 79164fca65f96476f98464011eded01d4ef61d325b9d3e45a1cec575045352d6266075d8f6acb328c52505b74d2715316750ea4a36bbf61adbf7e42a78310833
|
7
|
+
data.tar.gz: 1936e60103b2b2f62aadef95252547bfb7f91deda9a583fffb34191f3c720f01b77c30e43365bc2ef77d92784158897240fc7dbfd4469e94bcad8ff9732b7c02
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 niquola
|
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,77 @@
|
|
1
|
+
# StrongConcerns
|
2
|
+
|
3
|
+
Concerns is powerfull method of object decomposition:
|
4
|
+
|
5
|
+
http://37signals.com/svn/posts/3372-put-chubby-models-on-a-diet-with-concerns
|
6
|
+
|
7
|
+
But this pattern is easilly can be abused:
|
8
|
+
when you just spliting object behavior physicaly, not logicaly, you've got messy code.
|
9
|
+
|
10
|
+
The core point is tracking the internal interface between an object and a concern!
|
11
|
+
Minimal & explicit interface forward you to craft more clean and reusable concerns.
|
12
|
+
The ideal concern is ruby's Enumerable, which require only one method to be implemented in hosting class.
|
13
|
+
|
14
|
+
Gem **strong_concerns** is technically helping you to create concerns in a right way.
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
gem 'strong_concerns'
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install strong_concerns
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
``` ruby
|
33
|
+
|
34
|
+
module AgeAssertions
|
35
|
+
def young?
|
36
|
+
age < options[:young]
|
37
|
+
end
|
38
|
+
|
39
|
+
def reproductive?
|
40
|
+
(options[:young]..options[:old]).include?(age)
|
41
|
+
end
|
42
|
+
|
43
|
+
def old?
|
44
|
+
age > options[:old]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class Person
|
49
|
+
|
50
|
+
attr :age
|
51
|
+
|
52
|
+
def initialize(age)
|
53
|
+
@age = age
|
54
|
+
end
|
55
|
+
|
56
|
+
extend StrongConcerns
|
57
|
+
|
58
|
+
concern AgeAssertions,
|
59
|
+
require_methods: %w[age],
|
60
|
+
exports_methods: %w[young? reproductive?],
|
61
|
+
old: 70,
|
62
|
+
young: 14
|
63
|
+
end
|
64
|
+
|
65
|
+
nicola = Person.new('nicola', 'rhyzhikov', 33)
|
66
|
+
if nicola.reproductive?
|
67
|
+
puts "Cool, make me a child!"
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
## Contributing
|
72
|
+
|
73
|
+
1. Fork it
|
74
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
75
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
76
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
77
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require "strong_concerns/version"
|
3
|
+
|
4
|
+
module StrongConcerns
|
5
|
+
class Intermidiate
|
6
|
+
extend Forwardable
|
7
|
+
attr_accessor :options
|
8
|
+
|
9
|
+
def initialize(subject, options)
|
10
|
+
@__subject = subject
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
|
14
|
+
def inspect
|
15
|
+
"Intermidiate<#{self.methods}>"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def concern(mod, options)
|
20
|
+
intermidiate_class = Class.new(Intermidiate)
|
21
|
+
intermidiate_class.send(:include, mod)
|
22
|
+
meths = options.fetch(:require_methods)
|
23
|
+
intermidiate_class.def_delegators :@__subject, *meths
|
24
|
+
|
25
|
+
options.fetch(:exports_methods).each do |meth|
|
26
|
+
define_method meth do |*args, &block|
|
27
|
+
((@__strong_concerns ||= {})[mod.to_s] ||= intermidiate_class.new(self, options)).send(meth,*args, &block)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'strong_concerns'
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe StrongConcerns do
|
4
|
+
module FullNamed
|
5
|
+
def full_name
|
6
|
+
"#{first_name} #{last_name}"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module AgeAssertions
|
11
|
+
def young?
|
12
|
+
age < options[:young]
|
13
|
+
end
|
14
|
+
|
15
|
+
def reproductive?
|
16
|
+
(options[:young]..options[:old]).include?(age)
|
17
|
+
end
|
18
|
+
|
19
|
+
def old?
|
20
|
+
age > options[:old]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Person
|
25
|
+
|
26
|
+
attr :first_name
|
27
|
+
attr :last_name
|
28
|
+
attr :age
|
29
|
+
|
30
|
+
def initialize(first_name, last_name, age)
|
31
|
+
@first_name = first_name
|
32
|
+
@last_name = last_name
|
33
|
+
@age = age
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
extend StrongConcerns
|
38
|
+
|
39
|
+
concern AgeAssertions,
|
40
|
+
require_methods: %w[age],
|
41
|
+
exports_methods: %w[young? reproductive?],
|
42
|
+
young: 14, old: 70
|
43
|
+
|
44
|
+
concern FullNamed,
|
45
|
+
require_methods: %w[first_name last_name],
|
46
|
+
exports_methods: %w[full_name]
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
example do
|
51
|
+
Person.new('nicola', 'rhyzhikov', 33).tap do |nicola|
|
52
|
+
nicola.full_name.should == "nicola rhyzhikov"
|
53
|
+
nicola.should_not be_young
|
54
|
+
nicola.should be_reproductive
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -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 'strong_concerns/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "strong_concerns"
|
8
|
+
spec.version = StrongConcerns::VERSION
|
9
|
+
spec.authors = ["niquola"]
|
10
|
+
spec.email = ["niquola@gmail.com"]
|
11
|
+
spec.description = %q{Gem **strong_concerns** is technically helping you to create concerns in a right way, minimizing and making explicit interface between object and concern.}
|
12
|
+
spec.summary = %q{Gem **strong_concerns** is technically helping you to create concerns in a right way, minimizing and making explicit interface between object and concern.}
|
13
|
+
spec.homepage = ""
|
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
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: strong_concerns
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- niquola
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-11 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: Gem **strong_concerns** is technically helping you to create concerns
|
42
|
+
in a right way, minimizing and making explicit interface between object and concern.
|
43
|
+
email:
|
44
|
+
- niquola@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/strong_concerns.rb
|
55
|
+
- lib/strong_concerns/version.rb
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
- spec/strong_concerns_spec.rb
|
58
|
+
- strong_concerns.gemspec
|
59
|
+
homepage: ''
|
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.3
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Gem **strong_concerns** is technically helping you to create concerns in
|
83
|
+
a right way, minimizing and making explicit interface between object and concern.
|
84
|
+
test_files:
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
- spec/strong_concerns_spec.rb
|