strict_struct 0.0.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e5940d1c83ecfa23afa0cb7d348cbb34b72f397a
4
- data.tar.gz: 8dfc4803fd53e15b7b975651a2db7283a6bdffde
3
+ metadata.gz: c74573f10c24a66bc073f1246d89c381b94adc4b
4
+ data.tar.gz: 0dac3d83c4dd1d86972abb9521c3ae618885ab19
5
5
  SHA512:
6
- metadata.gz: fa96254650eabfe6f8d0fc2aae953c5bdca43cd9a9087a958700c78f0d38bea5ea93e4a3b84dad161e860bde509ecf5b4f79bc4a9e546816c8b9f1169d4c09db
7
- data.tar.gz: ad2b62577ef0f6f8ae44eb466be05ce161ee0799a82d62b668c182f437a3f27c5f65e19a5a623da92811b7d67e252de6f98f63c8b6e8c27f70b6a35d96d56928
6
+ metadata.gz: c36bbfe1ed0fad71cd0909ea43b586fd463d7df1c839c3e1727271e54959bd19364196c86d17ded8100de56fcff9ec2151e0ec5d0568f0f25d5fa229052ce03a
7
+ data.tar.gz: 0093c350d87be13f0160a97a814a2854fd9ef5fc9a6a37a55ffdaae0aa59e41f3a32c5fb02cb26381f5a534e34d537a2cf7b5f3207b8baa92b7084f8b96a9f94
data/.travis.yml CHANGED
@@ -1,11 +1,9 @@
1
1
  language: ruby
2
- gemfile:
3
- - Gemfile.travis
4
2
  rvm:
5
3
  - 1.9.3
6
4
  - 2.0.0
7
5
  - 2.1.0
8
6
  - ruby-head
9
- - rbx
7
+ - rbx-2
10
8
  - jruby-head
11
9
  cache: bundler
data/README.md CHANGED
@@ -34,6 +34,17 @@ class Rectange
34
34
  @y = y
35
35
  end
36
36
 
37
+ def to_h
38
+ to_h
39
+ end
40
+
41
+ def to_hash
42
+ {
43
+ x: x,
44
+ y: y
45
+ }
46
+ end
47
+
37
48
  def area
38
49
  x * y
39
50
  end
@@ -42,6 +53,21 @@ end
42
53
 
43
54
  Since this is meant to create immutable objects, the values aren't actually assigned to instance variables but safed internally in a hash.
44
55
 
56
+ ### Changing behaviour
57
+
58
+ You can also choose to override behaviour. You can just use super in the initialization block like you are used to with normal classes:
59
+
60
+ ```ruby
61
+ Rectangle = StrictStruct.new(:x, :y) do
62
+ def area
63
+ x * y
64
+ end
65
+
66
+ def to_hash
67
+ super.merge({area: area})
68
+ end
69
+ end
70
+ ```
45
71
 
46
72
  ## Installation
47
73
 
data/lib/strict_struct.rb CHANGED
@@ -1,4 +1,4 @@
1
- #require "strict_struct/version"
1
+ require "strict_struct/version"
2
2
 
3
3
  module StrictStruct
4
4
  module Helper
@@ -19,40 +19,45 @@ module StrictStruct
19
19
  end
20
20
  end
21
21
 
22
- def self.new(*attributes, &block)
23
- mod = Module.new do
24
- define_method :initialize do |hash={}|
25
- Helper.validate_arguments(hash.keys, attributes)
26
- @_strict_struct_hash = Hash[hash.to_a].freeze
27
- end
22
+ class AttributeModule < Module
23
+ def initialize *attributes
24
+ super() do
25
+ define_method :initialize do |hash={}|
26
+ Helper.validate_arguments(hash.keys, attributes)
27
+ @_strict_struct_hash = Hash[hash.to_a].freeze
28
+ end
28
29
 
29
- attributes.each do |attribute|
30
- define_method(attribute) do
31
- @_strict_struct_hash[attribute]
30
+ attributes.each do |attribute|
31
+ define_method(attribute) do
32
+ @_strict_struct_hash[attribute]
33
+ end
32
34
  end
33
- end
34
35
 
35
- def to_h
36
- to_hash
37
- end
36
+ def to_h
37
+ to_hash
38
+ end
38
39
 
39
- def to_hash
40
- Hash[@_strict_struct_hash.to_a]
41
- end
40
+ def to_hash
41
+ Hash[@_strict_struct_hash.to_a]
42
+ end
42
43
 
43
- define_method :== do |other|
44
- return false unless self.class == other.class
44
+ define_method :== do |other|
45
+ return false unless self.class == other.class
45
46
 
46
- attributes.all? {|name| self.send(name) == other.send(name)}
47
- end
47
+ attributes.all? {|name| self.send(name) == other.send(name)}
48
+ end
48
49
 
49
- define_method :hash do
50
- @_strict_struct_hash.hash
50
+ define_method :hash do
51
+ @_strict_struct_hash.hash
52
+ end
51
53
  end
52
54
  end
53
- klass = Class.new
54
- klass.send :include, mod
55
- klass.class_eval(&block) if block_given?
56
- klass
55
+ end
56
+
57
+ def self.new(*attributes, &block)
58
+ Class.new do
59
+ include AttributeModule.new(*attributes)
60
+ class_eval(&block) if block_given?
61
+ end
57
62
  end
58
63
  end
@@ -1,3 +1,3 @@
1
1
  module StrictStruct
2
- VERSION = "0.0.4"
2
+ VERSION = "1.0.0"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,7 @@
1
1
  require 'rspec'
2
2
 
3
3
  $LOAD_PATH << './lib/'
4
+
5
+ RSpec.configure do |c|
6
+ c.raise_errors_for_deprecations!
7
+ end
@@ -8,7 +8,7 @@ describe 'StrictStruct' do
8
8
 
9
9
  a_foo = foo.new
10
10
 
11
- a_foo.should_not be_nil
11
+ expect(a_foo).to_not be_nil
12
12
  end
13
13
 
14
14
  it "works with parameters" do
@@ -16,7 +16,7 @@ describe 'StrictStruct' do
16
16
 
17
17
  a_foo = foo.new(bar: 'baz')
18
18
 
19
- a_foo.should_not be_nil
19
+ expect(a_foo).to_not be_nil
20
20
  end
21
21
 
22
22
  it "raises an error when an argument is missing" do
@@ -77,7 +77,7 @@ describe 'StrictStruct' do
77
77
 
78
78
  rectangle = rectangle_class.new(x: 3, y: 5)
79
79
 
80
- rectangle.area.should == 15
80
+ expect(rectangle.area).to eq 15
81
81
  end
82
82
  end
83
83
 
@@ -87,8 +87,8 @@ describe 'StrictStruct' do
87
87
 
88
88
  a_foo = foo.new(bar: 'baz')
89
89
 
90
- a_foo.bar.should eq 'baz'
91
- a_foo.should respond_to 'bar'
90
+ expect(a_foo.bar).to eq 'baz'
91
+ expect(a_foo).to respond_to 'bar'
92
92
  end
93
93
 
94
94
  it "is not defined when the value isn't defined by the class" do
@@ -96,7 +96,7 @@ describe 'StrictStruct' do
96
96
 
97
97
  a_foo = foo.new(bar: 'baz')
98
98
 
99
- a_foo.should_not respond_to 'gerard'
99
+ expect(a_foo).to_not respond_to 'gerard'
100
100
  end
101
101
 
102
102
  it "raises a NoMethodError when called for a value which isn't defined" do
@@ -117,7 +117,7 @@ describe 'StrictStruct' do
117
117
  k = klass.new(params)
118
118
  params[:x] = 'bar'
119
119
 
120
- k.x.should eq 'foo'
120
+ expect(k.x).to eq 'foo'
121
121
  end
122
122
 
123
123
  it "isn't broken by modifying output of to_h" do
@@ -125,7 +125,7 @@ describe 'StrictStruct' do
125
125
  hash = k.to_h
126
126
  hash[:x] = 'bar'
127
127
 
128
- k.x.should eq 'foo'
128
+ expect(k.x).to eq 'foo'
129
129
  end
130
130
  end
131
131
 
@@ -135,7 +135,7 @@ describe 'StrictStruct' do
135
135
 
136
136
  a_foo = foo.new(bar: 'baz', baz: 3)
137
137
 
138
- a_foo.to_h.should eq({bar: 'baz', baz: 3})
138
+ expect(a_foo.to_h).to eq({ bar: 'baz', baz: 3 })
139
139
  end
140
140
 
141
141
  it "is possible to override, and reuse using super" do
@@ -147,7 +147,7 @@ describe 'StrictStruct' do
147
147
 
148
148
  a_foo = foo.new(bar: 'baz', baz: 3)
149
149
 
150
- a_foo.to_h.should eq({bar: 'baz', baz: 3, pirate: 'captain'})
150
+ expect(a_foo.to_h).to eq({ bar: 'baz', baz: 3, pirate: 'captain' })
151
151
  end
152
152
  end
153
153
 
@@ -157,7 +157,7 @@ describe 'StrictStruct' do
157
157
 
158
158
  a_foo = foo.new(bar: 'baz', baz: 3)
159
159
 
160
- a_foo.to_hash.should eq({bar: 'baz', baz: 3})
160
+ expect(a_foo.to_hash).to eq({ bar: 'baz', baz: 3 })
161
161
  end
162
162
  end
163
163
 
@@ -171,8 +171,8 @@ describe 'StrictStruct' do
171
171
 
172
172
  a_foo = foo.new(bar: 'baz', baz: 3)
173
173
 
174
- a_foo.to_h.should eq({bar: 'baz', baz: 3, pirate: 'captain'})
175
- a_foo.to_hash.should eq({bar: 'baz', baz: 3})
174
+ expect(a_foo.to_h).to eq({ bar: 'baz', baz: 3, pirate: 'captain' })
175
+ expect(a_foo.to_hash).to eq({ bar: 'baz', baz: 3 })
176
176
  end
