strong_concerns 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/README.md +29 -4
- data/lib/strong_concerns/version.rb +1 -1
- data/lib/strong_concerns.rb +28 -9
- data/spec/strong_concerns_spec.rb +41 -3
- data/strong_concerns.gemspec +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26c6d7992012ae2a5204e3643a55de1bfbf98b43
|
4
|
+
data.tar.gz: b3344d68b073077ff1849f31cb34969bee7ca56d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e406c4ff2eadf7c553fed7a263a6a16e0773a34acf2d55185d8afc1458f32287fc72c7e3f53cfc0ef035b0b5cbdf7e2ecf6600f949d325f7cae5187ce264c02
|
7
|
+
data.tar.gz: 6f172432785133c1e6cc7b3b4029aa04c58825dc73efd1e1b60951f506c58e54a1d2a3e3c11cee54a91a8d28c059eaad768d453faac544fcb621c99532de836a
|
data/README.md
CHANGED
@@ -32,6 +32,12 @@ Or install it yourself as:
|
|
32
32
|
``` ruby
|
33
33
|
|
34
34
|
module AgeAssertions
|
35
|
+
|
36
|
+
# list methods required for concern
|
37
|
+
def self.require_methods
|
38
|
+
%w[age]
|
39
|
+
end
|
40
|
+
|
35
41
|
def young?
|
36
42
|
age < options[:young]
|
37
43
|
end
|
@@ -45,27 +51,46 @@ module AgeAssertions
|
|
45
51
|
end
|
46
52
|
end
|
47
53
|
|
54
|
+
module Searchable
|
55
|
+
def self.require_methods
|
56
|
+
%w[all]
|
57
|
+
end
|
58
|
+
|
59
|
+
def find_by_name(name)
|
60
|
+
all.select {|item| item.first_name =~ /#{name}/}
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
48
64
|
class Person
|
49
65
|
|
50
66
|
attr :age
|
67
|
+
attr :name
|
68
|
+
|
69
|
+
def self.all
|
70
|
+
[new('nicola', 33), new('ivan', 33)]
|
71
|
+
end
|
51
72
|
|
52
|
-
def initialize(age)
|
53
|
-
@age = age
|
73
|
+
def initialize( name, age)
|
74
|
+
@name, @age = name, age
|
54
75
|
end
|
55
76
|
|
56
77
|
extend StrongConcerns
|
57
78
|
|
58
79
|
concern AgeAssertions,
|
59
|
-
require_methods: %w[age],
|
60
80
|
exports_methods: %w[young? reproductive?],
|
61
81
|
old: 70,
|
62
82
|
young: 14
|
83
|
+
|
84
|
+
class_concern Searchable,
|
85
|
+
exports_methods: %w[find_by_name]
|
63
86
|
end
|
64
87
|
|
65
|
-
nicola = Person.new('nicola',
|
88
|
+
nicola = Person.new('nicola', 33)
|
66
89
|
if nicola.reproductive?
|
67
90
|
puts "Cool, make me a child!"
|
68
91
|
end
|
92
|
+
|
93
|
+
Person.find_by_name('nicola') #=> Person(name: 'nicola')
|
69
94
|
```
|
70
95
|
|
71
96
|
## Contributing
|
data/lib/strong_concerns.rb
CHANGED
@@ -2,7 +2,7 @@ require 'forwardable'
|
|
2
2
|
require "strong_concerns/version"
|
3
3
|
|
4
4
|
module StrongConcerns
|
5
|
-
class
|
5
|
+
class Intermediate
|
6
6
|
extend Forwardable
|
7
7
|
attr_accessor :options
|
8
8
|
|
@@ -11,21 +11,40 @@ module StrongConcerns
|
|
11
11
|
@options = options
|
12
12
|
end
|
13
13
|
|
14
|
+
def method_missing(meth)
|
15
|
+
raise NameError.new("Looks like you not list method <#{meth}> in self.require_methods of concern or misspelled it")
|
16
|
+
end
|
17
|
+
|
14
18
|
def inspect
|
15
|
-
"
|
19
|
+
"Intermediate<#{self.methods}>"
|
16
20
|
end
|
17
21
|
end
|
18
22
|
|
19
|
-
def
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
23
|
+
def class_concern(mod, options)
|
24
|
+
intermediate_class = prepare_intermediate(mod)
|
25
|
+
options.fetch(:exports_methods).each do |meth|
|
26
|
+
self.define_singleton_method meth do |*args, &block|
|
27
|
+
((@__strong_concerns ||= {})[mod.to_s] ||= intermediate_class.new(self, options)).send(meth,*args, &block)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
24
31
|
|
32
|
+
def concern(mod, options)
|
33
|
+
intermediate_class = prepare_intermediate(mod)
|
25
34
|
options.fetch(:exports_methods).each do |meth|
|
26
|
-
define_method meth do |*args, &block|
|
27
|
-
((@__strong_concerns ||= {})[mod.to_s] ||=
|
35
|
+
self.send(:define_method, meth) do |*args, &block|
|
36
|
+
((@__strong_concerns ||= {})[mod.to_s] ||= intermediate_class.new(self, options)).send(meth,*args, &block)
|
28
37
|
end
|
29
38
|
end
|
30
39
|
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def prepare_intermediate(mod)
|
44
|
+
Class.new(Intermediate).tap do |kls|
|
45
|
+
kls.send(:include, mod)
|
46
|
+
meths = mod.require_methods
|
47
|
+
kls.def_delegators :@__subject, *meths
|
48
|
+
end
|
49
|
+
end
|
31
50
|
end
|
@@ -2,12 +2,20 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe StrongConcerns do
|
4
4
|
module FullNamed
|
5
|
+
def self.require_methods
|
6
|
+
%w[first_name last_name]
|
7
|
+
end
|
8
|
+
|
5
9
|
def full_name
|
6
10
|
"#{first_name} #{last_name}"
|
7
11
|
end
|
8
12
|
end
|
9
13
|
|
10
14
|
module AgeAssertions
|
15
|
+
def self.require_methods
|
16
|
+
%w[age]
|
17
|
+
end
|
18
|
+
|
11
19
|
def young?
|
12
20
|
age < options[:young]
|
13
21
|
end
|
@@ -19,6 +27,21 @@ describe StrongConcerns do
|
|
19
27
|
def old?
|
20
28
|
age > options[:old]
|
21
29
|
end
|
30
|
+
|
31
|
+
#should raise
|
32
|
+
def breaking
|
33
|
+
first_name
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module Searchable
|
38
|
+
def self.require_methods
|
39
|
+
%w[all]
|
40
|
+
end
|
41
|
+
|
42
|
+
def find_by_name(name)
|
43
|
+
all.select {|item| item.first_name =~ /#{name}/}
|
44
|
+
end
|
22
45
|
end
|
23
46
|
|
24
47
|
class Person
|
@@ -33,16 +56,23 @@ describe StrongConcerns do
|
|
33
56
|
@age = age
|
34
57
|
end
|
35
58
|
|
59
|
+
def self.all
|
60
|
+
[
|
61
|
+
new('nicola','rhyzhikov', 33),
|
62
|
+
new('ivan','ivanov', 33)
|
63
|
+
]
|
64
|
+
end
|
36
65
|
|
37
66
|
extend StrongConcerns
|
38
67
|
|
68
|
+
class_concern Searchable,
|
69
|
+
exports_methods: %w[find_by_name]
|
70
|
+
|
39
71
|
concern AgeAssertions,
|
40
|
-
|
41
|
-
exports_methods: %w[young? reproductive?],
|
72
|
+
exports_methods: %w[young? reproductive? breaking],
|
42
73
|
young: 14, old: 70
|
43
74
|
|
44
75
|
concern FullNamed,
|
45
|
-
require_methods: %w[first_name last_name],
|
46
76
|
exports_methods: %w[full_name]
|
47
77
|
|
48
78
|
end
|
@@ -52,6 +82,14 @@ describe StrongConcerns do
|
|
52
82
|
nicola.full_name.should == "nicola rhyzhikov"
|
53
83
|
nicola.should_not be_young
|
54
84
|
nicola.should be_reproductive
|
85
|
+
|
86
|
+
-> {
|
87
|
+
nicola.breaking
|
88
|
+
}.should raise_error(/not list method/)
|
55
89
|
end
|
56
90
|
end
|
91
|
+
|
92
|
+
example do
|
93
|
+
Person.find_by_name('nicola').should_not be_empty
|
94
|
+
end
|
57
95
|
end
|
data/strong_concerns.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["niquola@gmail.com"]
|
11
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
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 = ""
|
13
|
+
spec.homepage = "https://github.com/niquola/strong_concerns"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strong_concerns
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- niquola
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -56,7 +56,7 @@ files:
|
|
56
56
|
- spec/spec_helper.rb
|
57
57
|
- spec/strong_concerns_spec.rb
|
58
58
|
- strong_concerns.gemspec
|
59
|
-
homepage:
|
59
|
+
homepage: https://github.com/niquola/strong_concerns
|
60
60
|
licenses:
|
61
61
|
- MIT
|
62
62
|
metadata: {}
|