attribute-driven 1.0.1
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.
- data/README +43 -0
- data/lib/attribute-driven.rb +1 -0
- data/lib/attribute_driven.rb +61 -0
- data/lib/attributedriven.rb +1 -0
- data/test/attribute_driven_test.rb +65 -0
- metadata +66 -0
data/README
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
ATTRIBUTE-DRIVEN
|
2
|
+
by Aman King
|
3
|
+
|
4
|
+
http://bitbucket.org/amanking/attribute-driven/
|
5
|
+
|
6
|
+
A module that
|
7
|
+
(1) allows you to mention a list of attributes in a class,
|
8
|
+
(2) provides reader methods for the attributes,
|
9
|
+
(3) allows initialization of the class via a hash argument that gives values for the attributes,
|
10
|
+
(4) overrides equals and hash method to check for field equality
|
11
|
+
|
12
|
+
Usage:
|
13
|
+
|
14
|
+
require 'attribute_driven'
|
15
|
+
class Money
|
16
|
+
include AttributeDriven
|
17
|
+
attributes :value, :currency
|
18
|
+
# initializes_with :value, :currency #=> only adds the initialize method
|
19
|
+
# equality_based_on :value, :currency #=> only overrides equality methods and hash
|
20
|
+
end
|
21
|
+
|
22
|
+
money = Money.new(:value => 20, :currency => 'USD') #=> #<Money:0x5138e4 @value=20, @currency="USD">
|
23
|
+
money.value #=> 20
|
24
|
+
money.currency #=> "USD"
|
25
|
+
money == Money.new(:value => 20, :currency => 'USD') #=> true
|
26
|
+
|
27
|
+
(see test/attribute_driven_test.rb for more)
|
28
|
+
|
29
|
+
License
|
30
|
+
|
31
|
+
Copyright 2009 Aman King
|
32
|
+
|
33
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
34
|
+
you may not use this file except in compliance with the License.
|
35
|
+
You may obtain a copy of the License at
|
36
|
+
|
37
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
38
|
+
|
39
|
+
Unless required by applicable law or agreed to in writing, software
|
40
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
41
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
42
|
+
See the License for the specific language governing permissions and
|
43
|
+
limitations under the License.
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'attribute_driven'
|
@@ -0,0 +1,61 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2009 Aman King
|
3
|
+
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
you may not use this file except in compliance with the License.
|
6
|
+
You may obtain a copy of the License at
|
7
|
+
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
See the License for the specific language governing permissions and
|
14
|
+
limitations under the License.
|
15
|
+
=end
|
16
|
+
|
17
|
+
module AttributeDriven
|
18
|
+
def self.included(klass)
|
19
|
+
klass.extend(ClassMethods)
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
def initializes_with(*attributes)
|
24
|
+
class_eval(<<-EOS, __FILE__, __LINE__)
|
25
|
+
def initialize(params = {})
|
26
|
+
#{attributes.inspect}.each do |attribute|
|
27
|
+
instance_variable_set("@\#{attribute}", params[attribute])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
EOS
|
31
|
+
end
|
32
|
+
|
33
|
+
def equality_based_on(*attributes)
|
34
|
+
class_eval(<<-EOS, __FILE__, __LINE__)
|
35
|
+
def eql?(other)
|
36
|
+
return true if equal?(other)
|
37
|
+
#{attributes.inspect}.inject(true) do |result, attribute|
|
38
|
+
result &&= (self.send(attribute) == other.send(attribute))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
alias_method :==, :eql?
|
42
|
+
|
43
|
+
def hash
|
44
|
+
#{attributes.inspect}.inject(0) do |result, attribute|
|
45
|
+
result += self.send(attribute).hash if self.send(attribute)
|
46
|
+
result
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
protected
|
51
|
+
attr_reader *attributes
|
52
|
+
EOS
|
53
|
+
end
|
54
|
+
|
55
|
+
def attributes(*attributes)
|
56
|
+
initializes_with *attributes
|
57
|
+
equality_based_on *attributes
|
58
|
+
attr_reader *attributes
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'attribute_driven'
|
@@ -0,0 +1,65 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2009 Aman King
|
3
|
+
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
you may not use this file except in compliance with the License.
|
6
|
+
You may obtain a copy of the License at
|
7
|
+
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
See the License for the specific language governing permissions and
|
14
|
+
limitations under the License.
|
15
|
+
=end
|
16
|
+
|
17
|
+
require 'test/unit'
|
18
|
+
require File.join(File.dirname(__FILE__), '../lib/attribute_driven')
|
19
|
+
|
20
|
+
class AttributeDrivenTest < Test::Unit::TestCase
|
21
|
+
def setup
|
22
|
+
@klass = Class.new do
|
23
|
+
include AttributeDriven
|
24
|
+
attributes :foo, :bar
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_should_create_getters_for_specified_attributes
|
29
|
+
instance = @klass.new
|
30
|
+
assert_equal(true, instance.respond_to?(:foo))
|
31
|
+
assert_equal(true, instance.respond_to?(:bar))
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_should_allow_setting_attribute_values_when_instantiating
|
35
|
+
instance = @klass.new(:foo => "hi", :bar => "bye")
|
36
|
+
assert_equal("hi", instance.foo)
|
37
|
+
assert_equal("bye", instance.bar)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_should_return_nil_for_unset_attributes
|
41
|
+
instance = @klass.new(:foo => "hi")
|
42
|
+
assert_nil(instance.bar)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_should_ignore_unknown_attributes_when_instantiating
|
46
|
+
instance = @klass.new(:x => "x")
|
47
|
+
assert_equal(false, instance.respond_to?(:x))
|
48
|
+
assert_equal(false, instance.instance_variables.include?('@x'))
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_should_allow_two_instances_to_be_equated_based_on_attributes
|
52
|
+
assert_equal(@klass.new(:foo => "foo"), @klass.new(:foo => "foo"))
|
53
|
+
assert_not_equal(@klass.new(:foo => "foo1"), @klass.new(:foo => "foo2"))
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_should_return_same_hash_for_two_equal_objects
|
57
|
+
assert_equal(@klass.new.hash, @klass.new.hash)
|
58
|
+
assert_equal(@klass.new(:foo => "hi", :bar => "bye").hash, @klass.new(:foo => "hi", :bar => "bye").hash)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_should_not_break_equality_based_on_reference
|
62
|
+
instance = @klass.new(:foo => "foo")
|
63
|
+
assert_same(instance, instance)
|
64
|
+
end
|
65
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attribute-driven
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 1.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Aman King
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2008-02-12 00:00:00 +05:30
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: AttributeDriven provides a class method to generate attribute-based equality-methods, initializer, and reader methods.
|
22
|
+
email: amanking@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- README
|
31
|
+
- lib/attribute-driven.rb
|
32
|
+
- lib/attribute_driven.rb
|
33
|
+
- lib/attributedriven.rb
|
34
|
+
- test/attribute_driven_test.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://bitbucket.org/amanking/attribute-driven/
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.3.6
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: AttributeDriven provides a class method to generate attribute-based equality-methods, initializer, and reader methods.
|
65
|
+
test_files:
|
66
|
+
- test/attribute_driven_test.rb
|