memory_record 0.0.14 → 0.0.15

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: 64b86bd76f59f5da1e0e4111208b5fdb868df86865239e998c9d4de5796f43e6
4
- data.tar.gz: 405a3da00872ef9c104ac6208c225eb808b7329443be89cc1ee3328b3aec0a5e
3
+ metadata.gz: 5782c36c4656fbfea525ca83150171f5e7d72ef6e12c4a3bc5bb0c9cbebf999d
4
+ data.tar.gz: ae386371adb57bc10e97a3ec86c732eeb343fa53912ae7fc75fe6ddb41d15ae0
5
5
  SHA512:
6
- metadata.gz: 0f243e8cf16808efda1c6fab438c1631982d9d825d1dd760f5bc3c1741ee30d1ca01ec1e79a5259e6f2757cef8153d6823e8360ee4df09fa2644745c4ac6bb69
7
- data.tar.gz: 51f7d2216b5aef88194fd7d9c76362babab8800193c85b7bb511d6aa6d4cbda004e7992891d77113101e86e04e02f868b22831d41e28c17e43ca817d5bb89912
6
+ metadata.gz: 2c93d3d610e960a3a6b8da1041e8e6814de11ea93f5b341a18c39d8f80cf502b7bb05f1b07f1fa32fe575270809df31de441928bb0ec123cde3d0c02c7335a04
7
+ data.tar.gz: fb32e99ced614b0dc378662072f769e59d2bf28578537d48f734a078d793fc2b861f99bd96dff1496ed62ce80ddf10717ef4a861beff64a794ce4b00fd739866
data/README.md CHANGED
@@ -70,9 +70,12 @@ Palette.collect { |e| ... }
70
70
  ### How do I submit a form to select in Rails?
71
71
 
72
72
  ```ruby
73
- form.collection_select(:selection_code, Palette, :code, :name)
73
+ form.collection_select(:pallet_key, Palette, :key, :name) // object[pallet_key]=tomato
74
+ form.collection_select(:pallet_key, Palette, :code, :name) // object[pallet_key]=1
74
75
  ```
75
76
 
77
+ Either way can see it with `Palette[pallet_key]`
78
+
76
79
  ### Is the reference in subscripts slow?
77
80
 
78
81
  Since it has a hash internally using the key value as a key, it can be acquired with O (1).
@@ -221,4 +224,13 @@ color_info.as_json(only: [], include: {children: {only: :x}} ) # => {:children =
221
224
 
222
225
  ColorInfo.as_json(only: :key) # => [{:key=>:blue}, {:key=>:red}]
223
226
  ColorInfo.to_json(only: :key) # => "[{\"key\":\"blue\"},{\"key\":\"red\"}]"
227
+
228
+ #### Case of ActiveModelSerializers
229
+
230
+ ```ruby
231
+ class ColorInfoSerializer < ActiveModel::Serializer
232
+ attributes :key
233
+ end
234
+
235
+ ActiveModelSerializers::SerializableResource.new(ColorInfo.first).as_json # => { :key => :blue }
224
236
  ```
@@ -36,10 +36,11 @@ class ColorInfo
36
36
  end
37
37
  end
38
38
 
39
+ ColorInfo.first.key # => :blue
40
+
39
41
  ColorInfo.first.as_json(only: :key) # => {:key=>:blue}
40
42
  ColorInfo.first.as_json(except: [:rgb, :code, :a]) # => {:key=>:blue}
41
43
  ColorInfo.first.as_json(only: [], methods: :hex) # => {:hex=>"#0000FF"}
42
44
  ColorInfo.first.as_json(only: [], include: {children: {only: :a}} ) # => {:children=>[{"a"=>1}, {"a"=>1}, {:a=>1}]}
43
-
44
45
  ColorInfo.as_json(only: :key) # => [{:key=>:blue}, {:key=>:red}]
45
46
  ColorInfo.to_json(only: :key) # => "[{\"key\":\"blue\"},{\"key\":\"red\"}]"
