memory_record 0.0.17 → 0.0.18

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: bd17d93951ecd00a25da9401976fd6001479b0a259064154d5871459b9d28f86
4
- data.tar.gz: b14110ba80d0173912f0cfc75ea7f1ff8c193f07b5ffb87e7886bf23f91482e9
3
+ metadata.gz: 398cef71aad0b7039708dc1384634c230d52686183cfdc97f4b89fc9eac151c1
4
+ data.tar.gz: 246e934baa307893bf03ca5bf9784f1dc33fb8171f489956c736820ab4665cd6
5
5
  SHA512:
6
- metadata.gz: 1500621e070dcce0990e6a51b72aece8cdd85fb127f27bf50f0622f770db76d3beabac8ec851e28f0f04bb1e147fd9a52c59d96f706a8b236a20ddd34356310c
7
- data.tar.gz: c5560f63361c6770e9f3e0d79e61fa547930112a2cf61250ba8dd846d1b212d7666f4fa7122fcd6a2e9cd7ea125eefd46f5921a5ad1cac035a5dac6e3bab5dab
6
+ metadata.gz: 8ccc73b6b361000a2225f2f5afe0c1e311379ac176da62678fc1b08e6897dfb6c981d3baa830b52deee9b47917a4b41d2ea471d8ab8c2c2ba3522cd20697a112
7
+ data.tar.gz: 97f19afba6b7455bf82779794338d97a457028802ca093cd1b0aa766a1a62c8e1596ae9aa536834946bd0c47c7c63d5ffe04da9bd99cb866017d983eb0f05881
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.5
data/README.md CHANGED
@@ -165,6 +165,26 @@ Palette.fetch_if(:tomato) # => #<Palette:... @attributes={...}>
165
165
  Palette.fetch_if(:xxx) # => <KeyError: ...>
166
166
  ```
167
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
+
168
188
  ### How to refer to other keys
169
189
 
170
190
  ```ruby
@@ -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
@@ -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
@@ -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.17'
2
+ VERSION = '0.0.18'
3
3
  end
@@ -317,4 +317,19 @@ RSpec.describe MemoryRecord do
317
317
  # assert { ActiveModelSerializers::SerializableResource.new(ColorInfo.first).as_json == {:key => :blue} }
318
318
  # end
319
319
  # end
320
+
321
+ describe "valid_key" do
322
+ it do
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) }
333
+ end
334
+ end
320
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.17
4
+ version: 0.0.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - akicho8
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-30 00:00:00.000000000 Z
11
+ date: 2020-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -103,6 +103,7 @@ extra_rdoc_files: []
103
103
  files:
104
104
  - ".gitignore"
105
105
  - ".rspec"
106
+ - ".ruby-version"
106
107
  - ".travis.yml"
107
108
  - Gemfile
108
109
  - LICENSE.txt
@@ -135,6 +136,7 @@ files:
135
136
  - examples/0340_comparable.rb
136
137
  - examples/0350_power_assert.rb
137
138
  - examples/0360_sub_class.rb
139
+ - examples/0370_valid_key.rb
138
140
  - lib/memory_record.rb
139
141
  - lib/memory_record/memory_record.rb
140
142
  - lib/memory_record/memory_record/serialization.rb