memory_record 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -7
- data/examples/0320_as_json.rb +3 -0
- data/examples/0330_comparable.rb +14 -0
- data/lib/memory_record/memory_record.rb +2 -6
- data/lib/memory_record/version.rb +1 -1
- data/spec/memory_record_spec.rb +83 -63
- data/spec/spec_helper.rb +0 -3
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 64b86bd76f59f5da1e0e4111208b5fdb868df86865239e998c9d4de5796f43e6
|
4
|
+
data.tar.gz: 405a3da00872ef9c104ac6208c225eb808b7329443be89cc1ee3328b3aec0a5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f243e8cf16808efda1c6fab438c1631982d9d825d1dd760f5bc3c1741ee30d1ca01ec1e79a5259e6f2757cef8153d6823e8360ee4df09fa2644745c4ac6bb69
|
7
|
+
data.tar.gz: 51f7d2216b5aef88194fd7d9c76362babab8800193c85b7bb511d6aa6d4cbda004e7992891d77113101e86e04e02f868b22831d41e28c17e43ca817d5bb89912
|
data/README.md
CHANGED
@@ -2,7 +2,8 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/akicho8/memory_record.svg?branch=master)](https://travis-ci.org/akicho8/memory_record)
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/memory_record.svg)](https://badge.fury.io/rb/memory_record)
|
5
|
-
|
5
|
+
|
6
|
+
<!-- [![Dependency Status](https://gemnasium.com/badges/github.com/akicho8/memory_record.svg)](https://gemnasium.com/github.com/akicho8/memory_record) -->
|
6
7
|
|
7
8
|
## Introduction
|
8
9
|
|
@@ -206,14 +207,18 @@ class ColorInfo
|
|
206
207
|
|
207
208
|
def children
|
208
209
|
[
|
209
|
-
{
|
210
|
-
{
|
210
|
+
{x: 1, y: 2},
|
211
|
+
{x: 3, y: 4},
|
211
212
|
]
|
212
213
|
end
|
213
214
|
end
|
214
215
|
|
215
|
-
ColorInfo.first
|
216
|
-
|
217
|
-
|
218
|
-
|
216
|
+
color_info = ColorInfo.first
|
217
|
+
color_info.as_json(only: :key) # => {:key => :blue}
|
218
|
+
color_info.as_json(except: [:rgb, :code]) # => {:key => :blue}
|
219
|
+
color_info.as_json(only: [], methods: :hex) # => {:hex => "#0000FF"}
|
220
|
+
color_info.as_json(only: [], include: {children: {only: :x}} ) # => {:children => [{"x" => 1}, {"x" => 3}]}
|
221
|
+
|
222
|
+
ColorInfo.as_json(only: :key) # => [{:key=>:blue}, {:key=>:red}]
|
223
|
+
ColorInfo.to_json(only: :key) # => "[{\"key\":\"blue\"},{\"key\":\"red\"}]"
|
219
224
|
```
|
data/examples/0320_as_json.rb
CHANGED
@@ -40,3 +40,6 @@ ColorInfo.first.as_json(only: :key) # => {:key=>
|
|
40
40
|
ColorInfo.first.as_json(except: [:rgb, :code, :a]) # => {:key=>:blue}
|
41
41
|
ColorInfo.first.as_json(only: [], methods: :hex) # => {:hex=>"#0000FF"}
|
42
42
|
ColorInfo.first.as_json(only: [], include: {children: {only: :a}} ) # => {:children=>[{"a"=>1}, {"a"=>1}, {:a=>1}]}
|
43
|
+
|
44
|
+
ColorInfo.as_json(only: :key) # => [{:key=>:blue}, {:key=>:red}]
|
45
|
+
ColorInfo.to_json(only: :key) # => "[{\"key\":\"blue\"},{\"key\":\"red\"}]"
|
@@ -36,6 +36,7 @@ module MemoryRecord
|
|
36
36
|
end
|
37
37
|
|
38
38
|
extend Enumerable
|
39
|
+
include Comparable
|
39
40
|
include ::MemoryRecord::SingletonMethods
|
40
41
|
include ::MemoryRecord::Serialization
|
41
42
|
|
@@ -76,16 +77,11 @@ module MemoryRecord
|
|
76
77
|
define_method(:name) { key.to_s }
|
77
78
|
end
|
78
79
|
|
79
|
-
# sort matches definition order
|
80
|
+
# sort matches definition order. Comparable module methods will be available.
|
80
81
|
def <=>(other)
|
81
82
|
[self.class, code] <=> [other.class, other.code]
|
82
83
|
end
|
83
84
|
|
84
|
-
# Even if duplicate, objects match
|
85
|
-
def ==(other)
|
86
|
-
self.class == other.class && key == other.key
|
87
|
-
end
|
88
|
-
|
89
85
|
# Even if object_id of objects used as hash keys are different, they match. It also speeds up by defining hash.
|
90
86
|
def eql?(other)
|
91
87
|
self.class == other.class && key == other.key
|
data/spec/memory_record_spec.rb
CHANGED
@@ -1,22 +1,30 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
RSpec.describe MemoryRecord do
|
4
|
+
class Model5
|
5
|
+
include MemoryRecord
|
6
|
+
memory_record [
|
7
|
+
{ key: :a },
|
8
|
+
{ key: :b },
|
9
|
+
]
|
10
|
+
end
|
10
11
|
|
11
|
-
class
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
12
|
+
class Model
|
13
|
+
include MemoryRecord
|
14
|
+
memory_record [
|
15
|
+
{ name: 'A' },
|
16
|
+
{ name: 'B' },
|
17
|
+
], attr_reader: :name
|
18
|
+
end
|
19
|
+
|
20
|
+
class Legacy
|
21
|
+
include MemoryRecord
|
22
|
+
memory_record [
|
23
|
+
{ code: 10, key: :a, name: 'A', },
|
24
|
+
{ code: 20, key: :b, name: 'B', },
|
25
|
+
], attr_reader: :name
|
26
|
+
end
|
18
27
|
|
19
|
-
RSpec.describe MemoryRecord do
|
20
28
|
def class_new(table)
|
21
29
|
Class.new {
|
22
30
|
include MemoryRecord
|
@@ -26,21 +34,21 @@ RSpec.describe MemoryRecord do
|
|
26
34
|
|
27
35
|
context 'Useful Class Methods' do
|
28
36
|
it 'each' do
|
29
|
-
assert Model.each
|
37
|
+
assert { Model.each }
|
30
38
|
end
|
31
39
|
|
32
40
|
it 'keys' do
|
33
|
-
|
41
|
+
assert { [:_key0, :_key1] == Model.keys }
|
34
42
|
end
|
35
43
|
|
36
44
|
it 'values' do
|
37
|
-
|
45
|
+
assert { Model.each.to_a == Model.values }
|
38
46
|
end
|
39
47
|
end
|
40
48
|
|
41
49
|
context 'Subscript access to class' do
|
42
50
|
it 'When code and key are automatically waved' do
|
43
|
-
|
51
|
+
assert { 'A' == Model[0].name }
|
44
52
|
end
|
45
53
|
|
46
54
|
it 'It will not cause an error even if there is no corresponding key' do
|
@@ -58,23 +66,23 @@ RSpec.describe MemoryRecord do
|
|
58
66
|
|
59
67
|
context 'Subscript access to instance' do
|
60
68
|
it do
|
61
|
-
Model.first[:name]
|
62
|
-
Model.first[:xxxx]
|
69
|
+
assert { Model.first[:name] == 'A' }
|
70
|
+
assert { Model.first[:xxxx] == nil }
|
63
71
|
end
|
64
72
|
end
|
65
73
|
|
66
74
|
context 'to_s' do
|
67
75
|
it do
|
68
|
-
Model.first.to_s
|
76
|
+
assert { Model.first.to_s == 'A' }
|
69
77
|
end
|
70
78
|
end
|
71
79
|
|
72
80
|
context 'instance accessor' do
|
73
81
|
it do
|
74
|
-
assert Model.first.to_h
|
75
|
-
assert Model.first.attributes
|
76
|
-
assert Model.first.key
|
77
|
-
assert Model.first.code
|
82
|
+
assert { Model.first.to_h }
|
83
|
+
assert { Model.first.attributes }
|
84
|
+
assert { Model.first.key }
|
85
|
+
assert { Model.first.code }
|
78
86
|
end
|
79
87
|
end
|
80
88
|
|
@@ -84,35 +92,35 @@ RSpec.describe MemoryRecord do
|
|
84
92
|
@model.memory_record_reset [{key: :b}, {key: :c}]
|
85
93
|
end
|
86
94
|
it 'changed' do
|
87
|
-
|
88
|
-
|
95
|
+
assert { [:b, :c] == @model.keys }
|
96
|
+
assert { [0, 1] == @model.codes }
|
89
97
|
end
|
90
98
|
end
|
91
99
|
|
92
100
|
context 'Subtle specifications' do
|
93
101
|
it 'When keys are specified as an array, they become symbols with underscores' do
|
94
102
|
model = class_new [{key: [:id, :desc]}]
|
95
|
-
|
103
|
+
assert { [:id_desc] == model.keys }
|
96
104
|
end
|
97
105
|
|
98
106
|
it 'Name method is automatically defined if it is not defined' do
|
99
107
|
model = class_new [{key: :foo}]
|
100
|
-
|
101
|
-
|
108
|
+
assert { true == model.instance_methods.include?(:name) }
|
109
|
+
assert { "foo" == model[:foo].name }
|
102
110
|
end
|
103
111
|
end
|
104
112
|
|
105
113
|
it 'Japanese can be used for key' do
|
106
114
|
model = class_new [{key: 'あ'}]
|
107
|
-
assert model['あ']
|
115
|
+
assert { model['あ'] }
|
108
116
|
end
|
109
117
|
|
110
118
|
it 'When you define code and keys yourself' do
|
111
|
-
|
119
|
+
assert { 'A' == Legacy[10].name }
|
112
120
|
end
|
113
121
|
|
114
122
|
it 'We do not freeze values because memoization becomes impossible' do
|
115
|
-
Model.first.name.upcase!
|
123
|
+
assert_nothing_raised { Model.first.name.upcase! }
|
116
124
|
end
|
117
125
|
|
118
126
|
describe 'super' do
|
@@ -128,7 +136,7 @@ RSpec.describe MemoryRecord do
|
|
128
136
|
end
|
129
137
|
|
130
138
|
it 'Since methods are defined in ancestry, you can use super' do
|
131
|
-
|
139
|
+
assert { 'xy' == Model2.first.var }
|
132
140
|
end
|
133
141
|
end
|
134
142
|
|
@@ -142,15 +150,15 @@ RSpec.describe MemoryRecord do
|
|
142
150
|
end
|
143
151
|
|
144
152
|
it do
|
145
|
-
|
146
|
-
|
153
|
+
assert { 1 == Model3.first.a }
|
154
|
+
assert { nil == Model3.first.b }
|
147
155
|
end
|
148
156
|
end
|
149
157
|
|
150
158
|
describe 'Do not duplicate key and code' do
|
151
159
|
it do
|
152
|
-
|
153
|
-
|
160
|
+
assert_raises(ArgumentError) { Model.memory_record_reset([{key: :a}, {key: :a}]) }
|
161
|
+
assert_raises(ArgumentError) { Model.memory_record_reset([{code: 0}, {code: 0}]) }
|
154
162
|
end
|
155
163
|
end
|
156
164
|
|
@@ -169,27 +177,27 @@ RSpec.describe MemoryRecord do
|
|
169
177
|
context 'false' do
|
170
178
|
subject { element(attr_reader: false) }
|
171
179
|
it do
|
172
|
-
|
173
|
-
|
174
|
-
|
180
|
+
assert { subject.respond_to?(:x) == false }
|
181
|
+
assert { subject.respond_to?(:y) == false }
|
182
|
+
assert { subject.respond_to?(:z) == false }
|
175
183
|
end
|
176
184
|
end
|
177
185
|
|
178
186
|
context 'only' do
|
179
187
|
subject { element(attr_reader: {only: [:x, :z]}) }
|
180
188
|
it do
|
181
|
-
|
182
|
-
|
183
|
-
|
189
|
+
assert { subject.respond_to?(:x) == true }
|
190
|
+
assert { subject.respond_to?(:y) == false }
|
191
|
+
assert { subject.respond_to?(:z) == true }
|
184
192
|
end
|
185
193
|
end
|
186
194
|
|
187
195
|
context 'except' do
|
188
196
|
subject { element(attr_reader: {except: :y}) }
|
189
197
|
it do
|
190
|
-
|
191
|
-
|
192
|
-
|
198
|
+
assert { subject.respond_to?(:x) == true }
|
199
|
+
assert { subject.respond_to?(:y) == false }
|
200
|
+
assert { subject.respond_to?(:z) == true }
|
193
201
|
end
|
194
202
|
end
|
195
203
|
end
|
@@ -201,39 +209,50 @@ RSpec.describe MemoryRecord do
|
|
201
209
|
super.key
|
202
210
|
end
|
203
211
|
end
|
204
|
-
model[:a]
|
212
|
+
assert { model[:a] == :a }
|
205
213
|
end
|
206
214
|
|
207
215
|
it "minus" do
|
208
216
|
m = class_new [{key: :a}, {key: :b}]
|
209
|
-
([m[:a], m[:b]] - [m[:a]])
|
217
|
+
assert { ([m[:a], m[:b]] - [m[:a]]) == [m[:b]] }
|
210
218
|
end
|
211
219
|
|
212
220
|
describe "sort (<=> method)" do
|
213
221
|
it "same class" do
|
214
222
|
m = class_new [{key: :a}, {key: :b}]
|
215
|
-
[m[:b], m[:a]].sort
|
223
|
+
assert { [m[:b], m[:a]].sort == [m[:a], m[:b]] }
|
216
224
|
end
|
217
225
|
|
218
226
|
it "different class" do
|
219
227
|
m1 = class_new [{key: :a}, {key: :b}]
|
220
228
|
m2 = class_new [{key: :a}, {key: :b}]
|
221
|
-
|
229
|
+
assert_raises(ArgumentError) { [m1[:b], m2[:a]].sort }
|
222
230
|
end
|
223
231
|
end
|
224
232
|
|
225
|
-
it "==" do
|
226
|
-
a = Model.fetch(:_key0)
|
227
|
-
b = Marshal.load(Marshal.dump(a))
|
228
|
-
(a == b).should == true
|
229
|
-
end
|
230
|
-
|
231
233
|
it "eql?, hash" do
|
232
234
|
a = Model.fetch(:_key0)
|
233
235
|
b = Marshal.load(Marshal.dump(a))
|
234
236
|
h = {}
|
235
237
|
h[a] = true
|
236
|
-
h[b]
|
238
|
+
assert { h[b] == true }
|
239
|
+
end
|
240
|
+
|
241
|
+
describe "Comparable operator" do
|
242
|
+
it do
|
243
|
+
model = class_new [
|
244
|
+
{ key: :a },
|
245
|
+
{ key: :b },
|
246
|
+
]
|
247
|
+
assert { model[:a] < model[:b] }
|
248
|
+
assert { model[:a] == model[:a] }
|
249
|
+
end
|
250
|
+
|
251
|
+
it "==" do
|
252
|
+
a = Model5.first
|
253
|
+
b = Marshal.load(Marshal.dump(a))
|
254
|
+
assert { a == b }
|
255
|
+
end
|
237
256
|
end
|
238
257
|
|
239
258
|
context 'as_json' do
|
@@ -271,10 +290,11 @@ RSpec.describe MemoryRecord do
|
|
271
290
|
end
|
272
291
|
|
273
292
|
it "as_json(options)" do
|
274
|
-
ColorInfo.first.as_json(only: :key)
|
275
|
-
ColorInfo.first.as_json(except: [:rgb, :code, :a])
|
276
|
-
ColorInfo.first.as_json(only: [], methods: :hex)
|
277
|
-
ColorInfo.first.as_json(only: [], include: {children: {only: :a}}
|
293
|
+
assert { ColorInfo.first.as_json(only: :key) == {:key => :blue} }
|
294
|
+
assert { ColorInfo.first.as_json(except: [:rgb, :code, :a]) == {:key => :blue} }
|
295
|
+
assert { ColorInfo.first.as_json(only: [], methods: :hex) == {:hex => "#0000FF"} }
|
296
|
+
assert { ColorInfo.first.as_json(only: [], include: {children: {only: :a}}) == {:children => [{"a" => 1}, {"a" => 1}, {:a => 1}]} }
|
297
|
+
assert { ColorInfo.as_json(only: :key) == [{:key => :blue}, {:key => :red}] }
|
278
298
|
end
|
279
299
|
end
|
280
300
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: memory_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- akicho8
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -117,6 +117,7 @@ files:
|
|
117
117
|
- examples/0300_use_as_hash_key.rb
|
118
118
|
- examples/0310_same_if_dup.rb
|
119
119
|
- examples/0320_as_json.rb
|
120
|
+
- examples/0330_comparable.rb
|
120
121
|
- lib/memory_record.rb
|
121
122
|
- lib/memory_record/memory_record.rb
|
122
123
|
- lib/memory_record/memory_record/serialization.rb
|
@@ -144,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
144
145
|
version: '0'
|
145
146
|
requirements: []
|
146
147
|
rubyforge_project:
|
147
|
-
rubygems_version: 2.7.
|
148
|
+
rubygems_version: 2.7.6
|
148
149
|
signing_key:
|
149
150
|
specification_version: 4
|
150
151
|
summary: A simple library that handles a few records easily
|