demeter 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -77,6 +77,7 @@ Contributor
77
77
  -----------
78
78
 
79
79
  * Nando Vieira (<http://simplesideias.com.br/>)
80
+ * Tino Gomes (<http://blog.tinogomes.com/>)
80
81
 
81
82
  License:
82
83
  --------
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.2
1
+ 2.0.3
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{demeter}
8
- s.version = "2.0.2"
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-01-22}
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 = [
@@ -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
- module ClassMethods
29
- def demeter(*attr_names)
30
- @@demeter_names ||= []
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
- def demeter_names
35
- @@demeter_names ||= []
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
- def demeter_names=(names)
39
- @@demeter_names = names
45
+ module ClassMethods
46
+ def demeter(*attr_names)
47
+ self.demeter_names = attr_names
40
48
  end
41
49
  end
42
50
  end
@@ -1,3 +1,6 @@
1
- ActiveRecord::Base.class_eval do
2
- extend ::Demeter
1
+ class ActiveRecord::Base
2
+ def self.inherited(base)
3
+ super
4
+ base.send :extend, ::Demeter
5
+ end
3
6
  end if defined? ActiveRecord::Base
@@ -2,7 +2,7 @@ module Demeter
2
2
  module Version
3
3
  MAJOR = "2"
4
4
  MINOR = "0"
5
- PATCH = "2"
5
+ PATCH = "3"
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
@@ -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
- ActiveRecord::Base.should respond_to(:demeter)
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 #name uses ActiveRecord method_missing instance method
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
@@ -4,6 +4,7 @@ require "benchmark"
4
4
  require "active_record"
5
5
 
6
6
  # Load library
7
+ $LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
7
8
  require "demeter"
8
9
 
9
10
  # Establish connection with in memory SQLite 3 database
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.2
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-01-22 00:00:00 -02:00
12
+ date: 2010-02-27 00:00:00 -03:00
13
13
  default_executable:
14
14
  dependencies: []
15
15