memory_record 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -2
- data/README.org +3 -15
- data/examples/{0110_basic.rb → 0100_basic.rb} +1 -1
- data/examples/{0130_for_legacy_code.rb → 0110_for_legacy_code.rb} +3 -3
- data/examples/{0140_key_join_underscore_if_array.rb → 0120_key_join_underscore_if_array.rb} +0 -0
- data/examples/{0150_behavior_like_hash.rb → 0130_behavior_like_hash.rb} +0 -0
- data/examples/{0160_practice.rb → 0140_practice.rb} +4 -4
- data/examples/{0170_reset.rb → 0150_reset.rb} +4 -4
- data/examples/{0180_key_is_anything_ok.rb → 0160_key_is_anything_ok.rb} +2 -2
- data/examples/{0190_tips_on_handling_boolean_type.rb → 0170_tips_on_handling_boolean_type.rb} +2 -2
- data/examples/{0200_freeze_makes_it_impossible_to_memorize.rb → 0180_freeze_makes_it_impossible_to_memorize.rb} +2 -2
- data/examples/{0210_super_usable.rb → 0190_super_usable.rb} +1 -1
- data/examples/{0230_validation.rb → 0200_validation.rb} +1 -1
- data/examples/{0240_key_duplicate.rb → 0210_key_duplicate.rb} +0 -0
- data/examples/{0250_when_key_like_numeric.rb → 0220_when_key_like_numeric.rb} +1 -1
- data/examples/{0260_with_use_activerecord_enum.rb → 0230_with_use_activerecord_enum.rb} +4 -4
- data/lib/memory_record/memory_record.rb +59 -17
- data/lib/memory_record/version.rb +1 -1
- data/spec/memory_record_spec.rb +6 -6
- metadata +16 -17
- data/examples/0120_attr_reader_auto_option.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c9cd8f7e037fbdb335febc08e26283d0323f704
|
4
|
+
data.tar.gz: 0a3eaf7f098d120c2f4c6ce912a2856bd6948462
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4885b1819bb3cfdc97ee1acdeee43d4e6ceaf44e21d55c79d67054a52d7954db39539cd09f30d8f55d7a4271d9e932b27b095a54c52bc807759c4dd24789a5ca
|
7
|
+
data.tar.gz: a0bb34531ba701cf5a36def1d27d441169987299d64419031ac81dc362e218e2ef81623cdb4a9299de29ff0edb5aa423ff8c8fe404a42ecdfeada027809a4403
|
data/.travis.yml
CHANGED
data/README.org
CHANGED
@@ -28,7 +28,7 @@ class Language
|
|
28
28
|
{key: :lisp, author: "John McCarthy" },
|
29
29
|
{key: :c, author: "Dennis Ritchie" },
|
30
30
|
{key: :ruby, author: "Yukihiro Matsumoto" },
|
31
|
-
]
|
31
|
+
]
|
32
32
|
|
33
33
|
def mr_author
|
34
34
|
"Mr. #{author}"
|
@@ -53,7 +53,7 @@ class Foo
|
|
53
53
|
{code: 1, key: :a, name: "A"},
|
54
54
|
{code: 2, key: :b, name: "B"},
|
55
55
|
{code: 3, key: :c, name: "C"},
|
56
|
-
]
|
56
|
+
]
|
57
57
|
end
|
58
58
|
|
59
59
|
Foo.collect(&:code) # => [1, 2, 3]
|
@@ -93,18 +93,6 @@ object.key # => :a
|
|
93
93
|
object.code # => 1
|
94
94
|
#+END_SRC
|
95
95
|
|
96
|
-
*** =attr_reader= option
|
97
|
-
|
98
|
-
The attribute value can be referenced with =@attributes[:xxx]=, but it is troublesome to refer frequently, so it is methodized with =attr_reader: xxx=.
|
99
|
-
|
100
|
-
*** =attr_reader_auto= option
|
101
|
-
|
102
|
-
Automatically all attributes =attr_reader=
|
103
|
-
|
104
|
-
*** What if I do not want you to feel free to access the attributes of a record?
|
105
|
-
|
106
|
-
=attr_reader= with no method definition and reference =object.attributes[:xxx]=
|
107
|
-
|
108
96
|
*** How do I add a method to an instance?
|
109
97
|
|
110
98
|
For that, I am creating a new class so I need to define it normally
|
@@ -115,7 +103,7 @@ object.code # => 1
|
|
115
103
|
|
116
104
|
*** =to_s= method is defined?
|
117
105
|
|
118
|
-
|
106
|
+
Alias of =name=, =to_s= is defined.
|
119
107
|
|
120
108
|
*** If there is no key, use fetch to get an error
|
121
109
|
|
File without changes
|
File without changes
|
@@ -6,20 +6,20 @@ class Direction
|
|
6
6
|
memory_record [
|
7
7
|
{key: :left, name: '←', vector: [-1, 0]},
|
8
8
|
{key: :right, name: '→', vector: [ 1, 0]},
|
9
|
-
]
|
9
|
+
]
|
10
10
|
|
11
11
|
def long_name
|
12
|
-
|
12
|
+
"#{name}方向"
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
Direction.collect(&:name) # => [
|
16
|
+
Direction.collect(&:name) # => ["←", "→"]
|
17
17
|
Direction.keys # => [:left, :right]
|
18
18
|
|
19
19
|
Direction[:right].key # => :right
|
20
20
|
Direction[:right].code # => 1
|
21
21
|
Direction[:right].vector # => [1, 0]
|
22
|
-
Direction[:right].long_name # =>
|
22
|
+
Direction[:right].long_name # => "→方向"
|
23
23
|
|
24
24
|
Direction[1].key # => :right
|
25
25
|
|
@@ -3,19 +3,19 @@ require 'memory_record'
|
|
3
3
|
|
4
4
|
class C
|
5
5
|
include MemoryRecord
|
6
|
-
memory_record [{key: :a}]
|
6
|
+
memory_record [{key: :a}]
|
7
7
|
end
|
8
8
|
|
9
9
|
C.keys # => [:a]
|
10
10
|
|
11
11
|
# memory_record では更新できない
|
12
12
|
class C
|
13
|
-
memory_record [{key: :b}]
|
13
|
+
memory_record [{key: :b}]
|
14
14
|
end
|
15
15
|
|
16
16
|
C.keys # => [:a]
|
17
17
|
|
18
|
-
#
|
19
|
-
C.
|
18
|
+
# memory_record_reset を使うこと
|
19
|
+
C.memory_record_reset [{key: :c}]
|
20
20
|
|
21
21
|
C.keys # => [:c]
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# freeze
|
1
|
+
# If you freeze it will not be able to make these memos
|
2
2
|
|
3
3
|
$LOAD_PATH.unshift '../lib'
|
4
4
|
require 'memory_record'
|
@@ -13,7 +13,7 @@ class C2
|
|
13
13
|
include MemoryRecord
|
14
14
|
memory_record [
|
15
15
|
{model: C},
|
16
|
-
]
|
16
|
+
]
|
17
17
|
|
18
18
|
def x
|
19
19
|
@x ||= 'OK'
|
File without changes
|
@@ -16,9 +16,9 @@ end
|
|
16
16
|
class GenderInfo
|
17
17
|
include MemoryRecord
|
18
18
|
memory_record [
|
19
|
-
{key: :male, name: '
|
20
|
-
{key: :female, name: '
|
21
|
-
]
|
19
|
+
{key: :male, name: '♂'},
|
20
|
+
{key: :female, name: '♀'},
|
21
|
+
]
|
22
22
|
end
|
23
23
|
|
24
24
|
class User < ActiveRecord::Base
|
@@ -30,4 +30,4 @@ class User < ActiveRecord::Base
|
|
30
30
|
end
|
31
31
|
|
32
32
|
user = User.create!(gender_key: :male)
|
33
|
-
user.gender_info.name # => "
|
33
|
+
user.gender_info.name # => "♂"
|
@@ -9,30 +9,64 @@ module MemoryRecord
|
|
9
9
|
extend ActiveSupport::Concern
|
10
10
|
|
11
11
|
class_methods do
|
12
|
-
|
12
|
+
# Example
|
13
|
+
#
|
14
|
+
# memory_record [
|
15
|
+
# {id: 1, name: "alice"},
|
16
|
+
# {id: 2, name: "bob" },
|
17
|
+
# ], attr_reader: false
|
18
|
+
#
|
19
|
+
# or
|
20
|
+
#
|
21
|
+
# memory_record(attr_reader: false) do
|
22
|
+
# [
|
23
|
+
# {id: 1, name: "alice"},
|
24
|
+
# {id: 2, name: "bob" },
|
25
|
+
# ]
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
def memory_record(records = nil, **options, &block)
|
13
29
|
return if memory_record_defined?
|
14
30
|
|
31
|
+
if block
|
32
|
+
options = records || {}
|
33
|
+
records = block.call
|
34
|
+
end
|
35
|
+
|
15
36
|
extend ActiveModel::Translation
|
16
37
|
extend Enumerable
|
17
38
|
include ::MemoryRecord::SingletonMethods
|
18
39
|
|
19
|
-
class_attribute :
|
20
|
-
self.
|
21
|
-
attr_reader:
|
40
|
+
class_attribute :memory_record_options
|
41
|
+
self.memory_record_options = {
|
42
|
+
attr_reader: true,
|
22
43
|
}.merge(options)
|
23
44
|
|
24
45
|
if block_given?
|
25
|
-
yield
|
46
|
+
yield memory_record_options
|
26
47
|
end
|
27
48
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
49
|
+
attr_reader_args = []
|
50
|
+
all_keys = records.inject([]) { |a, e| a | e.keys.collect(&:to_sym) }
|
51
|
+
case v = memory_record_options[:attr_reader]
|
52
|
+
when true
|
53
|
+
attr_reader_args = all_keys
|
54
|
+
when Hash
|
55
|
+
if v[:only]
|
56
|
+
all_keys &= Array(v[:only])
|
57
|
+
elsif v[:except]
|
58
|
+
all_keys -= Array(v[:except])
|
59
|
+
end
|
60
|
+
attr_reader_args = all_keys
|
61
|
+
when Array
|
62
|
+
attr_reader_args = v
|
63
|
+
when Symbol, String
|
64
|
+
attr_reader_args = [v]
|
32
65
|
end
|
33
66
|
|
67
|
+
# Define it to an ancestor so that super method can be used.
|
34
68
|
include Module.new.tap { |m|
|
35
|
-
([:key, :code] +
|
69
|
+
([:key, :code] + attr_reader_args).uniq.each do |key|
|
36
70
|
m.class_eval do
|
37
71
|
define_method(key) { @attributes[key.to_sym] }
|
38
72
|
end
|
@@ -49,7 +83,7 @@ module MemoryRecord
|
|
49
83
|
end
|
50
84
|
}
|
51
85
|
|
52
|
-
|
86
|
+
memory_record_reset(records)
|
53
87
|
end
|
54
88
|
|
55
89
|
def memory_record_defined?
|
@@ -64,7 +98,10 @@ module MemoryRecord
|
|
64
98
|
end
|
65
99
|
|
66
100
|
def lookup(key)
|
67
|
-
|
101
|
+
if key.kind_of? self
|
102
|
+
return key
|
103
|
+
end
|
104
|
+
|
68
105
|
case key
|
69
106
|
when Symbol, String
|
70
107
|
@values_hash[:key][key.to_sym]
|
@@ -75,7 +112,10 @@ module MemoryRecord
|
|
75
112
|
alias [] lookup
|
76
113
|
|
77
114
|
def fetch(key, default = nil, &block)
|
78
|
-
|
115
|
+
if block_given? && default
|
116
|
+
raise ArgumentError
|
117
|
+
end
|
118
|
+
|
79
119
|
v = lookup(key)
|
80
120
|
unless v
|
81
121
|
case
|
@@ -112,18 +152,20 @@ module MemoryRecord
|
|
112
152
|
|
113
153
|
attr_reader :values
|
114
154
|
|
115
|
-
def
|
155
|
+
def memory_record_reset(records)
|
116
156
|
@keys = nil
|
117
157
|
@codes = nil
|
118
|
-
@values =
|
158
|
+
@values = records.collect.with_index { |e, i|
|
159
|
+
new(_attributes_normalize(e, i))
|
160
|
+
}.freeze
|
119
161
|
@values_hash = {}
|
120
162
|
[:code, :key].each do |pk|
|
121
163
|
@values_hash[pk] = @values.inject({}) do |a, e|
|
122
164
|
a.merge(e.send(pk) => e) do |key, a, b|
|
123
165
|
raise ArgumentError, [
|
124
166
|
"#{name}##{pk} #{key.inspect} is duplicate",
|
125
|
-
"Existing:
|
126
|
-
"
|
167
|
+
"Existing: #{a.attributes.inspect}",
|
168
|
+
"Conflict: #{b.attributes.inspect}",
|
127
169
|
].join("\n")
|
128
170
|
end
|
129
171
|
end
|
data/spec/memory_record_spec.rb
CHANGED
@@ -20,7 +20,7 @@ RSpec.describe MemoryRecord do
|
|
20
20
|
def __define(table)
|
21
21
|
Class.new {
|
22
22
|
include MemoryRecord
|
23
|
-
memory_record table,
|
23
|
+
memory_record table, attr_reader: true
|
24
24
|
}
|
25
25
|
end
|
26
26
|
|
@@ -82,7 +82,7 @@ RSpec.describe MemoryRecord do
|
|
82
82
|
context 'Re-set' do
|
83
83
|
before do
|
84
84
|
@model = __define [{key: :a}]
|
85
|
-
@model.
|
85
|
+
@model.memory_record_reset [{key: :b}, {key: :c}]
|
86
86
|
end
|
87
87
|
it 'changed' do
|
88
88
|
assert_equal [:b, :c], @model.keys
|
@@ -132,13 +132,13 @@ RSpec.describe MemoryRecord do
|
|
132
132
|
end
|
133
133
|
end
|
134
134
|
|
135
|
-
describe '
|
135
|
+
describe 'attr_reader' do
|
136
136
|
class Model3
|
137
137
|
include MemoryRecord
|
138
138
|
memory_record [
|
139
139
|
{a: 1},
|
140
140
|
{b: 2},
|
141
|
-
]
|
141
|
+
]
|
142
142
|
end
|
143
143
|
|
144
144
|
it do
|
@@ -149,8 +149,8 @@ RSpec.describe MemoryRecord do
|
|
149
149
|
|
150
150
|
describe 'Do not duplicate key and code' do
|
151
151
|
it do
|
152
|
-
expect { Model.
|
153
|
-
expect { Model.
|
152
|
+
expect { Model.memory_record_reset([{key: :a}, {key: :a}]) }.to raise_error(ArgumentError)
|
153
|
+
expect { Model.memory_record_reset([{code: 0}, {code: 0}]) }.to raise_error(ArgumentError)
|
154
154
|
end
|
155
155
|
end
|
156
156
|
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- akicho8
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -94,21 +94,20 @@ files:
|
|
94
94
|
- LICENSE.txt
|
95
95
|
- README.org
|
96
96
|
- Rakefile
|
97
|
-
- examples/
|
98
|
-
- examples/
|
99
|
-
- examples/
|
100
|
-
- examples/
|
101
|
-
- examples/
|
102
|
-
- examples/
|
103
|
-
- examples/
|
104
|
-
- examples/
|
105
|
-
- examples/
|
106
|
-
- examples/
|
107
|
-
- examples/
|
108
|
-
- examples/
|
109
|
-
- examples/
|
110
|
-
- examples/
|
111
|
-
- examples/0260_with_use_activerecord_enum.rb
|
97
|
+
- examples/0100_basic.rb
|
98
|
+
- examples/0110_for_legacy_code.rb
|
99
|
+
- examples/0120_key_join_underscore_if_array.rb
|
100
|
+
- examples/0130_behavior_like_hash.rb
|
101
|
+
- examples/0140_practice.rb
|
102
|
+
- examples/0150_reset.rb
|
103
|
+
- examples/0160_key_is_anything_ok.rb
|
104
|
+
- examples/0170_tips_on_handling_boolean_type.rb
|
105
|
+
- examples/0180_freeze_makes_it_impossible_to_memorize.rb
|
106
|
+
- examples/0190_super_usable.rb
|
107
|
+
- examples/0200_validation.rb
|
108
|
+
- examples/0210_key_duplicate.rb
|
109
|
+
- examples/0220_when_key_like_numeric.rb
|
110
|
+
- examples/0230_with_use_activerecord_enum.rb
|
112
111
|
- lib/memory_record.rb
|
113
112
|
- lib/memory_record/memory_record.rb
|
114
113
|
- lib/memory_record/version.rb
|
@@ -1,14 +0,0 @@
|
|
1
|
-
$LOAD_PATH.unshift '../lib'
|
2
|
-
require 'memory_record'
|
3
|
-
|
4
|
-
model = Class.new do
|
5
|
-
include MemoryRecord
|
6
|
-
memory_record [
|
7
|
-
{a: 1},
|
8
|
-
{b: 2},
|
9
|
-
], attr_reader_auto: true
|
10
|
-
end
|
11
|
-
|
12
|
-
model.first.name # => nil
|
13
|
-
model.first.a # => 1
|
14
|
-
model.first.b # => nil
|