memory_record 0.0.7 → 0.0.8

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
  SHA1:
3
- metadata.gz: 4a2317107890629490df470cd71e94e9f2b06d81
4
- data.tar.gz: 0bd134e177bd057e4079e07fe63092dc60eb90d5
3
+ metadata.gz: c8dfcb893c88a45a5e788f4e1b190b9dcd9647b9
4
+ data.tar.gz: 808381a0ccdb75f0600e5e6618b7755c7466c995
5
5
  SHA512:
6
- metadata.gz: 801976a4509a4aaccdce5f2b951221316690dc9d47ae650e535598e2029e00edf8d0d1619d32642eb1725cd601ed8b125aa42558607649a0ab7c5d7bcdddd6ee
7
- data.tar.gz: c7f267a44e6fc507366af02895ffa5c0ae8f4134049948e3f5cc1f501afeb0784fcaed272c8c4013f24e388c80c908ba4e78944aac09ba4f146e74baf238a4c9
6
+ metadata.gz: d598fd06e27533ce8aadd8531c2b944f592e16e4f9bdeb515af9f593d3a506062e9a0f0d3692f804d11e23fa3eaf8364fd98e3bf89d990831684e4baf9a0a1a5
7
+ data.tar.gz: 6848273b2c34f2ffb6c9914a135edc278954cd1989a7d54abee1d62ec55aea2be31c8ad1d348ec5d797ecf23bb4ee9227fd70ba6ad5e14eb49d18fa9133da2b3
data/README.org CHANGED
@@ -1,6 +1,7 @@
1
1
  * MemoryRecord
2
2
 
3
- A simple library that handles a few records easily
3
+ A simple library that handles a few records easily.
4
+ With this library can flexibly managed immutable data.
4
5
 
5
6
  ** Installation
6
7
 
@@ -110,6 +111,35 @@ Foo.fetch_if(:a) # => #<Foo:... @attributes={...}>
110
111
  Foo.fetch_if(:xxx) # => <KeyError: ...>
111
112
  #+END_SRC
112
113
 
114
+ *** How to refer to other keys
115
+
116
+ #+BEGIN_SRC ruby
117
+ class Foo
118
+ include MemoryRecord
119
+ memory_record [
120
+ {key: :a, other_key: :x},
121
+ {key: :b, other_key: :y},
122
+ {key: :c, other_key: :z},
123
+ ]
124
+
125
+ class << self
126
+ def lookup(v)
127
+ super || invert_table[v]
128
+ end
129
+
130
+ private
131
+
132
+ def invert_table
133
+ @invert_table ||= inject({}) {|a, e| a.merge(e.other_key => e) }
134
+ end
135
+ end
136
+ end
137
+
138
+ Foo[:a] == Foo[:x] # => true
139
+ Foo[:b] == Foo[:y] # => true
140
+ Foo[:c] == Foo[:z] # => true
141
+ #+END_SRC
142
+
113
143
  *** How can I prohibit the hash key from being attr_reader automatically?
114
144
 
115
145
  **** attr_reader: false
@@ -182,4 +212,3 @@ Foo.collect(&:code) # => [1, 2, 3]
182
212
 
183
213
  It is not recommended to specify it explicitly.
184
214
  It is useful only when refactoring legacy code with compatibility in mind.
185
-
@@ -1,6 +1,8 @@
1
1
  $LOAD_PATH.unshift '../lib'
2
2
  require 'memory_record'
3
3
 
4
+ # It is better to stop allocating code yourself
5
+
4
6
  class Foo
5
7
  include MemoryRecord
6
8
  memory_record [
@@ -9,7 +9,7 @@ class Direction
9
9
  ]
10
10
 
11
11
  def long_name
12
- "#{name}方向"
12
+ "#{name} direction"
13
13
  end
14
14
  end
15
15
 
@@ -19,7 +19,7 @@ Direction.keys # => [:left, :right]
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 # => "→ direction"
23
23
 
24
24
  Direction[1].key # => :right
25
25
 
@@ -8,14 +8,14 @@ end
8
8
 
9
9
  C.keys # => [:a]
10
10
 
11
- # memory_record では更新できない
11
+ # Can not update with memory_record
12
12
  class C
