memory_record 0.0.9 → 0.0.10

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
2
  SHA256:
3
- metadata.gz: e6a9d0e0253067464bc3fa778dab80bf47b635752094879aa4ad4f1a95ef6773
4
- data.tar.gz: 2fd9222c966e16931cff928629e2d7a50237a6c39b7be2266f12eeb8fc80ea87
3
+ metadata.gz: 2c8085a6c6fdc528864f2ba6ea2314864ee756ec70e3c0f084aefe284e2bdc33
4
+ data.tar.gz: aa03716f97fc13275339ca8a7432b4b1a298465c91e09848141c0762e0fea563
5
5
  SHA512:
6
- metadata.gz: c02532cc3ad012939336002e2cdad14e7a7afd0ba9684d5c510117454c915b9766dc3305a5c4737f1619e4db4439df9c723e52132f753772f2aa7bc45be9d76c
7
- data.tar.gz: 18cd2b7a7378fca5a9a0d511c8789e8ed7c021c21295fb4c21a7f4060bf453db04b7de819b91df66be6d8fafe4ba8d9399a7e0a3a3345f7f5837ef0868caea33
6
+ metadata.gz: ff89e669f37fa84e06fa8cf71ed23b3116018cf5608212eadb5707640244dd800bc5f0bb1d89a227db055cb2cf608c355b4a677f853c37f275ec9f92d32ee2d6
7
+ data.tar.gz: ab3c487124bcd4795b517ee2e9cef6d4db37e10d2c6ac7668930efaae422223495a5c2332c579ffb9a6f9c11b0a56a5a81aa5fb79b19b0c266c96469fd2fec9d
@@ -1,28 +1,34 @@
1
- * MemoryRecord
1
+ # MemoryRecord
2
2
 
