memory_record 0.0.9 → 0.0.10
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 → README.md} +66 -60
- data/examples/0300_use_as_hash_key.rb +16 -0
- data/examples/0310_same_if_dup.rb +13 -0
- data/lib/memory_record/memory_record.rb +15 -0
- data/lib/memory_record/version.rb +1 -1
- data/spec/memory_record_spec.rb +14 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2c8085a6c6fdc528864f2ba6ea2314864ee756ec70e3c0f084aefe284e2bdc33
|
4
|
+
data.tar.gz: aa03716f97fc13275339ca8a7432b4b1a298465c91e09848141c0762e0fea563
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff89e669f37fa84e06fa8cf71ed23b3116018cf5608212eadb5707640244dd800bc5f0bb1d89a227db055cb2cf608c355b4a677f853c37f275ec9f92d32ee2d6
|
7
|
+
data.tar.gz: ab3c487124bcd4795b517ee2e9cef6d4db37e10d2c6ac7668930efaae422223495a5c2332c579ffb9a6f9c11b0a56a5a81aa5fb79b19b0c266c96469fd2fec9d
|
data/{README.org → README.md}
RENAMED
@@ -1,28 +1,34 @@
|
|
1
|
-
|
1
|
+
# MemoryRecord
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
[](https://travis-ci.org/akicho8/memory_record)
|
4
|
+
[](https://badge.fury.io/rb/memory_record)
|
5
|
+
[](https://gemnasium.com/github.com/akicho8/memory_record)
|
5
6
|
|
6
|
-
|
7
|
+
## Introduction
|
7
8
|
|
8
|
-
|
9
|
+
A simple library that handles a few records easily.
|
10
|
+
With this library can flexibly managed immutable data.
|
9
11
|
|
10
|
-
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Install as a standalone gem
|
15
|
+
|
16
|
+
```shell
|
11
17
|
$ gem install memory_record
|
12
|
-
|
18
|
+
```
|
13
19
|
|
14
20
|
Or install within application using Gemfile
|
15
21
|
|
16
|
-
|
22
|
+
```shell
|
17
23
|
$ bundle add memory_record
|
18
24
|
$ bundle install
|
19
|
-
|
25
|
+
```
|
20
26
|
|
21
|
-
|
27
|
+
## Examples
|
22
28
|
|
23
|
-
|
29
|
+
### Basic usage
|
24
30
|
|
25
|
-
|
31
|
+
```ruby
|
26
32
|
class Language
|
27
33
|
include MemoryRecord
|
28
34
|
memory_record [
|
@@ -43,77 +49,77 @@ Language[:ruby].mr_author # => "Mr. Yukihiro Matsumoto"
|
|
43
49
|
|
44
50
|
Language.keys # => [:lisp, :c, :ruby]
|
45
51
|
Language.collect(&:author) # => ["John McCarthy", "Dennis Ritchie", "Yukihiro Matsumoto"]
|
46
|
-
|
52
|
+
```
|
47
53
|
|
48
|
-
|
54
|
+
### How to turn as an array?
|
49
55
|
|
50
|
-
|
56
|
+
**Enumerable** extended, so that **each** method is available
|
51
57
|
|
52
|
-
|
58
|
+
```ruby
|
53
59
|
Foo.each { |e| ... }
|
54
60
|
Foo.collect { |e| ... }
|
55
|
-
|
61
|
+
```
|
56
62
|
|
57
|
-
|
63
|
+
### How do I submit a form to select in Rails?
|
58
64
|
|
59
|
-
|
65
|
+
```ruby
|
60
66
|
form.collection_select(:selection_code, Foo, :code, :name)
|
61
|
-
|
67
|
+
```
|
62
68
|
|
63
|
-
|
69
|
+
### Is the reference in subscripts slow?
|
64
70
|
|
65
|
-
|
71
|
+
Since it has a hash internally using the key value as a key, it can be acquired with O (1).
|
66
72
|
|
67
|
-
|
73
|
+
```ruby
|
68
74
|
Foo[1].name # => "A"
|
69
75
|
Foo[:a].name # => "A"
|
70
|
-
|
76
|
+
```
|
71
77
|
|
72
|
-
|
78
|
+
### Instances always react to **code** and **key**
|
73
79
|
|
74
|
-
|
80
|
+
```ruby
|
75
81
|
object = Foo.first
|
76
82
|
object.key # => :a
|
77
83
|
object.code # => 1
|
78
|
-
|
84
|
+
```
|
79
85
|
|
80
|
-
|
86
|
+
### How do I add a method to an instance?
|
81
87
|
|
82
|
-
|
88
|
+
For that, I am creating a new class so I need to define it normally
|
83
89
|
|
84
|
-
|
90
|
+
### **name** method is special?
|
85
91
|
|
86
|
-
|
92
|
+
If **name** is not defined, it defines a **name** method that returns **key.to_s**
|
87
93
|
|
88
|
-
|
94
|
+
### **to_s** method is defined?
|
89
95
|
|
90
|
-
|
96
|
+
Alias of **name**, **to_s** is defined.
|
91
97
|
|
92
|
-
|
98
|
+
### If there is no key, use fetch to get an error
|
93
99
|
|
94
|
-
|
100
|
+
```ruby
|
95
101
|
Foo.fetch(:xxx) # => <KeyError: ...>
|
96
|
-
|
102
|
+
```
|
97
103
|
|
98
|
-
|
104
|
+
The following are all the same
|
99
105
|
|
100
|
-
|
106
|
+
```ruby
|
101
107
|
Foo[:xxx] || :default # => :default
|
102
108
|
Foo.fetch(:xxx, :default} # => :default
|
103
109
|
Foo.fetch(:xxx) { :default } # => :default
|
104
|
-
|
110
|
+
```
|
105
111
|
|
106
|
-
|
112
|
+
### Use fetch_if to ignore if the key is nil
|
107
113
|
|
108
|
-
|
114
|
+
```ruby
|
109
115
|
Foo.fetch_if(nil) # => nil
|
110
116
|
Foo.fetch_if(:a) # => #<Foo:... @attributes={...}>
|
111
117
|
Foo.fetch_if(:xxx) # => <KeyError: ...>
|
112
|
-
|
118
|
+
```
|
113
119
|
|
114
|
-
|
120
|
+
### How to refer to other keys
|
115
121
|
|
116
|
-
|
122
|
+
```ruby
|
117
123
|
class Foo
|
118
124
|
include MemoryRecord
|
119
125
|
memory_record [
|
@@ -138,15 +144,15 @@ end
|
|
138
144
|
Foo[:a] == Foo[:x] # => true
|
139
145
|
Foo[:b] == Foo[:y] # => true
|
140
146
|
Foo[:c] == Foo[:z] # => true
|
141
|
-
|
147
|
+
```
|
142
148
|
|
143
|
-
|
149
|
+
### How can I prohibit the hash key from being attr_reader automatically?
|
144
150
|
|
145
|
-
|
151
|
+
#### attr_reader: false
|
146
152
|
|
147
153
|
I think that it is better to use it when you want to make it difficult to access easily.
|
148
154
|
|
149
|
-
|
155
|
+
```ruby
|
150
156
|
class Foo
|
151
157
|
include MemoryRecord
|
152
158
|
memory_record attr_reader: false do
|
@@ -159,11 +165,11 @@ end
|
|
159
165
|
Foo.first.x rescue $! # => #<NoMethodError: undefined method `x' for #<Foo:0x007fb2c710eda8>>
|
160
166
|
Foo.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<Foo:0x007fb2c710eda8>>
|
161
167
|
Foo.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<Foo:0x007fb2c710eda8>>
|
162
|
-
|
168
|
+
```
|
163
169
|
|
164
|
-
|
170
|
+
#### attr_reader: {only: :y}
|
165
171
|
|
166
|
-
|
172
|
+
```ruby
|
167
173
|
class Foo
|
168
174
|
include MemoryRecord
|
169
175
|
memory_record attr_reader: {only: :y} do
|
@@ -176,11 +182,11 @@ end
|
|
176
182
|
Foo.first.x rescue $! # => #<NoMethodError: undefined method `x' for #<Foo:0x007fcc861ff108>>
|
177
183
|
Foo.first.y rescue $! # => 1
|
178
184
|
Foo.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<Foo:0x007fcc861ff108>>
|
179
|
-
|
185
|
+
```
|
180
186
|
|
181
|
-
|
187
|
+
#### attr_reader: {except: :y}
|
182
188
|
|
183
|
-
|
189
|
+
```ruby
|
184
190
|
class Foo
|
185
191
|
include MemoryRecord
|
186
192
|
memory_record attr_reader: {except: :y} do
|
@@ -193,11 +199,11 @@ end
|
|
193
199
|
Foo.first.x rescue $! # => 1
|
194
200
|
Foo.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<Foo:0x007ff033895e88>>
|
195
201
|
Foo.first.z rescue $! # => 1
|
196
|
-
|
202
|
+
```
|
197
203
|
|
198
|
-
*** How to decide
|
204
|
+
*** How to decide **code** yourself?
|
199
205
|
|
200
|
-
|
206
|
+
```ruby
|
201
207
|
class Foo
|
202
208
|
include MemoryRecord
|
203
209
|
memory_record [
|
@@ -208,7 +214,7 @@ class Foo
|
|
208
214
|
end
|
209
215
|
|
210
216
|
Foo.collect(&:code) # => [1, 2, 3]
|
211
|
-
|
217
|
+
```
|
212
218
|
|
213
|
-
|
214
|
-
|
219
|
+
It is not recommended to specify it explicitly.
|
220
|
+
It is useful only when refactoring legacy code with compatibility in mind.
|
@@ -78,9 +78,24 @@ module MemoryRecord
|
|
78
78
|
end
|
79
79
|
|
80
80
|
m.module_eval do
|
81
|
+
# sort matches definition order
|
81
82
|
def <=>(other)
|
82
83
|
[self.class, code] <=> [other.class, other.code]
|
83
84
|
end
|
85
|
+
|
86
|
+
# Even if duplicate, objects match
|
87
|
+
def ==(other)
|
88
|
+
self.class == other.class && key == other.key
|
89
|
+
end
|
90
|
+
|
91
|
+
# Even if object_id of objects used as hash keys are different, they match. It also speeds up by defining hash.
|
92
|
+
def eql?(other)
|
93
|
+
self.class == other.class && key == other.key
|
94
|
+
end
|
95
|
+
|
96
|
+
def hash
|
97
|
+
key.hash
|
98
|
+
end
|
84
99
|
end
|
85
100
|
}
|
86
101
|
|
data/spec/memory_record_spec.rb
CHANGED
@@ -220,4 +220,18 @@ RSpec.describe MemoryRecord do
|
|
220
220
|
expect { [m1[:b], m2[:a]].sort }.to raise_error(ArgumentError)
|
221
221
|
end
|
222
222
|
end
|
223
|
+
|
224
|
+
it "==" do
|
225
|
+
a = Model.fetch(:_key0)
|
226
|
+
b = Marshal.load(Marshal.dump(a))
|
227
|
+
(a == b).should == true
|
228
|
+
end
|
229
|
+
|
230
|
+
it "eql?, hash" do
|
231
|
+
a = Model.fetch(:_key0)
|
232
|
+
b = Marshal.load(Marshal.dump(a))
|
233
|
+
h = {}
|
234
|
+
h[a] = true
|
235
|
+
h[b].should == true
|
236
|
+
end
|
223
237
|
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.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- akicho8
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -78,7 +78,7 @@ files:
|
|
78
78
|
- ".travis.yml"
|
79
79
|
- Gemfile
|
80
80
|
- LICENSE.txt
|
81
|
-
- README.
|
81
|
+
- README.md
|
82
82
|
- Rakefile
|
83
83
|
- examples/0100_basic.rb
|
84
84
|
- examples/0110_for_legacy_code.rb
|
@@ -100,6 +100,8 @@ files:
|
|
100
100
|
- examples/0270_instance_freeze.rb
|
101
101
|
- examples/0280_sortable.rb
|
102
102
|
- examples/0290_eql_behavior.rb
|
103
|
+
- examples/0300_use_as_hash_key.rb
|
104
|
+
- examples/0310_same_if_dup.rb
|
103
105
|
- lib/memory_record.rb
|
104
106
|
- lib/memory_record/memory_record.rb
|
105
107
|
- lib/memory_record/version.rb
|