memory_record 0.0.21 → 0.0.22
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 +4 -4
- data/examples/0100_basic.rb +2 -2
- data/examples/0110_for_legacy_code.rb +5 -5
- data/examples/0120_key_join_underscore_if_array.rb +2 -2
- data/examples/0130_behavior_like_hash.rb +2 -2
- data/examples/0140_practice.rb +5 -5
- data/examples/0150_reset.rb +2 -2
- data/examples/0160_key_is_anything_ok.rb +4 -4
- data/examples/0170_tips_on_handling_boolean_type.rb +4 -4
- data/examples/0180_freeze_makes_it_impossible_to_memorize.rb +4 -4
- data/examples/0190_super_usable.rb +3 -3
- data/examples/0200_with_active_model_validation.rb +5 -5
- data/examples/0210_key_duplicate_check.rb +4 -4
- data/examples/0220_bad_design.rb +4 -4
- data/examples/0230_with_use_activerecord_enum.rb +6 -6
- data/examples/0240_attr_reader_option.rb +8 -8
- data/examples/0250_lookup_super_call.rb +4 -4
- data/examples/0260_also_refer_to_other_keys.rb +2 -2
- data/examples/0270_instance_freeze.rb +3 -3
- data/examples/0280_sortable.rb +2 -2
- data/examples/0290_eql_behavior.rb +2 -2
- data/examples/0300_use_as_hash_key.rb +2 -2
- data/examples/0310_same_if_dup.rb +2 -2
- data/examples/0320_as_json.rb +9 -9
- data/examples/0330_active_model_serializers.rb +32 -3
- data/examples/0340_comparable.rb +2 -2
- data/examples/0350_power_assert.rb +3 -3
- data/examples/0360_sub_class.rb +2 -2
- data/examples/0370_valid_key.rb +3 -3
- data/examples/0380_equal_nil.rb +19 -0
- data/lib/memory_record/memory_record.rb +11 -8
- data/lib/memory_record/version.rb +1 -1
- data/lib/memory_record.rb +2 -2
- data/spec/memory_record_spec.rb +9 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e70444943873ad37d57fabe1a479539228332b86e9a05dc252062a5266a90f46
|
4
|
+
data.tar.gz: 88abbda5031d8e10aa28f1ef19f728a6edf598ca184008bc5433daf665f21133
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b414a978f86f4cce6d95f90287153d0492ea06a9d8ab1c4dd64e4081292a0d0bbc44ba001b22636922bd557f14a86083d4ca81532af6c3312d9f331d14c83a8
|
7
|
+
data.tar.gz: 6aadfe0df1c36d63ae3cab98a6c1da22dce52ba9c996c7bc6136f33783197fb0d94a57ab325366c6d9cc0e2009857e20c498a01ea764920e775131116f640bab
|
data/examples/0100_basic.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
$LOAD_PATH.unshift
|
2
|
-
require
|
1
|
+
$LOAD_PATH.unshift "../lib"
|
2
|
+
require "memory_record"
|
3
3
|
|
4
4
|
# It is better to stop allocating code yourself
|
5
5
|
|
6
6
|
class Foo
|
7
7
|
include MemoryRecord
|
8
8
|
memory_record [
|
9
|
-
{code: 1, key: :a, name:
|
10
|
-
{code: 2, key: :b, name:
|
11
|
-
{code: 3, key: :c, name:
|
9
|
+
{code: 1, key: :a, name: "A"},
|
10
|
+
{code: 2, key: :b, name: "B"},
|
11
|
+
{code: 3, key: :c, name: "C"},
|
12
12
|
]
|
13
13
|
end
|
14
14
|
|
data/examples/0140_practice.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
$LOAD_PATH.unshift
|
2
|
-
require
|
1
|
+
$LOAD_PATH.unshift "../lib"
|
2
|
+
require "memory_record"
|
3
3
|
|
4
4
|
class Direction
|
5
5
|
include MemoryRecord
|
6
6
|
memory_record [
|
7
|
-
{key: :left, name:
|
8
|
-
{key: :right, name:
|
7
|
+
{key: :left, name: "←", vector: [-1, 0]},
|
8
|
+
{key: :right, name: "→", vector: [ 1, 0]},
|
9
9
|
]
|
10
10
|
|
11
11
|
def long_name
|
@@ -24,4 +24,4 @@ Direction[:right].long_name # => "→ direction"
|
|
24
24
|
Direction[1].key # => :right
|
25
25
|
|
26
26
|
Direction[:up] # => nil
|
27
|
-
Direction.fetch(:up) rescue $! # => #<KeyError:
|
27
|
+
Direction.fetch(:up) rescue $! # => #<KeyError:"Direction.fetch(:up) does not match anything\nkeys: [:left, :right]\ncodes: [0, 1]">
|
data/examples/0150_reset.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
$LOAD_PATH.unshift
|
2
|
-
require
|
1
|
+
$LOAD_PATH.unshift "../lib"
|
2
|
+
require "memory_record"
|
3
3
|
|
4
4
|
class Foo
|
5
5
|
include MemoryRecord
|
6
6
|
memory_record [
|
7
|
-
{key:
|
7
|
+
{key: "↑", name: "UP"},
|
8
8
|
]
|
9
9
|
end
|
10
10
|
|
11
|
-
Foo[
|
11
|
+
Foo["↑"].name # => "UP"
|
@@ -1,11 +1,11 @@
|
|
1
|
-
$LOAD_PATH.unshift
|
2
|
-
require
|
1
|
+
$LOAD_PATH.unshift "../lib"
|
2
|
+
require "memory_record"
|
3
3
|
|
4
4
|
class Foo
|
5
5
|
include MemoryRecord
|
6
6
|
memory_record [
|
7
|
-
{key:
|
8
|
-
{key:
|
7
|
+
{key: "true", name: "ON"},
|
8
|
+
{key: "false", name: "OFF"},
|
9
9
|
]
|
10
10
|
end
|
11
11
|
|
@@ -1,11 +1,11 @@
|
|
1
1
|
# If you freeze it will not be able to make these memos
|
2
2
|
|
3
|
-
$LOAD_PATH.unshift
|
4
|
-
require
|
3
|
+
$LOAD_PATH.unshift "../lib"
|
4
|
+
require "memory_record"
|
5
5
|
|
6
6
|
class C
|
7
7
|
def self.x
|
8
|
-
@x ||=
|
8
|
+
@x ||= "OK"
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
@@ -16,7 +16,7 @@ class C2
|
|
16
16
|
]
|
17
17
|
|
18
18
|
def x
|
19
|
-
@x ||=
|
19
|
+
@x ||= "OK"
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -1,5 +1,5 @@
|
|
1
|
-
$LOAD_PATH.unshift
|
2
|
-
require
|
1
|
+
$LOAD_PATH.unshift "../lib"
|
2
|
+
require "memory_record"
|
3
3
|
|
4
4
|
class Foo
|
5
5
|
include MemoryRecord
|
@@ -22,4 +22,4 @@ end
|
|
22
22
|
|
23
23
|
Foo.first.a # => 20
|
24
24
|
Foo.first.name # => "(_key0)"
|
25
|
-
Foo.first.to_h # => {:
|
25
|
+
Foo.first.to_h # => {a: 10, code: 0, key: :_key0, x: 1}
|
@@ -1,15 +1,15 @@
|
|
1
|
-
$LOAD_PATH.unshift
|
2
|
-
require
|
1
|
+
$LOAD_PATH.unshift "../lib"
|
2
|
+
require "memory_record"
|
3
3
|
|
4
4
|
class Foo
|
5
5
|
include MemoryRecord
|
6
6
|
memory_record [
|
7
|
-
{name:
|
8
|
-
{name:
|
7
|
+
{name: "alice"},
|
8
|
+
{name: "bob"},
|
9
9
|
]
|
10
10
|
end
|
11
11
|
|
12
|
-
require
|
12
|
+
require "active_model"
|
13
13
|
|
14
14
|
class Foo
|
15
15
|
include ActiveModel::Validations
|
@@ -1,14 +1,14 @@
|
|
1
|
-
$LOAD_PATH.unshift
|
2
|
-
require
|
1
|
+
$LOAD_PATH.unshift "../lib"
|
2
|
+
require "memory_record"
|
3
3
|
|
4
4
|
# An error occurs if key is duplicated
|
5
5
|
|
6
6
|
class Foo
|
7
7
|
include MemoryRecord
|
8
|
-
memory_record [{key: :a}, {key: :a},] rescue $! # => #<ArgumentError:
|
8
|
+
memory_record [{key: :a}, {key: :a},] rescue $! # => #<ArgumentError:"Foo#key :a is duplicate\nExisting: {key: :a, code: 0}\nConflict: {key: :a, code: 1}">
|
9
9
|
end
|
10
10
|
|
11
11
|
class Bar
|
12
12
|
include MemoryRecord
|
13
|
-
memory_record [{code: 0}, {code: 0},] rescue $! # => #<ArgumentError:
|
13
|
+
memory_record [{code: 0}, {code: 0},] rescue $! # => #<ArgumentError:"Bar#code 0 is duplicate\nExisting: {code: 0, key: :_key0}\nConflict: {code: 0, key: :_key1}">
|
14
14
|
end
|
data/examples/0220_bad_design.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
$LOAD_PATH.unshift
|
2
|
-
require
|
1
|
+
$LOAD_PATH.unshift "../lib"
|
2
|
+
require "memory_record"
|
3
3
|
|
4
4
|
# I can do it like this, but I can not recommend it at all
|
5
5
|
# Do not increase the magic number
|
@@ -13,5 +13,5 @@ class Foo
|
|
13
13
|
]
|
14
14
|
end
|
15
15
|
|
16
|
-
Foo["01"] # => #<Foo:
|
17
|
-
Foo["02"] # => #<Foo:
|
16
|
+
Foo["01"] # => #<Foo:0x000000011e3c36f0 @attributes={key: :"01", name: "left", code: 0}>
|
17
|
+
Foo["02"] # => #<Foo:0x000000011e3c3628 @attributes={key: :"02", name: "right", code: 1}>
|
@@ -1,11 +1,11 @@
|
|
1
|
-
$LOAD_PATH.unshift
|
2
|
-
require
|
1
|
+
$LOAD_PATH.unshift "../lib"
|
2
|
+
require "memory_record"
|
3
3
|
|
4
|
-
require
|
4
|
+
require "active_record"
|
5
5
|
|
6
6
|
ActiveRecord::VERSION::STRING # => "5.1.4"
|
7
7
|
ActiveRecord::Migration.verbose = false
|
8
|
-
ActiveRecord::Base.establish_connection(adapter:
|
8
|
+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
9
9
|
|
10
10
|
ActiveRecord::Schema.define do
|
11
11
|
create_table :users do |t|
|
@@ -16,8 +16,8 @@ end
|
|
16
16
|
class GenderInfo
|
17
17
|
include MemoryRecord
|
18
18
|
memory_record [
|
19
|
-
{key: :male, name:
|
20
|
-
{key: :female, name:
|
19
|
+
{key: :male, name: "♂"},
|
20
|
+
{key: :female, name: "♀"},
|
21
21
|
]
|
22
22
|
end
|
23
23
|
|
@@ -1,5 +1,5 @@
|
|
1
|
-
$LOAD_PATH.unshift
|
2
|
-
require
|
1
|
+
$LOAD_PATH.unshift "../lib"
|
2
|
+
require "memory_record"
|
3
3
|
|
4
4
|
class C1
|
5
5
|
include MemoryRecord
|
@@ -10,9 +10,9 @@ class C1
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
C1.first.x rescue $! # => #<NoMethodError: undefined method
|
14
|
-
C1.first.y rescue $! # => #<NoMethodError: undefined method
|
15
|
-
C1.first.z rescue $! # => #<NoMethodError: undefined method
|
13
|
+
C1.first.x rescue $! # => #<NoMethodError: undefined method 'x' for an instance of C1>
|
14
|
+
C1.first.y rescue $! # => #<NoMethodError: undefined method 'y' for an instance of C1>
|
15
|
+
C1.first.z rescue $! # => #<NoMethodError: undefined method 'z' for an instance of C1>
|
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
|
26
|
+
C2.first.x rescue $! # => #<NoMethodError: undefined method 'x' for an instance of C2>
|
27
27
|
C2.first.y rescue $! # => 1
|
28
|
-
C2.first.z rescue $! # => #<NoMethodError: undefined method
|
28
|
+
C2.first.z rescue $! # => #<NoMethodError: undefined method 'z' for an instance of C2>
|
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
|
40
|
+
C3.first.y rescue $! # => #<NoMethodError: undefined method 'y' for an instance of C3>
|
41
41
|
C3.first.z rescue $! # => 1
|
@@ -1,5 +1,5 @@
|
|
1
|
-
$LOAD_PATH.unshift
|
2
|
-
require
|
1
|
+
$LOAD_PATH.unshift "../lib"
|
2
|
+
require "memory_record"
|
3
3
|
|
4
4
|
class C
|
5
5
|
include MemoryRecord
|
@@ -15,5 +15,5 @@ class C
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
C.lookup(:alice) # => #<C:
|
19
|
-
C[:alice] # => #<C:
|
18
|
+
C.lookup(:alice) # => #<C:0x00000001203138c0 @attributes={key: :alice, code: 0}>
|
19
|
+
C[:alice] # => #<C:0x00000001203138c0 @attributes={key: :alice, code: 0}>
|
@@ -1,5 +1,5 @@
|
|
1
|
-
$LOAD_PATH.unshift
|
2
|
-
require
|
1
|
+
$LOAD_PATH.unshift "../lib"
|
2
|
+
require "memory_record"
|
3
3
|
|
4
4
|
class Foo
|
5
5
|
include MemoryRecord
|
@@ -17,6 +17,6 @@ class Foo
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
v = Foo.first # => #<Foo:
|
20
|
+
v = Foo.first # => #<Foo:0x00000001210f3710 @attributes={key: :a, code: 0}, @var=1>
|
21
21
|
v.frozen? # => true
|
22
22
|
v.a # => 1
|
data/examples/0280_sortable.rb
CHANGED
data/examples/0320_as_json.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
$LOAD_PATH.unshift
|
2
|
-
require
|
1
|
+
$LOAD_PATH.unshift "../lib"
|
2
|
+
require "memory_record"
|
3
3
|
|
4
4
|
if true
|
5
5
|
require "active_model"
|
@@ -11,7 +11,7 @@ if true
|
|
11
11
|
attr_accessor :a
|
12
12
|
|
13
13
|
def attributes
|
14
|
-
{
|
14
|
+
{"a" => a}
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
@@ -38,10 +38,10 @@ end
|
|
38
38
|
|
39
39
|
ColorInfo.first.key # => :blue
|
40
40
|
|
41
|
-
ColorInfo.first.as_json # => {:
|
42
|
-
ColorInfo.first.as_json(only: :key) # => {:
|
43
|
-
ColorInfo.first.as_json(except: [:rgb, :code, :a]) # => {:
|
44
|
-
ColorInfo.first.as_json(only: [], methods: :hex) # => {:
|
45
|
-
ColorInfo.first.as_json(only: [], include: {children: {only: :a}} ) # => {:
|
46
|
-
ColorInfo.as_json(only: :key) # => [{:
|
41
|
+
ColorInfo.first.as_json # => {key: :blue, rgb: [0, 0, 255], a: 1, code: 0}
|
42
|
+
ColorInfo.first.as_json(only: :key) # => {key: :blue}
|
43
|
+
ColorInfo.first.as_json(except: [:rgb, :code, :a]) # => {key: :blue}
|
44
|
+
ColorInfo.first.as_json(only: [], methods: :hex) # => {hex: "#0000FF"}
|
45
|
+
ColorInfo.first.as_json(only: [], include: {children: {only: :a}} ) # => {children: [{"a" => 1}, {"a" => 1}, {a: 1}]}
|
46
|
+
ColorInfo.as_json(only: :key) # => [{key: :blue}, {key: :red}]
|
47
47
|
ColorInfo.to_json(only: :key) # => "[{\"key\":\"blue\"},{\"key\":\"red\"}]"
|
@@ -17,6 +17,35 @@ class ColorInfoSerializer < ActiveModel::Serializer
|
|
17
17
|
attributes :key, :name
|
18
18
|
end
|
19
19
|
|
20
|
-
pp ActiveModelSerializers::SerializableResource.new(ColorInfo.first).as_json # =>
|
21
|
-
#
|
22
|
-
#
|
20
|
+
pp ActiveModelSerializers::SerializableResource.new(ColorInfo.first).as_json # =>
|
21
|
+
# ~> /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/resolver.rb:348:in 'Bundler::Resolver#raise_not_found!': Could not find gem 'active_model_serializers (= 0.10.7)' in locally installed gems. (Bundler::GemNotFound)
|
22
|
+
# ~>
|
23
|
+
# ~> The source contains the following gems matching 'active_model_serializers':
|
24
|
+
# ~> * active_model_serializers-0.10.15
|
25
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/resolver.rb:448:in 'block in Bundler::Resolver#prepare_dependencies'
|
26
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/resolver.rb:423:in 'Hash#each'
|
27
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/resolver.rb:423:in 'Enumerable#filter_map'
|
28
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/resolver.rb:423:in 'Bundler::Resolver#prepare_dependencies'
|
29
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/resolver.rb:65:in 'Bundler::Resolver#setup_solver'
|
30
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/resolver.rb:30:in 'Bundler::Resolver#start'
|
31
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/definition.rb:746:in 'Bundler::Definition#start_resolution'
|
32
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/definition.rb:342:in 'Bundler::Definition#resolve'
|
33
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/definition.rb:653:in 'Bundler::Definition#materialize'
|
34
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/definition.rb:237:in 'Bundler::Definition#specs'
|
35
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/installer.rb:227:in 'Bundler::Installer#ensure_specs_are_compatible!'
|
36
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/installer.rb:81:in 'block in Bundler::Installer#run'
|
37
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/rubygems.rb:835:in 'block in Gem.open_file_with_flock'
|
38
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/rubygems.rb:823:in 'IO.open'
|
39
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/rubygems.rb:823:in 'Gem.open_file_with_flock'
|
40
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/rubygems.rb:809:in 'Gem.open_file_with_lock'
|
41
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/process_lock.rb:13:in 'block in Bundler::ProcessLock.lock'
|
42
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/shared_helpers.rb:105:in 'Bundler::SharedHelpers#filesystem_access'
|
43
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/process_lock.rb:12:in 'Bundler::ProcessLock.lock'
|
44
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/installer.rb:71:in 'Bundler::Installer#run'
|
45
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/installer.rb:23:in 'Bundler::Installer.install'
|
46
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/inline.rb:64:in 'block (2 levels) in Object#gemfile'
|
47
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/settings.rb:159:in 'Bundler::Settings#temporary'
|
48
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/inline.rb:63:in 'block in Object#gemfile'
|
49
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/settings.rb:159:in 'Bundler::Settings#temporary'
|
50
|
+
# ~> from /opt/rbenv/versions/3.4.2/lib/ruby/site_ruby/3.4.0/bundler/inline.rb:58:in 'Object#gemfile'
|
51
|
+
# ~> from -:3:in '<main>'
|
data/examples/0340_comparable.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
$LOAD_PATH.unshift
|
2
|
-
require
|
1
|
+
$LOAD_PATH.unshift "../lib"
|
2
|
+
require "memory_record"
|
3
3
|
|
4
4
|
require "rspec/autorun"
|
5
5
|
|
@@ -16,6 +16,6 @@ describe do
|
|
16
16
|
end
|
17
17
|
# >> .
|
18
18
|
# >>
|
19
|
-
# >> Finished in 0.
|
19
|
+
# >> Finished in 0.00996 seconds (files took 0.12816 seconds to load)
|
20
20
|
# >> 1 example, 0 failures
|
21
21
|
# >>
|
data/examples/0360_sub_class.rb
CHANGED
data/examples/0370_valid_key.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
$LOAD_PATH.unshift
|
2
|
-
require
|
1
|
+
$LOAD_PATH.unshift "../lib"
|
2
|
+
require "memory_record"
|
3
3
|
|
4
4
|
class Color
|
5
5
|
include MemoryRecord
|
@@ -12,4 +12,4 @@ Color.lookup_key(:blue) # => :blue
|
|
12
12
|
Color.lookup_key(:unknown) # => nil
|
13
13
|
Color.lookup_key(:unknown, :blue) # => :blue
|
14
14
|
Color.lookup_key(:unknown) { :blue } # => :blue
|
15
|
-
Color.lookup_key(:unknown) { :xxx } rescue $! # => #<KeyError:
|
15
|
+
Color.lookup_key(:unknown) { :xxx } rescue $! # => #<KeyError:"Color.fetch(:xxx) does not match anything\nkeys: [:blue]\ncodes: [0]">
|
@@ -0,0 +1,19 @@
|
|
1
|
+
$LOAD_PATH.unshift "../lib"
|
2
|
+
require "memory_record"
|
3
|
+
|
4
|
+
class A
|
5
|
+
include MemoryRecord
|
6
|
+
memory_record [
|
7
|
+
{ key: :x },
|
8
|
+
]
|
9
|
+
end
|
10
|
+
|
11
|
+
class B
|
12
|
+
include MemoryRecord
|
13
|
+
memory_record [
|
14
|
+
{ key: :x },
|
15
|
+
]
|
16
|
+
end
|
17
|
+
|
18
|
+
A[:x] != nil # => true
|
19
|
+
[A[:x], B[:x]].sort rescue $! # => #<ArgumentError: comparison of A with B failed>
|
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
2
|
+
require "active_support/concern"
|
3
|
+
require "active_support/core_ext/module/concerning"
|
4
|
+
require "active_support/core_ext/class/attribute"
|
5
|
+
require "active_support/core_ext/module/delegation"
|
6
6
|
|
7
7
|
# json serialization
|
8
|
-
require
|
9
|
-
require
|
8
|
+
require "active_support/core_ext/object/json" # as_json
|
9
|
+
require "memory_record/memory_record/serialization"
|
10
10
|
|
11
11
|
module MemoryRecord
|
12
12
|
extend ActiveSupport::Concern
|
@@ -77,9 +77,12 @@ module MemoryRecord
|
|
77
77
|
define_method(:name) { key.to_s }
|
78
78
|
end
|
79
79
|
|
80
|
+
# https://docs.ruby-lang.org/ja/latest/class/Comparable.html
|
80
81
|
# sort matches definition order. Comparable module methods will be available.
|
81
82
|
def <=>(other)
|
82
|
-
|
83
|
+
if self.class == other.class
|
84
|
+
code <=> other.code
|
85
|
+
end
|
83
86
|
end
|
84
87
|
|
85
88
|
# Even if object_id of objects used as hash keys are different, they match. It also speeds up by defining hash.
|
@@ -217,7 +220,7 @@ module MemoryRecord
|
|
217
220
|
def _attributes_normalize(attrs, index)
|
218
221
|
key = attrs[:key] || "_key#{index}"
|
219
222
|
if key.kind_of? Array
|
220
|
-
key = key.join(
|
223
|
+
key = key.join("_")
|
221
224
|
end
|
222
225
|
attrs.merge(code: attrs[:code] || index, key: key.to_sym)
|
223
226
|
end
|
data/lib/memory_record.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "memory_record/version"
|
2
|
+
require "memory_record/memory_record"
|
data/spec/memory_record_spec.rb
CHANGED
@@ -332,4 +332,13 @@ RSpec.describe MemoryRecord do
|
|
332
332
|
assert_raises(KeyError) { model.lookup_key(:unknown, :xxx) }
|
333
333
|
end
|
334
334
|
end
|
335
|
+
|
336
|
+
describe "euqal nil" do
|
337
|
+
it do
|
338
|
+
model = class_new [
|
339
|
+
{ key: :blue },
|
340
|
+
]
|
341
|
+
assert { model[:blue] != nil }
|
342
|
+
end
|
343
|
+
end
|
335
344
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: memory_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.22
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- akicho8
|
@@ -135,6 +135,7 @@ files:
|
|
135
135
|
- examples/0350_power_assert.rb
|
136
136
|
- examples/0360_sub_class.rb
|
137
137
|
- examples/0370_valid_key.rb
|
138
|
+
- examples/0380_equal_nil.rb
|
138
139
|
- lib/memory_record.rb
|
139
140
|
- lib/memory_record/memory_record.rb
|
140
141
|
- lib/memory_record/memory_record/serialization.rb
|