value 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,3 @@
1
+ Value
2
+
3
+ Value is a library for defining value objects in Ruby.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'lookout/rake/tasks'
4
+ require 'yard'
5
+
6
+ Lookout::Rake::Tasks::Test.new
7
+ Lookout::Rake::Tasks::Gem.new
8
+ Lookout::Rake::Tasks::Tags.new
9
+ YARD::Rake::YardocTask.new
@@ -0,0 +1,5 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ class Value
4
+ Version = '0.1.0'
5
+ end
data/lib/value.rb ADDED
@@ -0,0 +1,49 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ class Value
4
+ autoload :Version, 'value/version'
5
+
6
+ class << self
7
+ def values(*values)
8
+ if values.empty?
9
+ raise RuntimeError,
10
+ 'no values have been set for class: %p' % self if not defined? @values
11
+ @values
12
+ else
13
+ @values ||= []
14
+ @values.concat values.map{ |value| value.to_sym }.tap{ |symbols|
15
+ attr_reader(*symbols)
16
+ protected(*symbols)
17
+ }
18
+ end
19
+ end
20
+ end
21
+
22
+ def initialize(*arguments)
23
+ raise ArgumentError,
24
+ 'wrong number of arguments (%d for %d)' %
25
+ [arguments.length, self.class.values.length] if
26
+ arguments.length != self.class.values.length
27
+ self.class.values.each_with_index do |value, index|
28
+ instance_variable_set :"@#{value}", arguments[index]
29
+ end
30
+ freeze
31
+ end
32
+
33
+ def ==(other)
34
+ self.class == other.class and
35
+ self.class.values.all?{ |value| send(value) == other.send(value) }
36
+ end
37
+
38
+ alias eql? ==
39
+
40
+ def hash
41
+ self.class.hash ^ self.class.values.map{ |value| send(value) }.hash
42
+ end
43
+
44
+ def inspect
45
+ '%s.new(%s)' %
46
+ [self.class,
47
+ self.class.values.map{ |value| send(value).inspect }.join(', ')]
48
+ end
49
+ end
@@ -0,0 +1,82 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ Expectations do
4
+ expect RuntimeError.new(/\Ano values have been set for class: /) do
5
+ Class.new(Value).values
6
+ end
7
+
8
+ expect [:a, :b, :c] do
9
+ Class.new(Value).values(:a, :b, :c)
10
+ end
11
+
12
+ expect [:a, :b, :c] do
13
+ Class.new(Value).tap{ |c| c.values :a, :b }.values(:c)
14
+ end
15
+
16
+ expect ArgumentError.new('wrong number of arguments (0 for 1)') do
17
+ Class.new(Value).tap{ |c| c.values :a }.new
18
+ end
19
+
20
+ expect ArgumentError.new('wrong number of arguments (2 for 1)') do
21
+ Class.new(Value).tap{ |c| c.values :a }.new(1, 2)
22
+ end
23
+
24
+ expect Class.new(Value) do |c|
25
+ c.tap{ |c| c.values :a }.new(1)
26
+ end
27
+
28
+ expect true do
29
+ Class.new(Value).tap{ |c| c.values :a }.new(1).frozen?
30
+ end
31
+
32
+ expect NoMethodError do
33
+ Class.new(Value).tap{ |c| c.values :a }.new(1).a
34
+ end
35
+
36
+ expect 1 do
37
+ Class.new(Value).tap{ |c| c.values :a }.new(1).instance_eval{ a }
38
+ end
39
+
40
+ expect 1 do
41
+ Class.new(Value).tap{ |c| c.values :a }.new(1).instance_variable_get(:@a)
42
+ end
43
+
44
+ expect true do
45
+ c = Class.new(Value).tap{ |c| c.values :a, :b, :c }
46
+ c.new(1, 2, 3) == c.new(1, 2, 3)
47
+ end
48
+
49
+ expect false do
50
+ c = Class.new(Value).tap{ |c| c.values :a, :b, :c }
51
+ c.new(1, 2, 3) == c.new(2, 3, 1)
52
+ end
53
+
54
+ expect true do
55
+ c = Class.new(Value).tap{ |c| c.values :a, :b, :c }
56
+ c.new(1, 2, 3).eql? c.new(1, 2, 3)
57
+ end
58
+
59
+ expect false do
60
+ c = Class.new(Value).tap{ |c| c.values :a, :b, :c }
61
+ c.new(1, 2, 3).eql? c.new(2, 3, 1)
62
+ end
63
+
64
+ expect true do
65
+ c = Class.new(Value).tap{ |c| c.values :a, :b, :c }
66
+ c.new(1, 2, 3).hash == c.new(1, 2, 3).hash
67
+ end
68
+
69
+ expect false do
70
+ c = Class.new(Value).tap{ |c| c.values :a, :b, :c }
71
+ c.new(1, 2, 3).hash == c.new(2, 3, 1).hash
72
+ end
73
+
74
+ expect false do
75
+ c = Class.new(Value).tap{ |c| c.values :a, :b, :c }
76
+ c.new(1, 2, 3).hash == [1, 2, 3].hash
77
+ end
78
+
79
+ expect(/\A#<Class:0x[[:xdigit:]]+>\.new\(1, 2, 3\)\z/) do
80
+ Class.new(Value).tap{ |c| c.values :a, :b, :c }.new(1, 2, 3).inspect
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: value
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Nikolai Weibull
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-05 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: lookout
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 2
32
+ - 0
33
+ version: "2.0"
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rbtags
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 27
45
+ segments:
46
+ - 0
47
+ - 1
48
+ - 0
49
+ version: 0.1.0
50
+ type: :development
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: yard
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ - 7
64
+ - 0
65
+ version: 0.7.0
66
+ type: :development
67
+ version_requirements: *id003
68
+ description: " Value\n\n Value is a library for defining value objects in Ruby.\n"
69
+ email: now@bitwi.se
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ extra_rdoc_files: []
75
+
76
+ files:
77
+ - lib/value/version.rb
78
+ - lib/value.rb
79
+ - test/unit/value.rb
80
+ - README
81
+ - Rakefile
82
+ has_rdoc: true
83
+ homepage: http://github.com/now/value
84
+ licenses: []
85
+
86
+ post_install_message:
87
+ rdoc_options: []
88
+
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ hash: 3
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ requirements: []
110
+
111
+ rubyforge_project:
112
+ rubygems_version: 1.3.7
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Value is a library for defining value objects in Ruby.
116
+ test_files: []
117
+