value_struct 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/ChangeLog.md +5 -2
- data/README.md +0 -4
- data/lib/value_struct.rb +1 -0
- data/lib/value_struct/clone.rb +5 -0
- data/lib/value_struct/dup_with_changes.rb +5 -5
- data/lib/value_struct/version.rb +2 -2
- data/spec/clone_spec.rb +10 -0
- data/spec/dup_with_changes_spec.rb +25 -0
- data/spec/freeze_spec.rb +11 -0
- data/spec/strict_arguments_spec.rb +13 -0
- data/spec/struct_value_spec.rb +5 -16
- data/spec/to_h_spec.rb +11 -0
- metadata +7 -1
data/.gitignore
CHANGED
data/ChangeLog.md
CHANGED
data/README.md
CHANGED
data/lib/value_struct.rb
CHANGED
@@ -3,6 +3,7 @@ require_relative 'value_struct/core'
|
|
3
3
|
require_relative 'value_struct/to_h'
|
4
4
|
require_relative 'value_struct/dup_with_changes'
|
5
5
|
require_relative 'value_struct/strict_arguments'
|
6
|
+
require_relative 'value_struct/clone'
|
6
7
|
require_relative 'value_struct/freeze'
|
7
8
|
|
8
9
|
class ValueStruct < Struct
|
@@ -2,12 +2,12 @@ module ValueStruct::DupWithChanges
|
|
2
2
|
def dup(changes = {})
|
3
3
|
case changes
|
4
4
|
when {}
|
5
|
-
new(values)
|
5
|
+
self.class.new(*values)
|
6
6
|
when Hash
|
7
|
-
new(
|
8
|
-
members.zip(values).map{ |member, value|
|
9
|
-
if changes.has_key?(
|
10
|
-
changes[
|
7
|
+
self.class.new(
|
8
|
+
*members.zip(values).map{ |member, value|
|
9
|
+
if changes.has_key?(member)
|
10
|
+
changes[member]
|
11
11
|
else
|
12
12
|
value
|
13
13
|
end
|
data/lib/value_struct/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
class ValueStruct < Struct
|
2
|
-
VERSION = '0.
|
3
|
-
end
|
2
|
+
VERSION = '0.6.0'
|
3
|
+
end
|
data/spec/clone_spec.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValueStruct::Clone do
|
4
|
+
subject do
|
5
|
+
ValueStruct.new_with_mixins(:x, :y, [ValueStruct::Clone]).new(1,2)
|
6
|
+
end
|
7
|
+
|
8
|
+
it{ subject.object_id.should == subject.clone.object_id }
|
9
|
+
it{ subject.object_id.should_not == subject.dup.object_id }
|
10
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValueStruct::DupWithChanges do
|
4
|
+
subject do
|
5
|
+
ValueStruct.new_with_mixins(:x, :y, [ValueStruct::DupWithChanges]).new(1,2)
|
6
|
+
end
|
7
|
+
|
8
|
+
context 'no arguments' do
|
9
|
+
it 'is equal' do
|
10
|
+
subject.should == subject.dup
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'is another object' do
|
14
|
+
subject.object_id.should_not == subject.dup.object_id
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'with arguments' do
|
19
|
+
let(:modified){ subject.dup(y: 5) }
|
20
|
+
it 'is equal, except for the changed field' do
|
21
|
+
subject.x.should == modified.x
|
22
|
+
subject.y.should_not == modified.y
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/spec/freeze_spec.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValueStruct::StrictArguments do
|
4
|
+
subject do
|
5
|
+
ValueStruct.new_with_mixins(:x, :y, [ValueStruct::StrictArguments]).new
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'raises argument errors if not given the right number of arguments' do
|
9
|
+
lambda{
|
10
|
+
subject.new
|
11
|
+
}.should raise_error(ArgumentError, 'wrong number of arguments (0 for 2)')
|
12
|
+
end
|
13
|
+
end
|
data/spec/struct_value_spec.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ValueStruct do
|
4
|
-
|
5
|
-
ValueStruct.const_get('VERSION').should_not be_empty
|
6
|
-
end
|
7
|
-
|
8
|
-
describe 'initialization' do
|
4
|
+
describe 'general behavior' do
|
9
5
|
subject do
|
10
6
|
ValueStruct.new(:x, :y)
|
11
7
|
end
|
12
8
|
|
9
|
+
it "should have a VERSION constant" do
|
10
|
+
ValueStruct.const_get('VERSION').should_not be_empty
|
11
|
+
end
|
12
|
+
|
13
13
|
it 'creates Class instances' do
|
14
14
|
should be_instance_of Class
|
15
15
|
end
|
@@ -37,12 +37,6 @@ describe ValueStruct do
|
|
37
37
|
expect{ subject[:x] = 5 }.to raise_error(NoMethodError)
|
38
38
|
end
|
39
39
|
|
40
|
-
# it 'raises argument errors if not given the right number of arguments' do
|
41
|
-
# lambda{
|
42
|
-
# Point.new
|
43
|
-
# }.should raise_error(ArgumentError, 'wrong number of arguments (0 for 2)')
|
44
|
-
# end
|
45
|
-
|
46
40
|
it 'can be inherited from to add methods' do
|
47
41
|
class GraphPoint < ValueStruct.new(:x, :y)
|
48
42
|
def inspect
|
@@ -86,9 +80,4 @@ describe ValueStruct do
|
|
86
80
|
debugger
|
87
81
|
end
|
88
82
|
end
|
89
|
-
|
90
|
-
# describe '#clone and #dup return the same object' do
|
91
|
-
# it{ subject.object_id.should == subject.clone.object_id }
|
92
|
-
# it{ subject.object_id.should == subject.dup.object_id }
|
93
|
-
# end
|
94
83
|
end
|
data/spec/to_h_spec.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: value_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -191,6 +191,7 @@ files:
|
|
191
191
|
- Rakefile
|
192
192
|
- gemspec.yml
|
193
193
|
- lib/value_struct.rb
|
194
|
+
- lib/value_struct/clone.rb
|
194
195
|
- lib/value_struct/core.rb
|
195
196
|
- lib/value_struct/dup_with_changes.rb
|
196
197
|
- lib/value_struct/freeze.rb
|
@@ -198,8 +199,13 @@ files:
|
|
198
199
|
- lib/value_struct/to_h.rb
|
199
200
|
- lib/value_struct/version.rb
|
200
201
|
- spec/benchmark.rb
|
202
|
+
- spec/clone_spec.rb
|
203
|
+
- spec/dup_with_changes_spec.rb
|
204
|
+
- spec/freeze_spec.rb
|
201
205
|
- spec/spec_helper.rb
|
206
|
+
- spec/strict_arguments_spec.rb
|
202
207
|
- spec/struct_value_spec.rb
|
208
|
+
- spec/to_h_spec.rb
|
203
209
|
- value_struct.gemspec
|
204
210
|
homepage: https://github.com/janlelis/value_struct
|
205
211
|
licenses:
|