memory_record 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.org +1 -1
- data/examples/0190_super_usable.rb +1 -1
- 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/examples/0280_sortable.rb +22 -0
- data/examples/0290_eql_behavior.rb +21 -0
- data/lib/memory_record/memory_record.rb +12 -10
- data/lib/memory_record/version.rb +1 -1
- data/memory_record.gemspec +0 -1
- data/spec/memory_record_spec.rb +26 -17
- metadata +5 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e6a9d0e0253067464bc3fa778dab80bf47b635752094879aa4ad4f1a95ef6773
|
4
|
+
data.tar.gz: 2fd9222c966e16931cff928629e2d7a50237a6c39b7be2266f12eeb8fc80ea87
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c02532cc3ad012939336002e2cdad14e7a7afd0ba9684d5c510117454c915b9766dc3305a5c4737f1619e4db4439df9c723e52132f753772f2aa7bc45be9d76c
|
7
|
+
data.tar.gz: 18cd2b7a7378fca5a9a0d511c8789e8ed7c021c21295fb4c21a7f4060bf453db04b7de819b91df66be6d8fafe4ba8d9399a7e0a3a3345f7f5837ef0868caea33
|
data/README.org
CHANGED
@@ -83,7 +83,7 @@ object.code # => 1
|
|
83
83
|
|
84
84
|
*** =name= method is special?
|
85
85
|
|
86
|
-
If =name= is not defined, it defines a =name= method that returns
|
86
|
+
If =name= is not defined, it defines a =name= method that returns =key.to_s=
|
87
87
|
|
88
88
|
*** =to_s= method is defined?
|
89
89
|
|
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:0x00007f9e519ba6d0 @attributes={:key=>:"01", :name=>"left", :code=>0}>
|
17
|
+
Foo["02"] # => #<Foo:0x00007f9e519ba568 @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:0x00007f83bc8e7078>>
|
14
|
+
C1.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<C1:0x00007f83bc8e7078>>
|
15
|
+
C1.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<C1:0x00007f83bc8e7078>>
|
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:0x00007f83bc9419d8>>
|
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:0x00007f83bc9419d8>>
|
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:0x00007f83bc948968>>
|
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:0x00007ffe599ba4d8 @attributes={:key=>:alice, :code=>0}>
|
19
|
+
C[:alice] # => #<C:0x00007ffe599ba4d8 @attributes={:key=>:alice, :code=>0}>
|
@@ -0,0 +1,22 @@
|
|
1
|
+
$LOAD_PATH.unshift '../lib'
|
2
|
+
require 'memory_record'
|
3
|
+
|
4
|
+
class A
|
5
|
+
include MemoryRecord
|
6
|
+
memory_record [
|
7
|
+
{key: :a},
|
8
|
+
{key: :b},
|
9
|
+
]
|
10
|
+
end
|
11
|
+
|
12
|
+
[A[:b], A[:a]].sort.collect(&:key) # => [:a, :b]
|
13
|
+
|
14
|
+
class B
|
15
|
+
include MemoryRecord
|
16
|
+
memory_record [
|
17
|
+
{key: :a},
|
18
|
+
]
|
19
|
+
end
|
20
|
+
|
21
|
+
# class different
|
22
|
+
[A[:a], B[:a]].sort.collect(&:key) rescue $! # => #<ArgumentError: comparison of A with B failed>
|
@@ -0,0 +1,21 @@
|
|
1
|
+
$LOAD_PATH.unshift '../lib'
|
2
|
+
require 'memory_record'
|
3
|
+
|
4
|
+
class C
|
5
|
+
include MemoryRecord
|
6
|
+
memory_record [
|
7
|
+
{key: :a},
|
8
|
+
]
|
9
|
+
def foo
|
10
|
+
@foo ||= Object.new
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
a = C[:a]
|
15
|
+
b = C[:a]
|
16
|
+
|
17
|
+
a.eql?(b) # => true
|
18
|
+
b.foo
|
19
|
+
a.eql?(b) # => true
|
20
|
+
|
21
|
+
[a, b] - [a] # => []
|
@@ -3,7 +3,8 @@ require 'active_support/concern'
|
|
3
3
|
require 'active_support/core_ext/module/concerning'
|
4
4
|
require 'active_support/core_ext/class/attribute'
|
5
5
|
require 'active_support/core_ext/array/wrap'
|
6
|
-
require '
|
6
|
+
require 'active_support/core_ext/array/wrap'
|
7
|
+
require 'active_support/core_ext/module/delegation'
|
7
8
|
|
8
9
|
module MemoryRecord
|
9
10
|
extend ActiveSupport::Concern
|
@@ -32,7 +33,6 @@ module MemoryRecord
|
|
32
33
|
records = block.call
|
33
34
|
end
|
34
35
|
|
35
|
-
extend ActiveModel::Translation
|
36
36
|
extend Enumerable
|
37
37
|
include ::MemoryRecord::SingletonMethods
|
38
38
|
|
@@ -66,18 +66,20 @@ module MemoryRecord
|
|
66
66
|
# Define it to an ancestor so that super method can be used.
|
67
67
|
include Module.new.tap { |m|
|
68
68
|
([:key, :code] + attr_reader_args).uniq.each do |key|
|
69
|
-
m.
|
69
|
+
m.module_eval do
|
70
70
|
define_method(key) { @attributes[key.to_sym] }
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
74
|
unless m.method_defined?(:name)
|
75
|
-
m.
|
76
|
-
define_method(:name)
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
75
|
+
m.module_eval do
|
76
|
+
define_method(:name) { key.to_s }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
m.module_eval do
|
81
|
+
def <=>(other)
|
82
|
+
[self.class, code] <=> [other.class, other.code]
|
81
83
|
end
|
82
84
|
end
|
83
85
|
}
|
@@ -115,7 +117,7 @@ module MemoryRecord
|
|
115
117
|
|
116
118
|
def fetch(key, default = nil, &block)
|
117
119
|
if block_given? && default
|
118
|
-
raise ArgumentError
|
120
|
+
raise ArgumentError, "Can't use default and block together"
|
119
121
|
end
|
120
122
|
|
121
123
|
v = lookup(key)
|
data/memory_record.gemspec
CHANGED
data/spec/memory_record_spec.rb
CHANGED
@@ -24,8 +24,6 @@ RSpec.describe MemoryRecord do
|
|
24
24
|
}
|
25
25
|
end
|
26
26
|
|
27
|
-
let(:instance) { Model.first }
|
28
|
-
|
29
27
|
context 'Useful Class Methods' do
|
30
28
|
it 'each' do
|
31
29
|
assert Model.each
|
@@ -60,22 +58,22 @@ RSpec.describe MemoryRecord do
|
|
60
58
|
|
61
59
|
context 'Subscript access to instance' do
|
62
60
|
it do
|
63
|
-
|
64
|
-
|
61
|
+
Model.first[:name].should == 'A'
|
62
|
+
Model.first[:xxxx].should == nil
|
65
63
|
end
|
66
64
|
end
|
67
65
|
|
68
66
|
context 'to_s' do
|
69
67
|
it do
|
70
|
-
|
68
|
+
Model.first.to_s.should == 'A'
|
71
69
|
end
|
72
70
|
end
|
73
71
|
|
74
72
|
context 'instance accessor' do
|
75
73
|
it do
|
76
|
-
assert
|
77
|
-
assert
|
78
|
-
assert
|
74
|
+
assert Model.first.attributes
|
75
|
+
assert Model.first.key
|
76
|
+
assert Model.first.code
|
79
77
|
end
|
80
78
|
end
|
81
79
|
|
@@ -97,8 +95,9 @@ RSpec.describe MemoryRecord do
|
|
97
95
|
end
|
98
96
|
|
99
97
|
it 'Name method is automatically defined if it is not defined' do
|
100
|
-
model = class_new []
|
98
|
+
model = class_new [{key: :foo}]
|
101
99
|
assert_equal true, model.instance_methods.include?(:name)
|
100
|
+
assert_equal "foo", model[:foo].name
|
102
101
|
end
|
103
102
|
end
|
104
103
|
|
@@ -154,14 +153,6 @@ RSpec.describe MemoryRecord do
|
|
154
153
|
end
|
155
154
|
end
|
156
155
|
|
157
|
-
describe 'Human_attribute_name can not be used in anonymous class' do
|
158
|
-
let(:model) { class_new [{foo: 1}] }
|
159
|
-
|
160
|
-
it 'It does not cause an error' do
|
161
|
-
model.first.name.should == nil
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
156
|
describe 'attr_reader option' do
|
166
157
|
def element(options)
|
167
158
|
Class.new {
|
@@ -211,4 +202,22 @@ RSpec.describe MemoryRecord do
|
|
211
202
|
end
|
212
203
|
model[:a].should == :a
|
213
204
|
end
|
205
|
+
|
206
|
+
it "minus" do
|
207
|
+
m = class_new [{key: :a}, {key: :b}]
|
208
|
+
([m[:a], m[:b]] - [m[:a]]).should == [m[:b]]
|
209
|
+
end
|
210
|
+
|
211
|
+
describe "sort (<=> method)" do
|
212
|
+
it "same class" do
|
213
|
+
m = class_new [{key: :a}, {key: :b}]
|
214
|
+
[m[:b], m[:a]].sort.should == [m[:a], m[:b]]
|
215
|
+
end
|
216
|
+
|
217
|
+
it "different class" do
|
218
|
+
m1 = class_new [{key: :a}, {key: :b}]
|
219
|
+
m2 = class_new [{key: :a}, {key: :b}]
|
220
|
+
expect { [m1[:b], m2[:a]].sort }.to raise_error(ArgumentError)
|
221
|
+
end
|
222
|
+
end
|
214
223
|
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.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- akicho8
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-12-
|
11
|
+
date: 2017-12-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -66,20 +66,6 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: activemodel
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :runtime
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
69
|
description: A simple library that handles a few records easily
|
84
70
|
email:
|
85
71
|
- akicho8@gmail.com
|
@@ -112,6 +98,8 @@ files:
|
|
112
98
|
- examples/0250_lookup_super_call.rb
|
113
99
|
- examples/0260_also_refer_to_other_keys.rb
|
114
100
|
- examples/0270_instance_freeze.rb
|
101
|
+
- examples/0280_sortable.rb
|
102
|
+
- examples/0290_eql_behavior.rb
|
115
103
|
- lib/memory_record.rb
|
116
104
|
- lib/memory_record/memory_record.rb
|
117
105
|
- lib/memory_record/version.rb
|
@@ -138,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
126
|
version: '0'
|
139
127
|
requirements: []
|
140
128
|
rubyforge_project:
|
141
|
-
rubygems_version: 2.
|
129
|
+
rubygems_version: 2.7.3
|
142
130
|
signing_key:
|
143
131
|
specification_version: 4
|
144
132
|
summary: A simple library that handles a few records easily
|