values 1.0.0
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/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +28 -0
- data/LICENSE.txt +20 -0
- data/README.md +23 -0
- data/Rakefile +59 -0
- data/VERSION +1 -0
- data/lib/values.rb +17 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/values_spec.rb +52 -0
- data/values.gemspec +74 -0
- metadata +135 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec", "~> 2.3.0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.5.2"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.5.2)
|
7
|
+
bundler (~> 1.0.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rake (0.8.7)
|
11
|
+
rcov (0.9.9)
|
12
|
+
rspec (2.3.0)
|
13
|
+
rspec-core (~> 2.3.0)
|
14
|
+
rspec-expectations (~> 2.3.0)
|
15
|
+
rspec-mocks (~> 2.3.0)
|
16
|
+
rspec-core (2.3.1)
|
17
|
+
rspec-expectations (2.3.0)
|
18
|
+
diff-lcs (~> 1.1.2)
|
19
|
+
rspec-mocks (2.3.0)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
bundler (~> 1.0.0)
|
26
|
+
jeweler (~> 1.5.2)
|
27
|
+
rcov
|
28
|
+
rspec (~> 2.3.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Tom Crayford
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Values is a tiny library for creating value objects in ruby.
|
2
|
+
These mostly look like classes created using Struct, but fix two problems with those:
|
3
|
+
|
4
|
+
- Struct constructors can take less than the default number of arguments and set other fields as nil:
|
5
|
+
Point = Struct.new(:x,:y)
|
6
|
+
Point.new(1)
|
7
|
+
=> #<struct Point x=1, y=nil>
|
8
|
+
|
9
|
+
- Structs are also mutable:
|
10
|
+
Point = Struct.new(:x,:y)
|
11
|
+
p = Point.new(1,2)
|
12
|
+
p.x = 2
|
13
|
+
p.x
|
14
|
+
=> 2
|
15
|
+
|
16
|
+
Values fixes both of these:
|
17
|
+
Point = Value.new(:x, :y)
|
18
|
+
Point.new(1)
|
19
|
+
=> SOME EXCEPTION
|
20
|
+
|
21
|
+
p = Point.new(1,2)
|
22
|
+
p.x = 1
|
23
|
+
=> SOME EXCEPTION
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "values"
|
16
|
+
gem.homepage = "http://github.com/tcrayford/values"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Simple immutable value objects for ruby}
|
19
|
+
gem.description = %Q{Simple immutable value objects for ruby.
|
20
|
+
|
21
|
+
Make a new value class: Point = Value.new(:x, :y)
|
22
|
+
And use it:
|
23
|
+
p = Point.new(1,0)
|
24
|
+
p.x
|
25
|
+
=> 1
|
26
|
+
p.y
|
27
|
+
=> 0
|
28
|
+
}
|
29
|
+
gem.email = "tcrayford@googlemail.com"
|
30
|
+
gem.authors = ["Tom Crayford"]
|
31
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
32
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
33
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
34
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
35
|
+
end
|
36
|
+
Jeweler::RubygemsDotOrgTasks.new
|
37
|
+
|
38
|
+
require 'rspec/core'
|
39
|
+
require 'rspec/core/rake_task'
|
40
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
41
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
42
|
+
end
|
43
|
+
|
44
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
45
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
46
|
+
spec.rcov = true
|
47
|
+
end
|
48
|
+
|
49
|
+
task :default => :spec
|
50
|
+
|
51
|
+
require 'rake/rdoctask'
|
52
|
+
Rake::RDocTask.new do |rdoc|
|
53
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
54
|
+
|
55
|
+
rdoc.rdoc_dir = 'rdoc'
|
56
|
+
rdoc.title = "values #{version}"
|
57
|
+
rdoc.rdoc_files.include('README*')
|
58
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
59
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/lib/values.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
class Value
|
2
|
+
def self.new(*fields)
|
3
|
+
value_class = Class.new
|
4
|
+
value_class.instance_eval do
|
5
|
+
attr_reader *fields
|
6
|
+
define_method(:initialize) do |*input_fields|
|
7
|
+
raise ArgumentError.new("wrong number of arguments, #{input_fields.size} for #{fields.size}") if fields.size != input_fields.size
|
8
|
+
|
9
|
+
fields.each_with_index do |field, index|
|
10
|
+
instance_variable_set('@' + field.to_s, input_fields[index])
|
11
|
+
end
|
12
|
+
self.freeze
|
13
|
+
end
|
14
|
+
end
|
15
|
+
return value_class
|
16
|
+
end
|
17
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'values'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
data/spec/values_spec.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/values')
|
2
|
+
describe 'values' do
|
3
|
+
it 'stores a single field' do
|
4
|
+
Cell = Value.new(:alive)
|
5
|
+
c = Cell.new(true)
|
6
|
+
c.alive.should == true
|
7
|
+
end
|
8
|
+
|
9
|
+
Point = Value.new(:x, :y)
|
10
|
+
it 'stores multiple values' do
|
11
|
+
p = Point.new(0,1)
|
12
|
+
p.x.should == 0
|
13
|
+
p.y.should == 1
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'raises argument errors if not given the right number of arguments' do
|
17
|
+
lambda { Point.new }.should raise_error(ArgumentError, 'wrong number of arguments, 0 for 2')
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'can be inherited from to add methods' do
|
21
|
+
class GraphPoint < Value.new(:x, :y)
|
22
|
+
def inspect
|
23
|
+
"GraphPoint at #{@x},#{@y}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
c = GraphPoint.new(0,0)
|
28
|
+
c.inspect.should == 'GraphPoint at 0,0'
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'cannot be mutated' do
|
32
|
+
p = Point.new(0,1)
|
33
|
+
lambda { p.x = 1}.should raise_error
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'cannot even be mutated inside a sublass with methods' do
|
37
|
+
class Cow < Value.new(:color)
|
38
|
+
def change_color(new_color)
|
39
|
+
@color = new_color
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
c = Cow.new("red")
|
44
|
+
lambda {c.change_color("blue")}.should raise_error
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'cannot be mutated using #instance_variable_set' do
|
48
|
+
Money = Value.new(:amount, :denomination)
|
49
|
+
m = Money.new(1, 'USD')
|
50
|
+
lambda {m.instance_variable_set('@amount',2)}.should raise_error
|
51
|
+
end
|
52
|
+
end
|
data/values.gemspec
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{values}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Tom Crayford"]
|
12
|
+
s.date = %q{2011-03-19}
|
13
|
+
s.description = %q{Simple immutable value objects for ruby.
|
14
|
+
|
15
|
+
Make a new value class: Point = Value.new(:x, :y)
|
16
|
+
And use it:
|
17
|
+
p = Point.new(1,0)
|
18
|
+
p.x
|
19
|
+
=> 1
|
20
|
+
p.y
|
21
|
+
=> 0
|
22
|
+
}
|
23
|
+
s.email = %q{tcrayford@googlemail.com}
|
24
|
+
s.extra_rdoc_files = [
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.md"
|
27
|
+
]
|
28
|
+
s.files = [
|
29
|
+
".document",
|
30
|
+
".rspec",
|
31
|
+
"Gemfile",
|
32
|
+
"Gemfile.lock",
|
33
|
+
"LICENSE.txt",
|
34
|
+
"README.md",
|
35
|
+
"Rakefile",
|
36
|
+
"VERSION",
|
37
|
+
"lib/values.rb",
|
38
|
+
"spec/spec_helper.rb",
|
39
|
+
"spec/values_spec.rb",
|
40
|
+
"values.gemspec"
|
41
|
+
]
|
42
|
+
s.homepage = %q{http://github.com/tcrayford/values}
|
43
|
+
s.licenses = ["MIT"]
|
44
|
+
s.require_paths = ["lib"]
|
45
|
+
s.rubygems_version = %q{1.3.7}
|
46
|
+
s.summary = %q{Simple immutable value objects for ruby}
|
47
|
+
s.test_files = [
|
48
|
+
"spec/spec_helper.rb",
|
49
|
+
"spec/values_spec.rb"
|
50
|
+
]
|
51
|
+
|
52
|
+
if s.respond_to? :specification_version then
|
53
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
54
|
+
s.specification_version = 3
|
55
|
+
|
56
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
57
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
|
58
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
59
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
60
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
61
|
+
else
|
62
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
63
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
64
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
65
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
66
|
+
end
|
67
|
+
else
|
68
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
69
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
70
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
71
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: values
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Tom Crayford
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-03-19 00:00:00 +00:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 2
|
29
|
+
- 3
|
30
|
+
- 0
|
31
|
+
version: 2.3.0
|
32
|
+
type: :development
|
33
|
+
prerelease: false
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: bundler
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 1
|
44
|
+
- 0
|
45
|
+
- 0
|
46
|
+
version: 1.0.0
|
47
|
+
type: :development
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: jeweler
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ~>
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 1
|
59
|
+
- 5
|
60
|
+
- 2
|
61
|
+
version: 1.5.2
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: rcov
|
67
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
type: :development
|
76
|
+
prerelease: false
|
77
|
+
version_requirements: *id004
|
78
|
+
description: "Simple immutable value objects for ruby.\n\n Make a new value class: Point = Value.new(:x, :y)\n And use it:\n p = Point.new(1,0)\n p.x\n => 1\n p.y\n => 0\n "
|
79
|
+
email: tcrayford@googlemail.com
|
80
|
+
executables: []
|
81
|
+
|
82
|
+
extensions: []
|
83
|
+
|
84
|
+
extra_rdoc_files:
|
85
|
+
- LICENSE.txt
|
86
|
+
- README.md
|
87
|
+
files:
|
88
|
+
- .document
|
89
|
+
- .rspec
|
90
|
+
- Gemfile
|
91
|
+
- Gemfile.lock
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- VERSION
|
96
|
+
- lib/values.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
- spec/values_spec.rb
|
99
|
+
- values.gemspec
|
100
|
+
has_rdoc: true
|
101
|
+
homepage: http://github.com/tcrayford/values
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
hash: -1632268745089486033
|
115
|
+
segments:
|
116
|
+
- 0
|
117
|
+
version: "0"
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
segments:
|
124
|
+
- 0
|
125
|
+
version: "0"
|
126
|
+
requirements: []
|
127
|
+
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 1.3.7
|
130
|
+
signing_key:
|
131
|
+
specification_version: 3
|
132
|
+
summary: Simple immutable value objects for ruby
|
133
|
+
test_files:
|
134
|
+
- spec/spec_helper.rb
|
135
|
+
- spec/values_spec.rb
|