attribute-driven 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1 @@
1
- require 'attribute_driven'
1
+ require File.join(File.dirname(__FILE__), 'attribute_driven')
@@ -15,13 +15,21 @@
15
15
  =end
16
16
 
17
17
  module AttributeDriven
18
+ def self.attributes_from(args)
19
+ (args.last.kind_of? Hash) ? args[0..-2] : args
20
+ end
21
+
22
+ def self.options_from(args)
23
+ (args.last.kind_of? Hash) ? args.last : {}
24
+ end
25
+
18
26
  def self.included(klass)
19
27
  klass.extend(ClassMethods)
20
28
  end
21
29
 
22
30
  module ClassMethods
23
31
  def initializes_with(*attributes)
24
- class_eval(<<-EOS, __FILE__, __LINE__)
32
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
25
33
  def initialize(params = {})
26
34
  #{attributes.inspect}.each do |attribute|
27
35
  instance_variable_set("@\#{attribute}", params[attribute])
@@ -30,10 +38,14 @@ module AttributeDriven
30
38
  EOS
31
39
  end
32
40
 
33
- def equality_based_on(*attributes)
34
- class_eval(<<-EOS, __FILE__, __LINE__)
41
+ def equality_based_on(*args)
42
+ attributes = AttributeDriven.attributes_from(args)
43
+ options = AttributeDriven.options_from(args)
44
+ raise "unknown option value of :class_check => #{options[:class_check].inspect}" unless [nil, :equal?, :kind_of?].include?(options[:class_check])
45
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
35
46
  def eql?(other)
36
47
  return true if equal?(other)
48
+ #{options[:class_check] ? "return false unless #{options[:class_check] == :equal? ? 'other.class.equal?(self.class)' : 'other.kind_of?(self.class)'}" : ""}
37
49
  #{attributes.inspect}.inject(true) do |result, attribute|
38
50
  result &&= (self.send(attribute) == other.send(attribute))
39
51
  end
@@ -48,14 +60,17 @@ module AttributeDriven
48
60
  end
49
61
 
50
62
  protected
51
- attr_reader *attributes
63
+ #{attributes.inspect}.each do |attribute|
64
+ attr_reader attribute unless instance_methods.include?(attribute)
65
+ end
52
66
  EOS
53
67
  end
54
68
 
55
- def attributes(*attributes)
56
- initializes_with *attributes
57
- equality_based_on *attributes
69
+ def attributes(*args)
70
+ attributes = AttributeDriven.attributes_from(args)
58
71
  attr_reader *attributes
72
+ initializes_with *attributes
73
+ equality_based_on *args
59
74
  end
60
75
  end
61
76
  end
@@ -1 +1 @@
1
- require 'attribute_driven'
1
+ require File.join(File.dirname(__FILE__), 'attribute_driven')
@@ -20,8 +20,8 @@ require File.join(File.dirname(__FILE__), '../lib/attribute_driven')
20
20
  class AttributeDrivenTest < Test::Unit::TestCase
21
21
  def setup
22
22
  @klass = Class.new do
23
- include AttributeDriven
24
- attributes :foo, :bar
23
+ include AttributeDriven
24
+ attributes :foo, :bar
25
25
  end
26
26
  end
27
27
 
@@ -53,6 +53,47 @@ class AttributeDrivenTest < Test::Unit::TestCase
53
53
  assert_not_equal(@klass.new(:foo => "foo1"), @klass.new(:foo => "foo2"))
54
54
  end
55
55
 
56
+ def test_should_allow_two_instances_to_be_equated_if_same
57
+ instance = @klass.new(:foo => "foo")
58
+ assert_equal(instance, instance)
59
+ end
60
+
61
+ def test_should_allow_equality_check_including_classes_based_on_equal
62
+ sub_klass = Class.new(@klass) do
63
+ include AttributeDriven
64
+ attributes :foo, :bar, :class_check => :equal?
65
+ end
66
+
67
+ another_sub_klass = Class.new(sub_klass)
68
+
69
+ assert_equal(true, sub_klass.new(:foo => "foo") == sub_klass.new(:foo => "foo"))
70
+ assert_equal(false, sub_klass.new(:foo => "foo") == @klass.new(:foo => "foo"))
71
+ assert_equal(false, sub_klass.new(:foo => "foo") == another_sub_klass.new(:foo => "foo"))
72
+ end
73
+
74
+ def test_should_allow_equality_check_including_classes_based_on_kind_of
75
+ sub_klass = Class.new(@klass) do
76
+ include AttributeDriven
77
+ attributes :foo, :bar, :class_check => :kind_of?
78
+ end
79
+
80
+ another_sub_klass = Class.new(sub_klass)
81
+
82
+ assert_equal(true, sub_klass.new(:foo => "foo") == sub_klass.new(:foo => "foo"))
83
+ assert_equal(false, sub_klass.new(:foo => "foo") == @klass.new(:foo => "foo"))
84
+ assert_equal(true, sub_klass.new(:foo => "foo") == another_sub_klass.new(:foo => "foo"))
85
+ end
86
+
87
+ def test_should_allow_equality_check_without_including_classes
88
+ other_klass = Class.new do
89
+ include AttributeDriven
90
+ attributes :foo, :bar
91
+ end
92
+
93
+ assert_equal(true, other_klass.new(:foo => "foo") == other_klass.new(:foo => "foo"))
94
+ assert_equal(true, other_klass.new(:foo => "foo") == @klass.new(:foo => "foo"))
95
+ end
96
+
56
97
  def test_should_return_same_hash_for_two_equal_objects
57
98
  assert_equal(@klass.new.hash, @klass.new.hash)
58
99
  assert_equal(@klass.new(:foo => "hi", :bar => "bye").hash, @klass.new(:foo => "hi", :bar => "bye").hash)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 1
9
- version: 1.0.1
8
+ - 2
9
+ version: 1.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Aman King
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2008-02-12 00:00:00 +05:30
17
+ date: 2011-02-12 00:00:00 +05:30
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
21
- description: AttributeDriven provides a class method to generate attribute-based equality-methods, initializer, and reader methods.
21
+ description: AttributeDriven provides class methods to generate attribute-based equality-methods, initializer, and reader methods.
22
22
  email: amanking@gmail.com
23
23
  executables: []
24
24
 
@@ -42,6 +42,7 @@ rdoc_options: []
42
42
  require_paths:
43
43
  - lib
44
44
  required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
45
46
  requirements:
46
47
  - - ">="
47
48
  - !ruby/object:Gem::Version
@@ -49,6 +50,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
49
50
  - 0
50
51
  version: "0"
51
52
  required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
52
54
  requirements:
53
55
  - - ">="
54
56
  - !ruby/object:Gem::Version
@@ -58,9 +60,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
60
  requirements: []
59
61
 
60
62
  rubyforge_project:
61
- rubygems_version: 1.3.6
63
+ rubygems_version: 1.3.7
62
64
  signing_key:
63
65
  specification_version: 3
64
- summary: AttributeDriven provides a class method to generate attribute-based equality-methods, initializer, and reader methods.
66
+ summary: AttributeDriven provides class methods to generate attribute-based equality-methods, initializer, and reader methods.
65
67
  test_files:
66
68
  - test/attribute_driven_test.rb