memory_record 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +42 -73
- data/examples/0100_basic.rb +19 -13
- data/examples/0190_super_usable.rb +5 -0
- data/examples/0220_bad_design.rb +2 -2
- data/examples/0240_attr_reader_option.rb +6 -6
- data/examples/0250_lookup_super_call.rb +2 -2
- data/examples/0270_instance_freeze.rb +1 -1
- data/lib/memory_record/memory_record.rb +19 -25
- data/lib/memory_record/version.rb +1 -1
- data/spec/memory_record_spec.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a31161518612170878b45f58d9952e75403826b6ed1a3345e3a116b81b8b24c
|
4
|
+
data.tar.gz: d0c23e8933f7576c00608b5cc1f5fe233b3f609db3371d22b5ac25e08d198d02
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 830f15f5f99178dc80e2f142d10aa4dd7f23431aca3ede47bb3591624d1e98e190485500742eb2622487f929cecd56f5507252b7b4941727e83315bc00ff4cad
|
7
|
+
data.tar.gz: bd185cef177836f31268c760225e7df2935f5f42921420168819f4007543d8108046b0d4f7bd76d3e5fb41d61ed83ff726d6d3bf3973f06e6c382278129e6d29
|
data/README.md
CHANGED
@@ -29,26 +29,32 @@ $ bundle install
|
|
29
29
|
### Basic usage
|
30
30
|
|
31
31
|
```ruby
|
32
|
-
class
|
32
|
+
class Palette
|
33
33
|
include MemoryRecord
|
34
34
|
memory_record [
|
35
|
-
{key: :
|
36
|
-
{key: :
|
37
|
-
{key: :
|
35
|
+
{ key: :coral, r: 255, g: 127, b: 0 },
|
36
|
+
{ key: :tomato, r: 255, g: 99, b: 71 },
|
37
|
+
{ key: :gold, r: 255, g: 215, b: 0 },
|
38
38
|
]
|
39
39
|
|
40
|
-
def
|
41
|
-
|
40
|
+
def rgb
|
41
|
+
[r, g, b]
|
42
42
|
end
|
43
|
-
end
|
44
43
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
Language[:ruby].mr_author # => "Mr. Yukihiro Matsumoto"
|
44
|
+
def hex
|
45
|
+
"#" + rgb.collect { |e| "%02X" % e }.join
|
46
|
+
end
|
49
47
|
|
50
|
-
|
51
|
-
|
48
|
+
def name
|
49
|
+
super.capitalize
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
Palette[:tomato].key # => :tomato
|
54
|
+
Palette[:tomato].name # => "Tomato"
|
55
|
+
Palette[:tomato].rgb # => [255, 99, 71]
|
56
|
+
Palette[:tomato].hex # => "#FF6347"
|
57
|
+
Palette.collect(&:hex) # => ["#FF7F00", "#FF6347", "#FFD700"]
|
52
58
|
```
|
53
59
|
|
54
60
|
### How to turn as an array?
|
@@ -56,14 +62,14 @@ Language.collect(&:author) # => ["John McCarthy", "Dennis Ritchie", "Yukihiro Ma
|
|
56
62
|
**Enumerable** extended, so that **each** method is available
|
57
63
|
|
58
64
|
```ruby
|
59
|
-
|
60
|
-
|
65
|
+
Palette.each { |e| ... }
|
66
|
+
Palette.collect { |e| ... }
|
61
67
|
```
|
62
68
|
|
63
69
|
### How do I submit a form to select in Rails?
|
64
70
|
|
65
71
|
```ruby
|
66
|
-
form.collection_select(:selection_code,
|
72
|
+
form.collection_select(:selection_code, Palette, :code, :name)
|
67
73
|
```
|
68
74
|
|
69
75
|
### Is the reference in subscripts slow?
|
@@ -71,15 +77,15 @@ form.collection_select(:selection_code, Foo, :code, :name)
|
|
71
77
|
Since it has a hash internally using the key value as a key, it can be acquired with O (1).
|
72
78
|
|
73
79
|
```ruby
|
74
|
-
|
75
|
-
|
80
|
+
Palette[1].name # => "Tomato"
|
81
|
+
Palette[:tomato].name # => "Tomato"
|
76
82
|
```
|
77
83
|
|
78
84
|
### Instances always react to **code** and **key**
|
79
85
|
|
80
86
|
```ruby
|
81
|
-
object =
|
82
|
-
object.key # => :
|
87
|
+
object = Palette[:tomato]
|
88
|
+
object.key # => :tomato
|
83
89
|
object.code # => 1
|
84
90
|
```
|
85
91
|
|
@@ -98,23 +104,23 @@ Alias of **name**, **to_s** is defined.
|
|
98
104
|
### If there is no key, use fetch to get an error
|
99
105
|
|
100
106
|
```ruby
|
101
|
-
|
107
|
+
Palette.fetch(:xxx) # => <KeyError: ...>
|
102
108
|
```
|
103
109
|
|
104
110
|
The following are all the same
|
105
111
|
|
106
112
|
```ruby
|
107
|
-
|
108
|
-
|
109
|
-
|
113
|
+
Palette[:xxx] || :default # => :default
|
114
|
+
Palette.fetch(:xxx, :default} # => :default
|
115
|
+
Palette.fetch(:xxx) { :default } # => :default
|
110
116
|
```
|
111
117
|
|
112
118
|
### Use fetch_if to ignore if the key is nil
|
113
119
|
|
114
120
|
```ruby
|
115
|
-
|
116
|
-
|
117
|
-
|
121
|
+
Palette.fetch_if(nil) # => nil
|
122
|
+
Palette.fetch_if(:tomato) # => #<Palette:... @attributes={...}>
|
123
|
+
Palette.fetch_if(:xxx) # => <KeyError: ...>
|
118
124
|
```
|
119
125
|
|
120
126
|
### How to refer to other keys
|
@@ -123,9 +129,9 @@ Foo.fetch_if(:xxx) # => <KeyError: ...>
|
|
123
129
|
class Foo
|
124
130
|
include MemoryRecord
|
125
131
|
memory_record [
|
126
|
-
{key: :a, other_key: :x},
|
127
|
-
{key: :b, other_key: :y},
|
128
|
-
{key: :c, other_key: :z},
|
132
|
+
{ key: :a, other_key: :x },
|
133
|
+
{ key: :b, other_key: :y },
|
134
|
+
{ key: :c, other_key: :z },
|
129
135
|
]
|
130
136
|
|
131
137
|
class << self
|
@@ -146,35 +152,16 @@ Foo[:b] == Foo[:y] # => true
|
|
146
152
|
Foo[:c] == Foo[:z] # => true
|
147
153
|
```
|
148
154
|
|
149
|
-
### How
|
150
|
-
|
151
|
-
#### attr_reader: false
|
152
|
-
|
153
|
-
I think that it is better to use it when you want to make it difficult to access easily.
|
154
|
-
|
155
|
-
```ruby
|
156
|
-
class Foo
|
157
|
-
include MemoryRecord
|
158
|
-
memory_record attr_reader: false do
|
159
|
-
[
|
160
|
-
{x: 1, y: 1, z: 1},
|
161
|
-
]
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
Foo.first.x rescue $! # => #<NoMethodError: undefined method `x' for #<Foo:0x007fb2c710eda8>>
|
166
|
-
Foo.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<Foo:0x007fb2c710eda8>>
|
167
|
-
Foo.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<Foo:0x007fb2c710eda8>>
|
168
|
-
```
|
155
|
+
### How prohibit the hash key from being attr_reader automatically?
|
169
156
|
|
170
|
-
|
157
|
+
Use `attr_reader: false` or `attr_reader: {only: ...}` or `attr_reader: {except: ...}`
|
171
158
|
|
172
159
|
```ruby
|
173
160
|
class Foo
|
174
161
|
include MemoryRecord
|
175
162
|
memory_record attr_reader: {only: :y} do
|
176
163
|
[
|
177
|
-
{x: 1, y: 1, z: 1},
|
164
|
+
{ x: 1, y: 1, z: 1 },
|
178
165
|
]
|
179
166
|
end
|
180
167
|
end
|
@@ -184,36 +171,18 @@ Foo.first.y rescue $! # => 1
|
|
184
171
|
Foo.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<Foo:0x007fcc861ff108>>
|
185
172
|
```
|
186
173
|
|
187
|
-
#### attr_reader: {except: :y}
|
188
|
-
|
189
|
-
```ruby
|
190
|
-
class Foo
|
191
|
-
include MemoryRecord
|
192
|
-
memory_record attr_reader: {except: :y} do
|
193
|
-
[
|
194
|
-
{x: 1, y: 1, z: 1},
|
195
|
-
]
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
Foo.first.x rescue $! # => 1
|
200
|
-
Foo.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<Foo:0x007ff033895e88>>
|
201
|
-
Foo.first.z rescue $! # => 1
|
202
|
-
```
|
203
|
-
|
204
174
|
*** How to decide **code** yourself?
|
205
175
|
|
206
176
|
```ruby
|
207
177
|
class Foo
|
208
178
|
include MemoryRecord
|
209
179
|
memory_record [
|
210
|
-
{code: 1, key: :a, name: "A"},
|
211
|
-
{code: 2, key: :b, name: "B"},
|
212
|
-
{code: 3, key: :c, name: "C"},
|
180
|
+
{ code: 1, key: :a, name: "A" },
|
181
|
+
{ code: 2, key: :b, name: "B" },
|
213
182
|
]
|
214
183
|
end
|
215
184
|
|
216
|
-
Foo.collect(&:code) # => [1, 2
|
185
|
+
Foo.collect(&:code) # => [1, 2]
|
217
186
|
```
|
218
187
|
|
219
188
|
It is not recommended to specify it explicitly.
|
data/examples/0100_basic.rb
CHANGED
@@ -1,23 +1,29 @@
|
|
1
1
|
$LOAD_PATH.unshift '../lib'
|
2
2
|
require 'memory_record'
|
3
3
|
|
4
|
-
class
|
4
|
+
class Palette
|
5
5
|
include MemoryRecord
|
6
6
|
memory_record [
|
7
|
-
{key: :
|
8
|
-
{key: :
|
9
|
-
{key: :
|
7
|
+
{ key: :coral, r: 255, g: 127, b: 0 },
|
8
|
+
{ key: :tomato, r: 255, g: 99, b: 71 },
|
9
|
+
{ key: :gold, r: 255, g: 215, b: 0 },
|
10
10
|
]
|
11
11
|
|
12
|
-
def
|
13
|
-
|
12
|
+
def rgb
|
13
|
+
[r, g, b]
|
14
|
+
end
|
15
|
+
|
16
|
+
def hex
|
17
|
+
"#" + rgb.collect { |e| "%02X" % e }.join
|
14
18
|
end
|
15
|
-
end
|
16
19
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
20
|
+
def name
|
21
|
+
super.capitalize
|
22
|
+
end
|
23
|
+
end
|
21
24
|
|
22
|
-
|
23
|
-
|
25
|
+
Palette[:tomato].key # => :tomato
|
26
|
+
Palette[:tomato].name # => "Tomato"
|
27
|
+
Palette[:tomato].rgb # => [255, 99, 71]
|
28
|
+
Palette[:tomato].hex # => "#FF6347"
|
29
|
+
Palette.collect(&:hex) # => ["#FF7F00", "#FF6347", "#FFD700"]
|
data/examples/0220_bad_design.rb
CHANGED
@@ -13,5 +13,5 @@ class Foo
|
|
13
13
|
]
|
14
14
|
end
|
15
15
|
|
16
|
-
Foo["01"] # => #<Foo:
|
17
|
-
Foo["02"] # => #<Foo:
|
16
|
+
Foo["01"] # => #<Foo:0x00007fb30e1ddcd8 @attributes={:key=>:"01", :name=>"left", :code=>0}>
|
17
|
+
Foo["02"] # => #<Foo:0x00007fb30e1ddbc0 @attributes={:key=>:"02", :name=>"right", :code=>1}>
|
@@ -10,9 +10,9 @@ class C1
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
C1.first.x rescue $! # => #<NoMethodError: undefined method `x' for #<C1:
|
14
|
-
C1.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<C1:
|
15
|
-
C1.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<C1:
|
13
|
+
C1.first.x rescue $! # => #<NoMethodError: undefined method `x' for #<C1:0x00007fb4d51afd60>>
|
14
|
+
C1.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<C1:0x00007fb4d51afd60>>
|
15
|
+
C1.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<C1:0x00007fb4d51afd60>>
|
16
16
|
|
17
17
|
class C2
|
18
18
|
include MemoryRecord
|
@@ -23,9 +23,9 @@ class C2
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
-
C2.first.x rescue $! # => #<NoMethodError: undefined method `x' for #<C2:
|
26
|
+
C2.first.x rescue $! # => #<NoMethodError: undefined method `x' for #<C2:0x00007fb4d50dfb60>>
|
27
27
|
C2.first.y rescue $! # => 1
|
28
|
-
C2.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<C2:
|
28
|
+
C2.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<C2:0x00007fb4d50dfb60>>
|
29
29
|
|
30
30
|
class C3
|
31
31
|
include MemoryRecord
|
@@ -37,5 +37,5 @@ class C3
|
|
37
37
|
end
|
38
38
|
|
39
39
|
C3.first.x rescue $! # => 1
|
40
|
-
C3.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<C3:
|
40
|
+
C3.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<C3:0x00007fb4d50d4440>>
|
41
41
|
C3.first.z rescue $! # => 1
|
@@ -15,5 +15,5 @@ class C
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
C.lookup(:alice) # => #<C:
|
19
|
-
C[:alice] # => #<C:
|
18
|
+
C.lookup(:alice) # => #<C:0x00007fa9dc11d8b0 @attributes={:key=>:alice, :code=>0}>
|
19
|
+
C[:alice] # => #<C:0x00007fa9dc11d8b0 @attributes={:key=>:alice, :code=>0}>
|
@@ -62,38 +62,32 @@ module MemoryRecord
|
|
62
62
|
end
|
63
63
|
|
64
64
|
# Define it to an ancestor so that super method can be used.
|
65
|
-
include Module.new
|
65
|
+
include Module.new {
|
66
66
|
([:key, :code] + attr_reader_args).uniq.each do |key|
|
67
|
-
|
68
|
-
define_method(key) { @attributes[key.to_sym] }
|
69
|
-
end
|
67
|
+
define_method(key) { @attributes[key.to_sym] }
|
70
68
|
end
|
71
69
|
|
72
|
-
unless
|
73
|
-
|
74
|
-
define_method(:name) { key.to_s }
|
75
|
-
end
|
70
|
+
unless method_defined?(:name)
|
71
|
+
define_method(:name) { key.to_s }
|
76
72
|
end
|
77
73
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
end
|
74
|
+
# sort matches definition order
|
75
|
+
def <=>(other)
|
76
|
+
[self.class, code] <=> [other.class, other.code]
|
77
|
+
end
|
83
78
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
79
|
+
# Even if duplicate, objects match
|
80
|
+
def ==(other)
|
81
|
+
self.class == other.class && key == other.key
|
82
|
+
end
|
88
83
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
84
|
+
# Even if object_id of objects used as hash keys are different, they match. It also speeds up by defining hash.
|
85
|
+
def eql?(other)
|
86
|
+
self.class == other.class && key == other.key
|
87
|
+
end
|
93
88
|
|
94
|
-
|
95
|
-
|
96
|
-
end
|
89
|
+
def hash
|
90
|
+
self.class.hash ^ key.hash
|
97
91
|
end
|
98
92
|
}
|
99
93
|
|
@@ -205,7 +199,7 @@ module MemoryRecord
|
|
205
199
|
|
206
200
|
attr_reader :attributes
|
207
201
|
|
208
|
-
delegate :[], to: :attributes
|
202
|
+
delegate :[], :to_h, to: :attributes
|
209
203
|
delegate :to_s, to: :name
|
210
204
|
|
211
205
|
def initialize(attributes)
|
data/spec/memory_record_spec.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.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- akicho8
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-02-
|
11
|
+
date: 2018-02-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|