demeter 2.0.2 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +1 -0
- data/VERSION +1 -1
- data/demeter.gemspec +2 -2
- data/lib/demeter.rb +18 -10
- data/lib/demeter/active_record.rb +5 -2
- data/lib/demeter/version.rb +1 -1
- data/spec/demeter/active_record_spec.rb +12 -3
- data/spec/demeter/demeter_spec.rb +32 -1
- data/spec/spec_helper.rb +1 -0
- metadata +2 -2
data/README.markdown
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.0.
|
1
|
+
2.0.3
|
data/demeter.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{demeter}
|
8
|
-
s.version = "2.0.
|
8
|
+
s.version = "2.0.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Emerson Macedo"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-02-27}
|
13
13
|
s.description = %q{A simple way to apply the Law of Demeter to your Ruby objects.}
|
14
14
|
s.email = %q{emerleite@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/demeter.rb
CHANGED
@@ -3,6 +3,12 @@ module Demeter
|
|
3
3
|
base.class_eval do
|
4
4
|
include InstanceMethods
|
5
5
|
extend ClassMethods
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_accessor :demeter_names
|
9
|
+
end
|
10
|
+
|
11
|
+
base.demeter_names = []
|
6
12
|
end
|
7
13
|
end
|
8
14
|
|
@@ -23,20 +29,22 @@ module Demeter
|
|
23
29
|
|
24
30
|
send(method_name)
|
25
31
|
end
|
26
|
-
end
|
27
32
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
@@demeter_names += attr_names
|
32
|
-
end
|
33
|
+
def respond_to?(method_name, include_private = false)
|
34
|
+
object_method_name = method_name.to_s
|
35
|
+
object_name = self.class.demeter_names.find {|name| object_method_name =~ /^#{name}_/ }
|
33
36
|
|
34
|
-
|
35
|
-
|
37
|
+
if object_name && (object = send(object_name))
|
38
|
+
object.respond_to?(object_method_name.gsub(/^#{Regexp.escape(object_name.to_s)}_/, ""))
|
39
|
+
else
|
40
|
+
super
|
41
|
+
end
|
36
42
|
end
|
43
|
+
end
|
37
44
|
|
38
|
-
|
39
|
-
|
45
|
+
module ClassMethods
|
46
|
+
def demeter(*attr_names)
|
47
|
+
self.demeter_names = attr_names
|
40
48
|
end
|
41
49
|
end
|
42
50
|
end
|
data/lib/demeter/version.rb
CHANGED
@@ -2,7 +2,9 @@ require File.dirname(__FILE__) + "/../spec_helper"
|
|
2
2
|
|
3
3
|
describe "Demeter on ActiveRecord" do
|
4
4
|
it "should respond to demeter method" do
|
5
|
-
|
5
|
+
Project.should respond_to(:demeter)
|
6
|
+
Task.should respond_to(:demeter)
|
7
|
+
Owner.should respond_to(:demeter)
|
6
8
|
end
|
7
9
|
|
8
10
|
it "should dispatch original method missing" do
|
@@ -13,8 +15,15 @@ describe "Demeter on ActiveRecord" do
|
|
13
15
|
end
|
14
16
|
|
15
17
|
it "should also dispatch original method missing when not demetered" do
|
16
|
-
Demeter::ClassMethods.send(:remove_class_variable, :@@demeter_names)
|
17
18
|
owner = Owner.new
|
18
|
-
owner.name.should be_nil
|
19
|
+
owner.name.should be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should load demeter_names for each model" do
|
23
|
+
Task.demeter :project
|
24
|
+
Owner.demeter :task
|
25
|
+
|
26
|
+
Task.demeter_names.should == [:project]
|
27
|
+
Owner.demeter_names.should == [:task]
|
19
28
|
end
|
20
29
|
end
|
@@ -14,6 +14,38 @@ describe "Demeter" do
|
|
14
14
|
subject.profile.interests = %w(games programming music movies)
|
15
15
|
end
|
16
16
|
|
17
|
+
it "should respond to demeterized methods" do
|
18
|
+
User.demeter :address
|
19
|
+
user = User.new
|
20
|
+
|
21
|
+
user.should respond_to(:address_street)
|
22
|
+
user.should respond_to(:address_city)
|
23
|
+
user.should respond_to(:address_country)
|
24
|
+
user.should respond_to(:address_state)
|
25
|
+
user.should respond_to(:address_zip_code)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should keep responding to instance methods" do
|
29
|
+
User.demeter :address
|
30
|
+
user = User.new
|
31
|
+
|
32
|
+
user.should respond_to(:name)
|
33
|
+
user.should respond_to(:address)
|
34
|
+
user.should respond_to(:video_game)
|
35
|
+
user.should respond_to(:profile)
|
36
|
+
user.should respond_to(:methods)
|
37
|
+
user.should respond_to(:public_methods)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should not respond to unknown methods" do
|
41
|
+
User.demeter :address
|
42
|
+
user = User.new
|
43
|
+
|
44
|
+
user.should_not respond_to(:video_game_title)
|
45
|
+
user.should_not respond_to(:video_production_year)
|
46
|
+
user.should_not respond_to(:profile_interests)
|
47
|
+
end
|
48
|
+
|
17
49
|
it "should allow demeter only one object" do
|
18
50
|
Person.demeter :animal
|
19
51
|
person = Person.new
|
@@ -68,7 +100,6 @@ describe "Demeter" do
|
|
68
100
|
|
69
101
|
describe Demeter::ClassMethods do
|
70
102
|
it "should return an array of demeter_names" do
|
71
|
-
Demeter::ClassMethods.send(:remove_class_variable, :@@demeter_names)
|
72
103
|
User.demeter_names.should be_kind_of(Array)
|
73
104
|
end
|
74
105
|
end
|
data/spec/spec_helper.rb
CHANGED
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: 2.0.
|
4
|
+
version: 2.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emerson Macedo
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-02-27 00:00:00 -03:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|