demeter 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +56 -0
- data/Rakefile +2 -2
- data/lib/demeter.rb +10 -5
- data/spec/demeter_spec.rb +39 -0
- metadata +2 -2
- data/README +0 -49
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
## Demeter
|
2
|
+
|
3
|
+
A simple way to apply the Law of Demeter to your objects.
|
4
|
+
|
5
|
+
## Instalation
|
6
|
+
|
7
|
+
It might not be a bad idea to make sure you are running the latest RubyGems:
|
8
|
+
|
9
|
+
sudo gem update --system
|
10
|
+
|
11
|
+
Install demeter gem from gemcutter
|
12
|
+
|
13
|
+
sudo gem sources -a http://gemcutter.org
|
14
|
+
sudo gem install demeter
|
15
|
+
|
16
|
+
## Usuage
|
17
|
+
|
18
|
+
The usuage is very straightforward. You only need to extend one module and use one method.
|
19
|
+
|
20
|
+
## Example
|
21
|
+
|
22
|
+
require 'demeter'
|
23
|
+
class Customer
|
24
|
+
attr_accessor :name
|
25
|
+
end
|
26
|
+
|
27
|
+
class Manager
|
28
|
+
attr_accessor :type
|
29
|
+
end
|
30
|
+
|
31
|
+
class Account
|
32
|
+
extend Demeter
|
33
|
+
attr_accessor :customer, :manager
|
34
|
+
demeter :customer, :manager
|
35
|
+
end
|
36
|
+
|
37
|
+
c = Customer.new
|
38
|
+
c.name = "Emerson"
|
39
|
+
|
40
|
+
m = Manager.new
|
41
|
+
m.type = "General"
|
42
|
+
|
43
|
+
a = Account.new
|
44
|
+
a.customer = c
|
45
|
+
a.manager = m
|
46
|
+
puts a.customer_name #Should print Emerson at console
|
47
|
+
puts a.manager_type #Should print General at console
|
48
|
+
|
49
|
+
## Credits
|
50
|
+
#### Author: Emerson Macedo
|
51
|
+
#### email: emerleite at gmail dot com
|
52
|
+
#### twitter: emerleite
|
53
|
+
#### github: github dot com slash emerleite
|
54
|
+
|
55
|
+
## Licence
|
56
|
+
See LICENCE.txt
|
data/Rakefile
CHANGED
@@ -3,7 +3,7 @@ require "rake/gempackagetask"
|
|
3
3
|
|
4
4
|
spec = Gem::Specification.new do |s|
|
5
5
|
s.name = %q{demeter}
|
6
|
-
s.version = "1.0.
|
6
|
+
s.version = "1.0.4"
|
7
7
|
s.authors = ['Emerson Macedo']
|
8
8
|
s.email = ['emerleite@gmail.com']
|
9
9
|
s.date = %q{2009-11-11}
|
@@ -15,7 +15,7 @@ spec = Gem::Specification.new do |s|
|
|
15
15
|
s.rubygems_version = %q{1.3.5}
|
16
16
|
|
17
17
|
s.files = ['Rakefile',
|
18
|
-
'README',
|
18
|
+
'README.md',
|
19
19
|
'LICENSE.txt',
|
20
20
|
'TODO.txt',
|
21
21
|
'lib/demeter.rb',
|
data/lib/demeter.rb
CHANGED
@@ -6,7 +6,9 @@ module Demeter
|
|
6
6
|
def self.extended(base)
|
7
7
|
@@demeter_objects = @@demeter_objects ||= Hash.new
|
8
8
|
@@demeter_objects[base.to_s.to_sym] = Array.new
|
9
|
+
base.send(:alias_method, :old_method_missing, :method_missing)
|
9
10
|
base.send(:include, DemeterMethods)
|
11
|
+
base.send(:alias_method, :method_missing, :demeter_method_missing)
|
10
12
|
end
|
11
13
|
|
12
14
|
def demeter(*symbols)
|
@@ -16,12 +18,15 @@ module Demeter
|
|
16
18
|
end
|
17
19
|
|
18
20
|
module DemeterMethods
|
19
|
-
def
|
21
|
+
def demeter_method_missing(method, *args, &block)
|
20
22
|
message_split = method.to_s.split("_")
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
if Demeter.demeter_objects[self.class.to_s.to_sym].select { |object| message_split.first.to_sym == object }.length == 0
|
24
|
+
old_method_missing(method, *args, &block)
|
25
|
+
else
|
26
|
+
object = self.send(message_split.first)
|
27
|
+
message_split.delete message_split.first
|
28
|
+
object ? object.send(message_split.join("_")) : nil
|
29
|
+
end
|
25
30
|
end
|
26
31
|
end
|
27
32
|
end
|
data/spec/demeter_spec.rb
CHANGED
@@ -222,4 +222,43 @@ describe Demeter do
|
|
222
222
|
instance = AClass.new
|
223
223
|
instance.person_name.should be_nil
|
224
224
|
end
|
225
|
+
|
226
|
+
it 'Should invoke old method missing when not demeter' do
|
227
|
+
AClass.class_eval do
|
228
|
+
define_method :method_missing do | method, *args |
|
229
|
+
"original #{method}"
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
AClass.send(:attr_accessor, :person)
|
234
|
+
AClass.demeter :person
|
235
|
+
|
236
|
+
instance = AClass.new
|
237
|
+
instance.some_method.should be_eql("original some_method")
|
238
|
+
end
|
239
|
+
|
240
|
+
it 'Should demeter when have old method missing but invoke demetered method' do
|
241
|
+
AClass = Class.new
|
242
|
+
AClass.class_eval do
|
243
|
+
define_method :method_missing do | method, *args |
|
244
|
+
"original #{method}"
|
245
|
+
end
|
246
|
+
end
|
247
|
+
AClass.send(:extend, Demeter)
|
248
|
+
|
249
|
+
Person = Class.new
|
250
|
+
Person.send(:attr_accessor, :name)
|
251
|
+
|
252
|
+
AClass.send(:attr_accessor, :person)
|
253
|
+
AClass.demeter :person
|
254
|
+
|
255
|
+
instance = AClass.new
|
256
|
+
|
257
|
+
person = Person.new
|
258
|
+
person.name = "emerson"
|
259
|
+
|
260
|
+
instance.person = person
|
261
|
+
puts instance.person_name
|
262
|
+
instance.person_name.should be_eql("emerson")
|
263
|
+
end
|
225
264
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: demeter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emerson Macedo
|
@@ -24,7 +24,7 @@ extra_rdoc_files: []
|
|
24
24
|
|
25
25
|
files:
|
26
26
|
- Rakefile
|
27
|
-
- README
|
27
|
+
- README.md
|
28
28
|
- LICENSE.txt
|
29
29
|
- TODO.txt
|
30
30
|
- lib/demeter.rb
|
data/README
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
= Demeter
|
2
|
-
|
3
|
-
A simple way to apply the Law of Demeter to your objects.
|
4
|
-
|
5
|
-
== Usuage
|
6
|
-
|
7
|
-
The usuage is very straightforward. You only need to extend one module and use one method.
|
8
|
-
|
9
|
-
== Example
|
10
|
-
|
11
|
-
------------------------------------
|
12
|
-
require 'demeter'
|
13
|
-
class Customer
|
14
|
-
attr_accessor :name
|
15
|
-
end
|
16
|
-
|
17
|
-
class Manager
|
18
|
-
attr_accessor :type
|
19
|
-
end
|
20
|
-
|
21
|
-
class Account
|
22
|
-
extend Demeter
|
23
|
-
attr_accessor :customer, :manager
|
24
|
-
demeter :customer, :manager
|
25
|
-
end
|
26
|
-
|
27
|
-
c = Customer.new
|
28
|
-
c.name = "Emerson"
|
29
|
-
|
30
|
-
m = Manager.new
|
31
|
-
m.type = "General"
|
32
|
-
|
33
|
-
a = Account.new
|
34
|
-
a.customer = c
|
35
|
-
a.manager = m
|
36
|
-
puts a.customer_name
|
37
|
-
puts a.manager_type
|
38
|
-
------------------------------------
|
39
|
-
It should print "Emerson" at console
|
40
|
-
And should print "General" at console
|
41
|
-
|
42
|
-
== Credits
|
43
|
-
Author: Emerson Macedo
|
44
|
-
email: emerleite at gmail dot com
|
45
|
-
twitter: emerleite
|
46
|
-
github: github dot com slash emerleite
|
47
|
-
|
48
|
-
== Licence
|
49
|
-
See LICENCE.txt
|