memory_record 0.0.3 → 0.0.4
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.
- checksums.yaml +4 -4
- data/README.org +19 -0
- data/examples/0240_attr_reader_option.rb +41 -0
- data/lib/memory_record/memory_record.rb +0 -1
- data/lib/memory_record/version.rb +1 -1
- data/spec/memory_record_spec.rb +47 -7
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42384e2c065906f318f6b46cf58200e3d2b72f28
|
4
|
+
data.tar.gz: 1caef9514c4b62b8807f19fe87ccb7c97c7b8250
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1834685035a93dbb9606798c0837ce07ec2a61ede9f6dfb6b7de16b5fc807efbb8e713ac3918d2b8b99fd08647e14eedd370caa010e3c4224d404e02fbdf0f65
|
7
|
+
data.tar.gz: fdc248682afc325efbac0c660aa3ca2c5439f30dc6a61785cb6b4d2141c402b0ddb26ff02d4c1aca6338d5b6199d75bd5ee4b45a0020d96672a60ee0fc3e70a4
|
data/README.org
CHANGED
@@ -126,3 +126,22 @@ Foo.fetch_if(nil) # => nil
|
|
126
126
|
Foo.fetch_if(:a) # => #<Foo:... @attributes={...}>
|
127
127
|
Foo.fetch_if(:xxx) # => <KeyError: ...>
|
128
128
|
#+END_SRC
|
129
|
+
|
130
|
+
*** How can I prohibit the hash key from being attr_reader automatically?
|
131
|
+
|
132
|
+
I think that it is better to use it when you want to make it difficult to access easily.
|
133
|
+
|
134
|
+
#+BEGIN_SRC ruby
|
135
|
+
class Foo
|
136
|
+
include MemoryRecord
|
137
|
+
memory_record attr_reader: false do
|
138
|
+
[
|
139
|
+
{x: 1},
|
140
|
+
]
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
Foo.first.x rescue $! # => #<NoMethodError: undefined method `x' for #<Foo:0x007fb070a5d4d0>>
|
145
|
+
Foo.first[:x] # => 1
|
146
|
+
Foo.first.attributes[:x] # => 1
|
147
|
+
#+END_SRC
|
@@ -0,0 +1,41 @@
|
|
1
|
+
$LOAD_PATH.unshift '../lib'
|
2
|
+
require 'memory_record'
|
3
|
+
|
4
|
+
class C1
|
5
|
+
include MemoryRecord
|
6
|
+
memory_record attr_reader: false do
|
7
|
+
[
|
8
|
+
{x: 1, y: 1, z: 1},
|
9
|
+
]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
C1.first.x rescue $! # => #<NoMethodError: undefined method `x' for #<C1:0x007fb2c710eda8>>
|
14
|
+
C1.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<C1:0x007fb2c710eda8>>
|
15
|
+
C1.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<C1:0x007fb2c710eda8>>
|
16
|
+
|
17
|
+
class C2
|
18
|
+
include MemoryRecord
|
19
|
+
memory_record attr_reader: {only: :x} do
|
20
|
+
[
|
21
|
+
{x: 1, y: 1, z: 1},
|
22
|
+
]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
C2.first.x rescue $! # => 1
|
27
|
+
C2.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<C2:0x007fb2c70e6df8>>
|
28
|
+
C2.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<C2:0x007fb2c70e6df8>>
|
29
|
+
|
30
|
+
class C3
|
31
|
+
include MemoryRecord
|
32
|
+
memory_record attr_reader: {except: :x} do
|
33
|
+
[
|
34
|
+
{x: 1, y: 1, z: 1},
|
35
|
+
]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
C3.first.x rescue $! # => #<NoMethodError: undefined method `x' for #<C3:0x007fb2c70ce668>>
|
40
|
+
C3.first.y rescue $! # => 1
|
41
|
+
C3.first.z rescue $! # => 1
|
data/spec/memory_record_spec.rb
CHANGED
@@ -17,10 +17,10 @@ class Legacy
|
|
17
17
|
end
|
18
18
|
|
19
19
|
RSpec.describe MemoryRecord do
|
20
|
-
def
|
20
|
+
def class_new(table)
|
21
21
|
Class.new {
|
22
22
|
include MemoryRecord
|
23
|
-
memory_record table
|
23
|
+
memory_record table
|
24
24
|
}
|
25
25
|
end
|
26
26
|
|
@@ -81,7 +81,7 @@ RSpec.describe MemoryRecord do
|
|
81
81
|
|
82
82
|
context 'Re-set' do
|
83
83
|
before do
|
84
|
-
@model =
|
84
|
+
@model = class_new [{key: :a}]
|
85
85
|
@model.memory_record_reset [{key: :b}, {key: :c}]
|
86
86
|
end
|
87
87
|
it 'changed' do
|
@@ -92,18 +92,18 @@ RSpec.describe MemoryRecord do
|
|
92
92
|
|
93
93
|
context 'Subtle specifications' do
|
94
94
|
it 'When keys are specified as an array, they become symbols with underscores' do
|
95
|
-
model =
|
95
|
+
model = class_new [{key: [:id, :desc]}]
|
96
96
|
assert_equal [:id_desc], model.keys
|
97
97
|
end
|
98
98
|
|
99
99
|
it 'Name method is automatically defined if it is not defined' do
|
100
|
-
model =
|
100
|
+
model = class_new []
|
101
101
|
assert_equal true, model.instance_methods.include?(:name)
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
105
105
|
it 'Japanese can be used for key' do
|
106
|
-
model =
|
106
|
+
model = class_new [{key: 'あ'}]
|
107
107
|
assert model['あ']
|
108
108
|
end
|
109
109
|
|
@@ -155,10 +155,50 @@ RSpec.describe MemoryRecord do
|
|
155
155
|
end
|
156
156
|
|
157
157
|
describe 'Human_attribute_name can not be used in anonymous class' do
|
158
|
-
let(:model) {
|
158
|
+
let(:model) { class_new [{foo: 1}] }
|
159
159
|
|
160
160
|
it 'It does not cause an error' do
|
161
161
|
model.first.name.should == nil
|
162
162
|
end
|
163
163
|
end
|
164
|
+
|
165
|
+
describe 'attr_reader option' do
|
166
|
+
def element(options)
|
167
|
+
Class.new {
|
168
|
+
include MemoryRecord
|
169
|
+
memory_record options do
|
170
|
+
[
|
171
|
+
{x: 1, y: 1, z: 1},
|
172
|
+
]
|
173
|
+
end
|
174
|
+
}.first
|
175
|
+
end
|
176
|
+
|
177
|
+
context 'false' do
|
178
|
+
subject { element(attr_reader: false) }
|
179
|
+
it do
|
180
|
+
assert_equal false, subject.respond_to?(:x)
|
181
|
+
assert_equal false, subject.respond_to?(:y)
|
182
|
+
assert_equal false, subject.respond_to?(:z)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
context 'only' do
|
187
|
+
subject { element(attr_reader: {only: [:x, :z]}) }
|
188
|
+
it do
|
189
|
+
assert_equal true, subject.respond_to?(:x)
|
190
|
+
assert_equal false, subject.respond_to?(:y)
|
191
|
+
assert_equal true, subject.respond_to?(:z)
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
context 'except' do
|
196
|
+
subject { element(attr_reader: {except: :y}) }
|
197
|
+
it do
|
198
|
+
assert_equal true, subject.respond_to?(:x)
|
199
|
+
assert_equal false, subject.respond_to?(:y)
|
200
|
+
assert_equal true, subject.respond_to?(:z)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
164
204
|
end
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- akicho8
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -108,6 +108,7 @@ files:
|
|
108
108
|
- examples/0210_key_duplicate.rb
|
109
109
|
- examples/0220_when_key_like_numeric.rb
|
110
110
|
- examples/0230_with_use_activerecord_enum.rb
|
111
|
+
- examples/0240_attr_reader_option.rb
|
111
112
|
- lib/memory_record.rb
|
112
113
|
- lib/memory_record/memory_record.rb
|
113
114
|
- lib/memory_record/version.rb
|