3
- A simple library that handles a few records easily.
4
- With this library can flexibly managed immutable data.
3
+ [![Build Status](https://travis-ci.org/akicho8/memory_record.svg?branch=master)](https://travis-ci.org/akicho8/memory_record)
4
+ [![Gem Version](https://badge.fury.io/rb/memory_record.svg)](https://badge.fury.io/rb/memory_record)
5
+ [![Dependency Status](https://gemnasium.com/badges/github.com/akicho8/memory_record.svg)](https://gemnasium.com/github.com/akicho8/memory_record)
5
6
 
6
- ** Installation
7
+ ## Introduction
7
8
 
8
- Install as a standalone gem
9
+ A simple library that handles a few records easily.
10
+ With this library can flexibly managed immutable data.
9
11
 
10
- #+BEGIN_SRC shell
12
+ ## Installation
13
+
14
+ Install as a standalone gem
15
+
16
+ ```shell
11
17
  $ gem install memory_record
12
- #+END_SRC
18
+ ```
13
19
 
14
20
  Or install within application using Gemfile
15
21
 
16
- #+BEGIN_SRC shell
22
+ ```shell
17
23
  $ bundle add memory_record
18
24
  $ bundle install
19
- #+END_SRC
25
+ ```
20
26
 
21
- ** Examples
27
+ ## Examples
22
28
 
23
- *** Basic usage
29
+ ### Basic usage
24
30
 
25
- #+BEGIN_SRC ruby
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
- #+END_SRC
52
+ ```
47
53
 
48
- *** How to turn as an array?
54
+ ### How to turn as an array?
49
55
 
50
- =Enumerable= extended, so that =each= method is available
56
+ **Enumerable** extended, so that **each** method is available
51
57
 
52
- #+BEGIN_SRC ruby
58
+ ```ruby
53
59
  Foo.each { |e| ... }
54
60
  Foo.collect { |e| ... }
55
- #+END_SRC
61
+ ```
56
62
 
57
- *** How do I submit a form to select in Rails?
63
+ ### How do I submit a form to select in Rails?
58
64
 
59
- #+BEGIN_SRC ruby
65
+ ```ruby
60
66
  form.collection_select(:selection_code, Foo, :code, :name)
61
- #+END_SRC
67
+ ```
62
68
 
63
- *** Is the reference in subscripts slow?
69
+ ### Is the reference in subscripts slow?
64
70
 
65
- Since it has a hash internally using the key value as a key, it can be acquired with O (1).
71
+ Since it has a hash internally using the key value as a key, it can be acquired with O (1).
66
72
 
67
- #+BEGIN_SRC ruby
73
+ ```ruby
68
74
  Foo[1].name # => "A"
69
75
  Foo[:a].name # => "A"
70
- #+END_SRC
76
+ ```
71
77
 
72
- *** Instances always react to =code= and =key=
78
+ ### Instances always react to **code** and **key**
73
79
 
74
- #+BEGIN_SRC ruby
80
+ ```ruby
75
81
  object = Foo.first
76
82
  object.key # => :a
77
83
  object.code # => 1
78
- #+END_SRC
84
+ ```
79
85
 
80
- *** How do I add a method to an instance?
86
+ ### How do I add a method to an instance?
81
87
 
82
- For that, I am creating a new class so I need to define it normally
88
+ For that, I am creating a new class so I need to define it normally
83
89
 
84
- *** =name= method is special?
90
+ ### **name** method is special?
85
91
 
86
- If =name= is not defined, it defines a =name= method that returns =key.to_s=
92
+ If **name** is not defined, it defines a **name** method that returns **key.to_s**
87
93
 
88
- *** =to_s= method is defined?
94
+ ### **to_s** method is defined?
89
95
 
90
- Alias of =name=, =to_s= is defined.
96
+ Alias of **name**, **to_s** is defined.
91
97
 
92
- *** If there is no key, use fetch to get an error
98
+ ### If there is no key, use fetch to get an error
93
99
 
94
- #+BEGIN_SRC ruby
100
+ ```ruby
95
101
  Foo.fetch(:xxx) # => <KeyError: ...>
96
- #+END_SRC
102
+ ```
97
103
 
98
- The following are all the same
104
+ The following are all the same
99
105
 
100
- #+BEGIN_SRC ruby
106
+ ```ruby
101
107
  Foo[:xxx] || :default # => :default
102
108
  Foo.fetch(:xxx, :default} # => :default
103
109
  Foo.fetch(:xxx) { :default } # => :default
104
- #+END_SRC
110
+ ```
105
111
 
106
- *** Use fetch_if to ignore if the key is nil
112
+ ### Use fetch_if to ignore if the key is nil
107
113
 
108
- #+BEGIN_SRC ruby
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
- #+END_SRC
118
+ ```
113
119
 
114
- *** How to refer to other keys
120
+ ### How to refer to other keys
115
121
 
116
- #+BEGIN_SRC ruby
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
- #+END_SRC
147
+ ```
142
148
 
143
- *** How can I prohibit the hash key from being attr_reader automatically?
149
+ ### How can I prohibit the hash key from being attr_reader automatically?
144
150
 
145
- **** attr_reader: false
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
- #+BEGIN_SRC ruby
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
- #+END_SRC
168
+ ```
163
169
 
164
- **** attr_reader: {only: :y}
170
+ #### attr_reader: {only: :y}
165
171
 
166
- #+BEGIN_SRC ruby
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
- #+END_SRC
185
+ ```
180
186
 
181
- **** attr_reader: {except: :y}
187
+ #### attr_reader: {except: :y}
182
188
 
183
- #+BEGIN_SRC ruby
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
- #+END_SRC
202
+ ```
197
203
 
198
- *** How to decide =code= yourself?
204
+ *** How to decide **code** yourself?
199
205
 
200
- #+BEGIN_SRC ruby
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
- #+END_SRC
217
+ ```
212
218
 
213
- It is not recommended to specify it explicitly.
214
- It is useful only when refactoring legacy code with compatibility in mind.
219
+ It is not recommended to specify it explicitly.
220
+ It is useful only when refactoring legacy code with compatibility in mind.
@@ -0,0 +1,16 @@
1
+ $LOAD_PATH.unshift '../lib'
2
+ require 'memory_record'
3
+
4
+ class C
5
+ include MemoryRecord
6
+ memory_record [
7
+ {key: :a},
8
+ ]
9
+ end
10
+
11
+ a = C[:a]
12
+ b = Marshal.load(Marshal.dump(a))
13
+
14
+ h = {}
15
+ h[a] = true
16
+ h[b] # => true
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift '../lib'
2
+ require 'memory_record'
3
+
4
+ class C
5
+ include MemoryRecord
6
+ memory_record [
7
+ {key: :a},
8
+ ]
9
+ end
10
+
11
+ a = C[:a]
12
+ b = Marshal.load(Marshal.dump(a))
13
+ a == b # => true
@@ -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
 
@@ -1,3 +1,3 @@
1
1
  module MemoryRecord
2
- VERSION = '0.0.9'
2
+ VERSION = '0.0.10'
3
3
  end
@@ -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.9
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: 2017-12-31 00:00:00.000000000 Z
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.org
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