@@ -0,0 +1,22 @@
1
+ require "bundler/inline"
2
+
3
+ gemfile do
4
+ gem "memory_record", path: ".."
5
+ gem "active_model_serializers"
6
+ end
7
+
8
+ class ColorInfo
9
+ include MemoryRecord
10
+ memory_record [
11
+ { key: :blue, },
12
+ { key: :red, },
13
+ ]
14
+ end
15
+
16
+ class ColorInfoSerializer < ActiveModel::Serializer
17
+ attributes :key, :name
18
+ end
19
+
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)
22
+ # >> {:key=>:blue, :name=>"blue"}
File without changes
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.unshift '../lib'
2
+ require 'memory_record'
3
+
4
+ require "rspec/autorun"
5
+
6
+ Foo = {}
7
+
8
+ RSpec.configure do |config|
9
+ config.expect_with :test_unit
10
+ end
11
+
12
+ describe do
13
+ it do
14
+ assert { Foo["あ"] == Foo["い"] }
15
+ end
16
+ end
17
+ # >> .
18
+ # >>
19
+ # >> Finished in 0.01009 seconds (files took 0.30828 seconds to load)
20
+ # >> 1 example, 0 failures
21
+ # >>
@@ -0,0 +1,24 @@
1
+ $LOAD_PATH.unshift '../lib'
2
+ require 'memory_record'
3
+
4
+ class A
5
+ include MemoryRecord
6
+ memory_record [
7
+ { key: :a, x: 1, y: 2 },
8
+ ]
9
+
10
+ def z
11
+ x + y
12
+ end
13
+ end
14
+
15
+ class B < A
16
+ memory_record_reset superclass.collect(&:attributes)
17
+
18
+ def z
19
+ super * 2
20
+ end
21
+ end
22
+
23
+ B.values # => [#<B:0x00007feea40395b8 @attributes={:key=>:a, :x=>1, :y=>2, :code=>0}>]
24
+ B.first.z # => 6
@@ -21,7 +21,7 @@ module MemoryRecord
21
21
 
22
22
  hash = {}
23
23
  keys.each { |e| hash[e] = send(e) }
24
- Array(options[:methods]).each { |e| hash[e] = send(e) }
24
+ Array(options[:methods]).each { |e| hash[e] = read_attribute_for_serialization(e) }
25
25
 
26
26
  serializable_add_includes(options) do |association, records, opts|
27
27
  hash[association] = -> {
@@ -42,6 +42,8 @@ module MemoryRecord
42
42
 
43
43
  private
44
44
 
45
+ alias :read_attribute_for_serialization :send # for ActiveModelSerializers
46
+
45
47
  # Add associations specified via the <tt>:include</tt> option.
46
48
  #
47
49
  # Expects a block that takes as arguments:
@@ -1,3 +1,3 @@
1
1
  module MemoryRecord
2
- VERSION = '0.0.14'
2
+ VERSION = '0.0.15'
3
3
  end
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency 'rspec'
22
22
  spec.add_development_dependency 'test-unit'
23
23
  spec.add_development_dependency 'activemodel'
24
+ spec.add_development_dependency 'active_model_serializers'
24
25
 
25
26
  spec.add_dependency 'activesupport'
26
27
  end
@@ -297,4 +297,23 @@ RSpec.describe MemoryRecord do
297
297
  assert { ColorInfo.as_json(only: :key) == [{:key => :blue}, {:key => :red}] }
298
298
  end
299
299
  end
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
+
315
+ it do
316
+ assert { ActiveModelSerializers::SerializableResource.new(ColorInfo.first).as_json == {:key => :blue} }
317
+ end
318
+ end
300
319
  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.14
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - akicho8
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-03 00:00:00.000000000 Z
11
+ date: 2018-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: active_model_serializers
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: activesupport
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -117,7 +131,10 @@ files:
117
131
  - examples/0300_use_as_hash_key.rb
118
132
  - examples/0310_same_if_dup.rb
119
133
  - examples/0320_as_json.rb
120
- - examples/0330_comparable.rb
134
+ - examples/0330_active_model_serializers.rb
135
+ - examples/0340_comparable.rb
136
+ - examples/0350_power_assert.rb
137
+ - examples/0360_sub_class.rb
121
138
  - lib/memory_record.rb
122
139
  - lib/memory_record/memory_record.rb
123
140
  - lib/memory_record/memory_record/serialization.rb