13
13
  memory_record [{key: :b}]
14
14
  end
15
15
 
16
16
  C.keys # => [:a]
17
17
 
18
- # memory_record_reset を使うこと
18
+ # With memory_record_reset you can rebuild data
19
19
  C.memory_record_reset [{key: :c}]
20
20
 
21
21
  C.keys # => [:c]
@@ -4,8 +4,8 @@ require 'memory_record'
4
4
  class Foo
5
5
  include MemoryRecord
6
6
  memory_record [
7
- {key: '↑', name: ''},
7
+ {key: '↑', name: 'UP'},
8
8
  ]
9
9
  end
10
10
 
11
- Foo['↑'].name # => ""
11
+ Foo['↑'].name # => "UP"
@@ -4,11 +4,11 @@ require 'memory_record'
4
4
  class Foo
5
5
  include MemoryRecord
6
6
  memory_record [
7
- {key: 'true', name: '有効'},
8
- {key: 'false', name: '無効'},
7
+ {key: 'true', name: 'ON'},
8
+ {key: 'false', name: 'OFF'},
9
9
  ]
10
10
  end
11
11
 
12
12
  flag = true
13
13
 
14
- Foo[flag.to_s].name # => "有効"
14
+ Foo[flag.to_s].name # => "ON"
@@ -20,4 +20,4 @@ Foo.collect(&:valid?) # => [false, true]
20
20
 
21
21
  foo = Foo.first
22
22
  foo.valid? # => false
23
- foo.errors.full_messages # => ['Name is too long (maximum is 3 characters)']
23
+ foo.errors.full_messages # => ["Name is too long (maximum is 3 characters)"]
@@ -1,12 +1,14 @@
1
1
  $LOAD_PATH.unshift '../lib'
2
2
  require 'memory_record'
3
3
 
4
+ # An error occurs if key is duplicated
5
+
4
6
  class Foo
5
7
  include MemoryRecord
6
- memory_record [{key: :a}, {key: :a},] rescue $! # => #<ArgumentError: Foo#key :a が重複しています
8
+ memory_record [{key: :a}, {key: :a},] rescue $! # => #<ArgumentError: Foo#key :a is duplicate
7
9
  end
8
10
 
9
11
  class Bar
10
12
  include MemoryRecord
11
- memory_record [{code: 0}, {code: 0},] rescue $! # => #<ArgumentError: Bar#code 0 が重複しています
13
+ memory_record [{code: 0}, {code: 0},] rescue $! # => #<ArgumentError: Bar#code 0 is duplicate
12
14
  end
@@ -0,0 +1,17 @@
1
+ $LOAD_PATH.unshift '../lib'
2
+ require 'memory_record'
3
+
4
+ # I can do it like this, but I can not recommend it at all
5
+ # Do not increase the magic number
6
+ # Let"s see what key there is for
7
+
8
+ class Foo
9
+ include MemoryRecord
10
+ memory_record [
11
+ {key: "01", name: "left"},
12
+ {key: "02", name: "right"},
13
+ ]
14
+ end
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}>
@@ -3,7 +3,7 @@ require 'memory_record'
3
3
 
4
4
  require 'active_record'
5
5
 
6
- ActiveRecord::VERSION::STRING # => "5.1.3"
6
+ ActiveRecord::VERSION::STRING # => "5.1.4"
7
7
  ActiveRecord::Migration.verbose = false
8
8
  ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
9
9
 
@@ -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:0x007ff28323d480>>
14
- C1.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<C1:0x007ff28323d480>>
15
- C1.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<C1:0x007ff28323d480>>
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>>
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:0x007ff2838d8880>>
26
+ C2.first.x rescue $! # => #<NoMethodError: undefined method `x' for #<C2:0x007f93578e2550>>
27
27
  C2.first.y rescue $! # => 1
