memory_record 0.0.12 → 0.0.13

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: 8a31161518612170878b45f58d9952e75403826b6ed1a3345e3a116b81b8b24c
4
- data.tar.gz: d0c23e8933f7576c00608b5cc1f5fe233b3f609db3371d22b5ac25e08d198d02
3
+ metadata.gz: a4e8c3d1cda033561e441032be32332be1327c9876a4a62d0d64cac44b024f99
4
+ data.tar.gz: 2b9fed19a0944263ac9d0895677018537557c5f4d2d5bba9b4536f02c97f517c
5
5
  SHA512:
6
- metadata.gz: 830f15f5f99178dc80e2f142d10aa4dd7f23431aca3ede47bb3591624d1e98e190485500742eb2622487f929cecd56f5507252b7b4941727e83315bc00ff4cad
7
- data.tar.gz: bd185cef177836f31268c760225e7df2935f5f42921420168819f4007543d8108046b0d4f7bd76d3e5fb41d61ed83ff726d6d3bf3973f06e6c382278129e6d29
6
+ metadata.gz: 8412d6ec55aaea46004a97a34d111e66c0a5fc5901ad975f2714842087f5a5168935920f752f426930fa1967816950c742a4a4f14c9e03951d9fdf248e372565
7
+ data.tar.gz: 3d994cce8445425741eb6245b65dfac26d7dae8253062ea8b9a5694f32baab72be36d4b1b167c29cc8fe26c68b662ce4a1d96ab345aee008b145a767e429175e
data/README.md CHANGED
@@ -171,7 +171,7 @@ Foo.first.y rescue $! # => 1
171
171
  Foo.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<Foo:0x007fcc861ff108>>
172
172
  ```
173
173
 
174
- *** How to decide **code** yourself?
174
+ ### How to decide **code** yourself?
175
175
 
176
176
  ```ruby
177
177
  class Foo
@@ -187,3 +187,33 @@ Foo.collect(&:code) # => [1, 2]
187
187
 
188
188
  It is not recommended to specify it explicitly.
189
189
  It is useful only when refactoring legacy code with compatibility in mind.
190
+
191
+ ### Convert to JSON
192
+
193
+ Similar to ActiveModel's serialization, there is an `only` `except` `methods` `include` method.
194
+
195
+ ```ruby
196
+ class ColorInfo
197
+ include MemoryRecord
198
+ memory_record [
199
+ { key: :blue, rgb: [ 0, 0, 255], },
200
+ { key: :red, rgb: [255, 0, 0], },
201
+ ]
202
+
203
+ def hex
204
+ "#" + rgb.collect { |e| "%02X" % e }.join
205
+ end
206
+
207
+ def children
208
+ [
209
+ {foo: 1, bar: 3},
210
+ {foo: 2, bar: 4},
211
+ ]
212
+ end
213
+ end
214
+
215
+ ColorInfo.first.as_json(only: :key) # => {:key => :blue}
216
+ ColorInfo.first.as_json(except: [:rgb, :code]) # => {:key => :blue}
217
+ ColorInfo.first.as_json(only: [], methods: :hex) # => {:hex => "#0000FF"}
218
+ ColorInfo.first.as_json(only: [], include: {children: {only: :foo}} ).should == {:children => [{"foo" => 1}, {"foo" => 2}]}
219
+ ```
@@ -0,0 +1,42 @@
1
+ $LOAD_PATH.unshift '../lib'
2
+ require 'memory_record'
3
+
4
+ if true
5
+ require "active_model"
6
+
7
+ class Person
8
+ include ActiveModel::Model
9
+ include ActiveModel::Serializers::JSON
10
+
11
+ attr_accessor :a
12
+
13
+ def attributes
14
+ {'a' => a}
15
+ end
16
+ end
17
+ end
18
+
19
+ class ColorInfo
20
+ include MemoryRecord
21
+ memory_record [
22
+ { key: :blue, rgb: [ 0, 0, 255], a: 1},
23
+ { key: :red, rgb: [255, 0, 0], a: 2},
24
+ ]
25
+
26
+ def hex
27
+ "#" + rgb.collect { |e| "%02X" % e }.join
28
+ end
29
+
30
+ def children
31
+ [
32
+ {a: 1, b: 2},
33
+ Person.new(a: 1),
34
+ ColorInfo[:blue],
35
+ ]
36
+ end
37
+ end
38
+
39
+ ColorInfo.first.as_json(only: :key) # => {:key=>:blue}
40
+ ColorInfo.first.as_json(except: [:rgb, :code, :a]) # => {:key=>:blue}
41
+ ColorInfo.first.as_json(only: [], methods: :hex) # => {:hex=>"#0000FF"}
42
+ ColorInfo.first.as_json(only: [], include: {children: {only: :a}} ) # => {:children=>[{"a"=>1}, {"a"=>1}, {:a=>1}]}
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/hash/except"
4
+ require "active_support/core_ext/hash/slice"
5
+
6
+ module MemoryRecord
7
+ # Reference: ActiveModel::Serializers::JSON
8
+ # https://github.com/rails/rails/blob/0605f45ab323331b06dde3ed16838f56f141ca3f/activemodel/lib/active_model/serialization.rb
9
+ # The original code and the implementation are slightly different
10
+ module Serialization
11
+ def serializable_hash(options = nil)
12
+ options ||= {}
13
+
14
+ keys = attributes.keys
15
+ if only = options[:only]
16
+ keys &= Array(only)
17
+ elsif except = options[:except]
18
+ keys -= Array(except)
19
+ end
20
+ keys += Array(options[:methods])
21
+
22
+ hash = {}
23
+ keys.each { |e| hash[e] = send(e) }
24
+ Array(options[:methods]).each { |e| hash[e] = send(e) }
25
+
26
+ serializable_add_includes(options) do |association, records, opts|
27
+ hash[association] = -> {
28
+ if records.respond_to?(:to_ary)
29
+ records.to_ary.map { |a| a.as_json(opts) }
30
+ else
31
+ records.as_json(opts)
32
+ end
33
+ }.call
34
+ end
35
+
36
+ hash
37
+ end
38
+
39
+ def as_json(**options)
40
+ serializable_hash(options)
41
+ end
42
+
43
+ private
44
+
45
+ # Add associations specified via the <tt>:include</tt> option.
46
+ #
47
+ # Expects a block that takes as arguments:
48
+ # +association+ - name of the association
49
+ # +records+ - the association record(s) to be serialized
50
+ # +opts+ - options for the association records
51
+ def serializable_add_includes(options = {}) #:nodoc:
52
+ return unless includes = options[:include]
53
+
54
+ unless includes.is_a?(Hash)
55
+ includes = Hash[Array(includes).flat_map { |n| n.is_a?(Hash) ? n.to_a : [[n, {}]] }]
56
+ end
57
+
58
+ includes.each do |association, opts|
59
+ if records = send(association)
60
+ yield association, records, opts
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -4,6 +4,10 @@ require 'active_support/core_ext/module/concerning'
4
4
  require 'active_support/core_ext/class/attribute'
