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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c8dfcb893c88a45a5e788f4e1b190b9dcd9647b9
4
- data.tar.gz: 808381a0ccdb75f0600e5e6618b7755c7466c995
2
+ SHA256:
3
+ metadata.gz: e6a9d0e0253067464bc3fa778dab80bf47b635752094879aa4ad4f1a95ef6773
4
+ data.tar.gz: 2fd9222c966e16931cff928629e2d7a50237a6c39b7be2266f12eeb8fc80ea87
5
5
  SHA512:
6
- metadata.gz: d598fd06e27533ce8aadd8531c2b944f592e16e4f9bdeb515af9f593d3a506062e9a0f0d3692f804d11e23fa3eaf8364fd98e3bf89d990831684e4baf9a0a1a5
7
- data.tar.gz: 6848273b2c34f2ffb6c9914a135edc278954cd1989a7d54abee1d62ec55aea2be31c8ad1d348ec5d797ecf23bb4ee9227fd70ba6ad5e14eb49d18fa9133da2b3
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 a translation of =key=
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
 
@@ -17,4 +17,4 @@ class Foo
17
17
  end
18
18
 
19
19
  Foo.first.a # => 20
20
- Foo.first.name # => "(Key0)"
20
+ Foo.first.name # => "(_key0)"
@@ -13,5 +13,5 @@ class Foo
13
13
  ]
14
14
  end
15
15
 
16
- Foo["01"] # => #<Foo:0x007f9db5ac1b10 @attributes={:key=>:"01", :name=>"left", :code=>0}>
17
- Foo["02"] # => #<Foo:0x007f9db5ac15e8 @attributes={:key=>:"02", :name=>"right", :code=>1}>
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:0x007f93579027d8>>
14
- C1.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<C1:0x007f93579027d8>>
15
- C1.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<C1:0x007f93579027d8>>
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:0x007f93578e2550>>
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:0x007f93578e2550>>
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:0x007f93578c2278>>
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:0x007f92c2addf98 @attributes={:key=>:alice, :code=>0}>
19
- C[:alice] # => #<C:0x007f92c2addf98 @attributes={:key=>:alice, :code=>0}>
18
+ C.lookup(:alice) # => #<C:0x00007ffe599ba4d8 @attributes={:key=>:alice, :code=>0}>
19
+ C[:alice] # => #<C:0x00007ffe599ba4d8 @attributes={:key=>:alice, :code=>0}>
@@ -17,6 +17,6 @@ class Foo
17
17
  end
18
18
  end
19
19
 
20
- v = Foo.first # => #<Foo:0x007fd0c09cfc50 @attributes={:key=>:a, :code=>0}, @var=1>
20
+ v = Foo.first # => #<Foo:0x00007fdc05a532d0 @attributes={:key=>:a, :code=>0}, @var=1>
21
21
  v.frozen? # => true
22
22
  v.a # => 1
@@ -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 'active_model'
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.class_eval do
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.class_eval do
76
- define_method(:name) do
77
- if self.class.name
78
- self.class.human_attribute_name(key)
79
- end
80
- end
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)
@@ -1,3 +1,3 @@
1
1
  module MemoryRecord
2
- VERSION = '0.0.8'
2
+ VERSION = '0.0.9'
3
3
  end
@@ -22,5 +22,4 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency 'test-unit'
23
23
 
24
24
  spec.add_dependency 'activesupport'
25
- spec.add_dependency 'activemodel'
26
25
  end
@@ -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
- instance[:name].should == 'A'
64
- instance[:xxxx].should == nil
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
- instance.to_s.should == 'A'
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 instance.attributes
77
- assert instance.key
78
- assert instance.code
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.8
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 00:00:00.000000000 Z
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.6.11
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