28
- C2.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<C2:0x007ff2838d8880>>
28
+ C2.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<C2:0x007f93578e2550>>
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:0x007ff28321f7c8>>
40
+ C3.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<C3:0x007f93578c2278>>
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:0x007f954e834fb0 @attributes={:key=>:alice, :code=>0}>
19
- C[:alice] # => #<C:0x007f954e834fb0 @attributes={:key=>:alice, :code=>0}>
18
+ C.lookup(:alice) # => #<C:0x007f92c2addf98 @attributes={:key=>:alice, :code=>0}>
19
+ C[:alice] # => #<C:0x007f92c2addf98 @attributes={:key=>:alice, :code=>0}>
@@ -0,0 +1,27 @@
1
+ $LOAD_PATH.unshift '../lib'
2
+ require 'memory_record'
3
+
4
+ class Foo
5
+ include MemoryRecord
6
+ memory_record [
7
+ {key: :a, other_key: :x},
8
+ {key: :b, other_key: :y},
9
+ {key: :c, other_key: :z},
10
+ ]
11
+
12
+ class << self
13
+ def lookup(v)
14
+ super || invert_table[v]
15
+ end
16
+
17
+ private
18
+
19
+ def invert_table
20
+ @invert_table ||= inject({}) {|a, e| a.merge(e.other_key => e) }
21
+ end
22
+ end
23
+ end
24
+
25
+ Foo[:a] == Foo[:x] # => true
26
+ Foo[:b] == Foo[:y] # => true
27
+ Foo[:c] == Foo[:z] # => true
@@ -0,0 +1,22 @@
1
+ $LOAD_PATH.unshift '../lib'
2
+ require 'memory_record'
3
+
4
+ class Foo
5
+ include MemoryRecord
6
+ memory_record [
7
+ {key: :a},
8
+ ]
9
+
10
+ def a
11
+ @var ||= 1
12
+ end
13
+
14
+ each do |e|
15
+ e.a
16
+ e.freeze
17
+ end
18
+ end
19
+
20
+ v = Foo.first # => #<Foo:0x007fd0c09cfc50 @attributes={:key=>:a, :code=>0}, @var=1>
21
+ v.frozen? # => true
22
+ v.a # => 1
@@ -157,10 +157,13 @@ module MemoryRecord
157
157
  def memory_record_reset(records)
158
158
  @keys = nil
159
159
  @codes = nil
160
+
160
161
  @values = records.collect.with_index { |e, i|
161
162
  new(_attributes_normalize(e, i))
162
163
  }.freeze
164
+
163
165
  @values_hash = {}
166
+
164
167
  [:code, :key].each do |pk|
165
168
  @values_hash[pk] = @values.inject({}) do |a, e|
166
169
  a.merge(e.send(pk) => e) do |key, a, b|
@@ -1,3 +1,3 @@
1
1
  module MemoryRecord
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.8'
3
3
  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.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - akicho8
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-19 00:00:00.000000000 Z
11
+ date: 2017-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -104,12 +104,14 @@ files:
104
104
  - examples/0170_tips_on_handling_boolean_type.rb
105
105
  - examples/0180_freeze_makes_it_impossible_to_memorize.rb
106
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
107
+ - examples/0200_with_active_model_validation.rb
108
+ - examples/0210_key_duplicate_check.rb
109
+ - examples/0220_bad_design.rb
110
110
  - examples/0230_with_use_activerecord_enum.rb
111
111
  - examples/0240_attr_reader_option.rb
112
112
  - examples/0250_lookup_super_call.rb
113
+ - examples/0260_also_refer_to_other_keys.rb
114
+ - examples/0270_instance_freeze.rb
113
115
  - lib/memory_record.rb
114
116
  - lib/memory_record/memory_record.rb
115
117
  - lib/memory_record/version.rb
@@ -1,17 +0,0 @@
1
- $LOAD_PATH.unshift '../lib'
2
- require 'memory_record'
3
-
4
- class Foo
5
- include MemoryRecord
6
- memory_record [
7
- {key: '01', name: '→'},
8
- {key: '02', name: '←'},
9
- ]
10
- end
11
-
12
- Foo['01'] # => #<Foo:0x007fac2c35db28 @attributes={:key=>:'01', :name=>'→', :code=>0}>
13
- Foo['02'] # => #<Foo:0x007fac2c35d9e8 @attributes={:key=>:'02', :name=>'←', :code=>1}>
14
-
15
- # このようにもできるがまったくオススメできない
16
- # マジックナンバーを増やすメリットはない
17
- # なんのためにキーがあるのか考えよう