177
177
  end
178
178
 
@@ -186,8 +186,8 @@ describe 'StrictStruct' do
186
186
 
187
187
  a_foo = foo.new(bar: 'baz', baz: 3)
188
188
 
189
- a_foo.to_h.should eq({bar: 'baz', baz: 3, pirate: 'captain'})
190
- a_foo.to_hash.should eq({bar: 'baz', baz: 3, pirate: 'captain'})
189
+ expect(a_foo.to_h).to eq({ bar: 'baz', baz: 3, pirate: 'captain' })
190
+ expect(a_foo.to_hash).to eq({ bar: 'baz', baz: 3, pirate: 'captain' })
191
191
  end
192
192
  end
193
193
 
@@ -205,9 +205,8 @@ describe 'StrictStruct' do
205
205
 
206
206
  a_foo = foo.new(bar: 'baz', baz: 3)
207
207
 
208
- a_foo.to_h.should eq({bar: 'baz', baz: 3, pirate: 'captain', samurai: 'sword'})
209
- a_foo.to_hash.should eq({bar: 'baz', baz: 3, pirate: 'captain'})
210
-
208
+ expect(a_foo.to_h).to eq({ bar: 'baz', baz: 3, pirate: 'captain', samurai: 'sword' })
209
+ expect(a_foo.to_hash).to eq({ bar: 'baz', baz: 3, pirate: 'captain' })
211
210
  end
212
211
  end
213
212
 
@@ -218,7 +217,7 @@ describe 'StrictStruct' do
218
217
  a_foo = foo.new(bar: 'baz')
219
218
  another_foo = foo.new(bar: 'baz')
220
219
 
221
- a_foo.should eq another_foo
220
+ expect(a_foo).to eq another_foo
222
221
  end
223
222
 
224
223
  it "returns false if the values are not the same" do
@@ -227,7 +226,7 @@ describe 'StrictStruct' do
227
226
  a_foo = foo.new(bar: 'baz')
228
227
  another_foo = foo.new(bar: 'not_baz')
229
228
 
230
- a_foo.should_not eq another_foo
229
+ expect(a_foo).to_not eq another_foo
231
230
  end
232
231
 
233
232
  it "returns false if two values are different StrictStructs" do
@@ -237,14 +236,14 @@ describe 'StrictStruct' do
237
236
  chicken = chicken_class.new(wings: 2)
238
237
  plane = plane_class.new(wings: 2)
239
238
 
240
- plane.should_not eq chicken
239
+ expect(plane).to_not eq chicken
241
240
  end
242
241
  end
243
242
 
244
243
  describe '#hash' do
245
244
  it "returns the same value for two empty objects" do
246
245
  empty = StrictStruct.new
247
- empty.new.hash.should == empty.new.hash
246
+ expect(empty.new.hash).to eq empty.new.hash
248
247
  end
249
248
 
250
249
  it "returns a different value for two non-empty objects with different content" do
@@ -253,7 +252,7 @@ describe 'StrictStruct' do
253
252
  first = container.new(a: 1)
254
253
  second = container.new(a: 2)
255
254
 
256
- first.hash.should_not == second.hash
255
+ expect(first.hash).to_not eq second.hash
257
256
  end
258
257
 
259
258
  it "returns the same value for two non-empty objects with the same content" do
@@ -262,10 +261,16 @@ describe 'StrictStruct' do
262
261
  first = container.new(a: 1)
263
262
  second = container.new(a: 1)
264
263
 
265
- first.hash.should == second.hash
264
+ expect(first.hash).to eq second.hash
266
265
  end
267
266
 
268
- # we need no guarantee that objects with the same content, but different classes
269
- # return different hashes
267
+ it "returns a different value for two different objects" do |variable|
268
+ container = StrictStruct.new(:a)
269
+
270
+ first = container.new(a: 1)
271
+ second = container.new(a: 2)
272
+
273
+ expect(first.hash).to_not eq second.hash
274
+ end
270
275
  end
271
276
  end
@@ -20,5 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "rspec", '3.0.0.beta2'
24
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strict_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark IJbema
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-15 00:00:00.000000000 Z
11
+ date: 2014-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -42,16 +42,16 @@ dependencies:
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - '='
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 3.0.0.beta2
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - '='
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 3.0.0.beta2
55
55
  description: Extremely simple value objects
56
56
  email:
57
57
  - markijbema@gmail.com
@@ -62,7 +62,6 @@ files:
62
62
  - ".gitignore"
63
63
  - ".travis.yml"
64
64
  - Gemfile
65
- - Gemfile.travis
66
65
  - LICENSE.txt
67
66
  - README.md
68
67
  - Rakefile
data/Gemfile.travis DELETED
@@ -1,7 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- platforms :rbx do
4
- gem 'rubysl', '~> 2.0'
5
- end
6
-
7
- eval_gemfile File.expand_path('../Gemfile', __FILE__)