5
5
  require 'active_support/core_ext/module/delegation'
6
6
 
7
+ # json serialization
8
+ require 'active_support/core_ext/object/json' # as_json
9
+ require 'memory_record/memory_record/serialization'
10
+
7
11
  module MemoryRecord
8
12
  extend ActiveSupport::Concern
9
13
 
@@ -33,6 +37,7 @@ module MemoryRecord
33
37
 
34
38
  extend Enumerable
35
39
  include ::MemoryRecord::SingletonMethods
40
+ include ::MemoryRecord::Serialization
36
41
 
37
42
  class_attribute :memory_record_options
38
43
  self.memory_record_options = {
@@ -1,3 +1,3 @@
1
1
  module MemoryRecord
2
- VERSION = '0.0.12'
2
+ VERSION = '0.0.13'
3
3
  end
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.add_development_dependency 'rake'
21
21
  spec.add_development_dependency 'rspec'
22
22
  spec.add_development_dependency 'test-unit'
23
+ spec.add_development_dependency 'activemodel'
23
24
 
24
25
  spec.add_dependency 'activesupport'
25
26
  end
@@ -235,4 +235,46 @@ RSpec.describe MemoryRecord do
235
235
  h[a] = true
236
236
  h[b].should == true
237
237
  end
238
+
239
+ context 'as_json' do
240
+ require "active_model"
241
+
242
+ class Person
243
+ include ActiveModel::Model
244
+ include ActiveModel::Serializers::JSON
245
+
246
+ attr_accessor :a
247
+
248
+ def attributes
249
+ {'a' => a}
250
+ end
251
+ end
252
+
253
+ class ColorInfo
254
+ include MemoryRecord
255
+ memory_record [
256
+ { key: :blue, rgb: [ 0, 0, 255], a: 1},
257
+ { key: :red, rgb: [255, 0, 0], a: 2},
258
+ ]
259
+
260
+ def hex
261
+ "#" + rgb.collect { |e| "%02X" % e }.join
262
+ end
263
+
264
+ def children
265
+ [
266
+ {a: 1, b: 2},
267
+ Person.new(a: 1),
268
+ ColorInfo[:blue],
269
+ ]
270
+ end
271
+ end
272
+
273
+ it "as_json(options)" do
274
+ ColorInfo.first.as_json(only: :key).should == {:key => :blue}
275
+ ColorInfo.first.as_json(except: [:rgb, :code, :a]).should == {:key => :blue}
276
+ ColorInfo.first.as_json(only: [], methods: :hex).should == {:hex => "#0000FF"}
277
+ ColorInfo.first.as_json(only: [], include: {children: {only: :a}} ).should == {:children => [{"a" => 1}, {"a" => 1}, {:a => 1}]}
278
+ end
279
+ end
238
280
  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.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - akicho8
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-17 00:00:00.000000000 Z
11
+ date: 2018-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: activesupport
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -102,8 +116,10 @@ files:
102
116
  - examples/0290_eql_behavior.rb
103
117
  - examples/0300_use_as_hash_key.rb
104
118
  - examples/0310_same_if_dup.rb
119
+ - examples/0320_as_json.rb
105
120
  - lib/memory_record.rb
106
121
  - lib/memory_record/memory_record.rb
122
+ - lib/memory_record/memory_record/serialization.rb
107
123
  - lib/memory_record/version.rb
108
124
  - memory_record.gemspec
109
125
  - spec/memory_record_spec.rb