memory_record 0.0.15 → 0.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5782c36c4656fbfea525ca83150171f5e7d72ef6e12c4a3bc5bb0c9cbebf999d
4
- data.tar.gz: ae386371adb57bc10e97a3ec86c732eeb343fa53912ae7fc75fe6ddb41d15ae0
3
+ metadata.gz: b1ff4966063583e1b982805aaa2cb70ca4a4489498892280850e43b608def31a
4
+ data.tar.gz: 3efe77744703e2765bcc77f8e5888ba568ac5ba88ce29f7c1afaa13059e9fe86
5
5
  SHA512:
6
- metadata.gz: 2c93d3d610e960a3a6b8da1041e8e6814de11ea93f5b341a18c39d8f80cf502b7bb05f1b07f1fa32fe575270809df31de441928bb0ec123cde3d0c02c7335a04
7
- data.tar.gz: fb32e99ced614b0dc378662072f769e59d2bf28578537d48f734a078d793fc2b861f99bd96dff1496ed62ce80ddf10717ef4a861beff64a794ce4b00fd739866
6
+ metadata.gz: 34edd3b363bc9fe46aea07e9ea3626ece155a7cd5aefbafb90319a49a2d8c9a48dc7b23940478b40a19ea364a38ebe99e383ba9e620894dfa281e9f8c642b4ed
7
+ data.tar.gz: 371a5cd5f00281683650b9614b036b0bb70d964e8cd24b5bd147ef62de988be36ebc8887bc54a8b60bc019d7389fcd05b85697cbaea7258eca03389252500a12
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  /*.html
19
+ *.ruby-version
data/README.md CHANGED
@@ -27,7 +27,44 @@ $ bundle install
27
27
 
28
28
  ## Examples
29
29
 
30
- ### Basic usage
30
+ ### Basic usage 1
31
+
32
+ ```ruby
33
+ class Palette
34
+ include MemoryRecord
35
+ memory_record [
36
+ { key: :coral, },
37
+ { key: :tomato, },
38
+ { key: :gold, },
39
+ ]
40
+ end
41
+
42
+ Palette[:tomato].key # => :tomato
43
+ Palette[:tomato].name # => "tomato"
44
+ Palette[:tomato].code # => 1
45
+ ```
46
+
47
+ ### Basic usage 2
48
+
49
+ ```ruby
50
+ class Palette
51
+ include MemoryRecord
52
+ memory_record [
53
+ { key: :coral, r: 255, g: 127, b: 0 },
54
+ { key: :tomato, r: 255, g: 99, b: 71 },
55
+ { key: :gold, r: 255, g: 215, b: 0 },
56
+ ]
57
+ end
58
+
59
+ Palette[:tomato].key # => :tomato
60
+ Palette[:tomato].name # => "tomato"
61
+ Palette[:tomato].code # => 1
62
+ Palette[:tomato].r # => 255
63
+ Palette[:tomato].g # => 99
64
+ Palette[:tomato].b # => 71
65
+ ```
66
+
67
+ ### Basic usage 3
31
68
 
32
69
  ```ruby
33
70
  class Palette
@@ -53,6 +90,7 @@ end
53
90
 
54
91
  Palette[:tomato].key # => :tomato
55
92
  Palette[:tomato].name # => "Tomato"
93
+ Palette[:tomato].code # => 1
56
94
  Palette[:tomato].rgb # => [255, 99, 71]
57
95
  Palette[:tomato].hex # => "#FF6347"
58
96
  Palette.collect(&:hex) # => ["#FF7F00", "#FF6347", "#FFD700"]
@@ -127,6 +165,26 @@ Palette.fetch_if(:tomato) # => #<Palette:... @attributes={...}>
127
165
  Palette.fetch_if(:xxx) # => <KeyError: ...>
128
166
  ```
129
167
 
168
+ ### Key validation
169
+
170
+ ```ruby
171
+ Palette.valid_key(:tomato) # => :tomato
172
+ Palette.valid_key("tomato") # => :tomato
173
+ Palette.valid_key(1) # => :tomato
174
+ Palette.valid_key(:xxx) # => nil
175
+ Palette.valid_key(:xxx, :tomato) # => :tomato
176
+ Palette.valid_key(:xxx) { :tomato } # => :tomato
177
+
178
+ Palette.valid_key(:xxx, :tomato) # => :tomato
179
+ Palette.valid_key(:xxx, :black) rescue $! # => #<KeyError: Palette.fetch(:black) does not match anything
180
+ ```
181
+
182
+ #### A common example in Rails
183
+
184
+ ```ruby
185
+ palette_key = Palette.valid_key(params[:palette_key], :tomato)
186
+ ```
187
+
130
188
  ### How to refer to other keys
131
189
 
132
190
  ```ruby
@@ -217,6 +275,7 @@ class ColorInfo
217
275
  end
218
276
 
219
277
  color_info = ColorInfo.first
278
+ color_info.as_json # => {:key=>:blue, :rgb=>[0, 0, 255], :a=>1, :code=>0}
220
279
  color_info.as_json(only: :key) # => {:key => :blue}
221
280
  color_info.as_json(except: [:rgb, :code]) # => {:key => :blue}
222
281
  color_info.as_json(only: [], methods: :hex) # => {:hex => "#0000FF"}
@@ -224,6 +283,7 @@ color_info.as_json(only: [], include: {children: {only: :x}} ) # => {:children =
224
283
 
225
284
  ColorInfo.as_json(only: :key) # => [{:key=>:blue}, {:key=>:red}]
226
285
  ColorInfo.to_json(only: :key) # => "[{\"key\":\"blue\"},{\"key\":\"red\"}]"
286
+ ```
227
287
 
228
288
  #### Case of ActiveModelSerializers
229
289
 
@@ -27,3 +27,13 @@ Palette[:tomato].name # => "Tomato"
27
27
  Palette[:tomato].rgb # => [255, 99, 71]
28
28
  Palette[:tomato].hex # => "#FF6347"
29
29
  Palette.collect(&:hex) # => ["#FF7F00", "#FF6347", "#FFD700"]
30
+
31
+ Palette.valid_key(:tomato) # => :tomato
32
+ Palette.valid_key("tomato") # => :tomato
33
+ Palette.valid_key(1) # => :tomato
34
+ Palette.valid_key(:xxx) # => nil
35
+ Palette.valid_key(:xxx, :tomato) # => :tomato
36
+ Palette.valid_key(:xxx) { :tomato } # => :tomato
37
+
38
+ Palette.valid_key(:xxx, :tomato) # => :tomato
39
+ Palette.valid_key(:xxx, :black) rescue $! # => #<KeyError: Palette.fetch(:black) does not match anything
@@ -38,6 +38,7 @@ end
38
38
 
39
39
  ColorInfo.first.key # => :blue
40
40
 
41
+ ColorInfo.first.as_json # => {:key=>:blue, :rgb=>[0, 0, 255], :a=>1, :code=>0}
41
42
  ColorInfo.first.as_json(only: :key) # => {:key=>:blue}
42
43
  ColorInfo.first.as_json(except: [:rgb, :code, :a]) # => {:key=>:blue}
43
44
  ColorInfo.first.as_json(only: [], methods: :hex) # => {:hex=>"#0000FF"}
@@ -2,7 +2,7 @@ require "bundler/inline"
2
2
 
3
3
  gemfile do
4
4
  gem "memory_record", path: ".."
5
- gem "active_model_serializers"
5
+ gem "active_model_serializers", "0.10.7" # It does not work with version 0.10.10
6
6
  end
7
7
 
8
8
  class ColorInfo
@@ -18,5 +18,5 @@ class ColorInfoSerializer < ActiveModel::Serializer
18
18
  end
19
19
 
20
20
  pp ActiveModelSerializers::SerializableResource.new(ColorInfo.first).as_json # => {:key=>:blue, :name=>"blue"}
21
- # >> [active_model_serializers] Rendered ColorInfoSerializer with ActiveModelSerializers::Adapter::Attributes (0.14ms)
21
+ # >> [active_model_serializers] Rendered ColorInfoSerializer with ActiveModelSerializers::Adapter::Attributes (0.12ms)
22
22
  # >> {:key=>:blue, :name=>"blue"}
@@ -4,21 +4,21 @@ require 'memory_record'
4
4
  class A
5
5
  include MemoryRecord
6
6
  memory_record [
7
- { key: :a, x: 1, y: 2 },
7
+ { key: :foo },
8
8
  ]
9
9
 
10
- def z
11
- x + y
10
+ def name
11
+ super.to_s.upcase
12
12
  end
13
13
  end
14
14
 
15
15
  class B < A
16
16
  memory_record_reset superclass.collect(&:attributes)
17
17
 
18
- def z
19
- super * 2
18
+ def name
19
+ "(#{super})"
20
20
  end
21
21
  end
22
22
 
23
- B.values # => [#<B:0x00007feea40395b8 @attributes={:key=>:a, :x=>1, :y=>2, :code=>0}>]
24
- B.first.z # => 6
23
+ A.first.name # => "FOO"
24
+ B.first.name # => "(FOO)"
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift '../lib'
2
+ require 'memory_record'
3
+
4
+ class Color
5
+ include MemoryRecord
6
+ memory_record [
7
+ { key: :blue },
8
+ ]
9
+ end
10
+
11
+ Color.valid_key(:blue) # => :blue
12
+ Color.valid_key(:unknown) # => nil
13
+ Color.valid_key(:unknown, :blue) # => :blue
14
+ Color.valid_key(:unknown) { :blue } # => :blue
15
+ Color.valid_key(:unknown) { :xxx } rescue $! # => #<KeyError: Color.fetch(:xxx) does not match anything
@@ -36,7 +36,7 @@ module MemoryRecord
36
36
  hash
37
37
  end
38
38
 
39
- def as_json(**options)
39
+ def as_json(options = {})
40
40
  serializable_hash(options)
41
41
  end
42
42
 
@@ -15,16 +15,16 @@ module MemoryRecord
15
15
  # Example
16
16
  #
17
17
  # memory_record [
18
- # {id: 1, name: "alice"},
19
- # {id: 2, name: "bob" },
18
+ # { id: 1, name: "alice" },
19
+ # { id: 2, name: "bob" },
20
20
  # ], attr_reader: false
21
21
  #
22
22
  # or
23
23
  #
24
24
  # memory_record(attr_reader: false) do
25
25
  # [
26
- # {id: 1, name: "alice"},
27
- # {id: 2, name: "bob" },
26
+ # { id: 1, name: "alice" },
27
+ # { id: 2, name: "bob" },
28
28
  # ]
29
29
  # end
30
30
  #
@@ -152,6 +152,25 @@ module MemoryRecord
152
152
  end
153
153
  end
154
154
 
155
+ # Color.valid_key(:blue) # => :blue
156
+ # Color.valid_key(:unknown) # => nil
157
+ # Color.valid_key(:unknown, :blue) # => :blue
158
+ # Color.valid_key(:unknown) { :blue } # => :blue
159
+ # Color.valid_key(:unknown) { :xxx } # => KeyError Exception
160
+ def valid_key(key, default = nil, &block)
161
+ if block_given? && default
162
+ raise ArgumentError, "Can't use default and block together"
163
+ end
164
+
165
+ if e = lookup(key)
166
+ e.key
167
+ elsif block_given?
168
+ fetch(yield).key
169
+ elsif default
170
+ fetch(default).key
171
+ end
172
+ end
173
+
155
174
  delegate :each, to: :values
156
175
 
157
176
  def keys
@@ -1,3 +1,3 @@
1
1
  module MemoryRecord
2
- VERSION = '0.0.15'
2
+ VERSION = '0.0.19'
3
3
  end
@@ -166,7 +166,7 @@ RSpec.describe MemoryRecord do
166
166
  def element(options)
167
167
  Class.new {
168
168
  include MemoryRecord
169
- memory_record options do
169
+ memory_record **options do
170
170
  [
171
171
  {x: 1, y: 1, z: 1},
172
172
  ]
@@ -298,22 +298,38 @@ RSpec.describe MemoryRecord do
298
298
  end
299
299
  end
300
300
 
301
- context 'active_model_serializers' do
302
- require "active_model_serializers"
303
-
304
- class ColorInfo
305
- include MemoryRecord
306
- memory_record [
307
- { key: :blue, },
308
- ]
309
- end
310
-
311
- class ColorInfoSerializer < ActiveModel::Serializer
312
- attributes :key
313
- end
314
-
301
+ # broken with version 0.10.10
302
+ # context 'active_model_serializers' do
303
+ # require "active_model_serializers"
304
+ #
305
+ # class ColorInfo
306
+ # include MemoryRecord
307
+ # memory_record [
308
+ # { key: :blue, },
309
+ # ]
310
+ # end
311
+ #
312
+ # class ColorInfoSerializer < ActiveModel::Serializer
313
+ # attributes :key
314
+ # end
315
+ #
316
+ # it do
317
+ # assert { ActiveModelSerializers::SerializableResource.new(ColorInfo.first).as_json == {:key => :blue} }
318
+ # end
319
+ # end
320
+
321
+ describe "valid_key" do
315
322
  it do
316
- assert { ActiveModelSerializers::SerializableResource.new(ColorInfo.first).as_json == {:key => :blue} }
323
+ model = class_new [
324
+ { key: :blue },
325
+ ]
326
+ assert { model.valid_key(:blue) == :blue }
327
+ assert { model.valid_key(:unknown) == nil }
328
+ assert { model.valid_key(:unknown, :blue) == :blue }
329
+ assert { model.valid_key(:unknown) { :blue } == :blue }
330
+ assert { model.valid_key(:unknown) { :blue } == :blue }
331
+ assert_raises(KeyError) { model.valid_key(:unknown) { :xxx } }
332
+ assert_raises(KeyError) { model.valid_key(:unknown, :xxx) }
317
333
  end
318
334
  end
319
335
  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.15
4
+ version: 0.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - akicho8
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-25 00:00:00.000000000 Z
11
+ date: 2022-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -135,6 +135,7 @@ files:
135
135
  - examples/0340_comparable.rb
136
136
  - examples/0350_power_assert.rb
137
137
  - examples/0360_sub_class.rb
138
+ - examples/0370_valid_key.rb
138
139
  - lib/memory_record.rb
139
140
  - lib/memory_record/memory_record.rb
140
141
  - lib/memory_record/memory_record/serialization.rb
@@ -146,7 +147,7 @@ homepage: ''
146
147
  licenses:
147
148
  - MIT
148
149
  metadata: {}
149
- post_install_message:
150
+ post_install_message:
150
151
  rdoc_options: []
151
152
  require_paths:
152
153
  - lib
@@ -161,9 +162,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
162
  - !ruby/object:Gem::Version
162
163
  version: '0'
163
164
  requirements: []
164
- rubyforge_project:
165
- rubygems_version: 2.7.6
166
- signing_key:
165
+ rubygems_version: 3.3.4
166
+ signing_key:
167
167
  specification_version: 4
168
168
  summary: A simple library that handles a few records easily
169
169
  test_files: