panko_serializer 0.6.0 → 0.7.0

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: c49d12e1be46851f1fdb226b37d1c09673a586d053edb8b084f6345bf74b1637
4
- data.tar.gz: e9895a4a74b16c83904ee77961c70259cb84a1d739b46d7203f7bdf2a06d3392
3
+ metadata.gz: c4f169a6a48bba339628ae84995ec2ee3bf66ef059e941be74a89dbe6ec4e983
4
+ data.tar.gz: dff287d42fe253ef3647f0555e47be7fea55ecc2cf1025cb6b0e55a734ca7806
5
5
  SHA512:
6
- metadata.gz: 063cf79e3a6580a71a8b4e6f098cdb1514e7997cd22130a5189c4db7030b39ebada570f721cbfc36e4024141b30da7a40ed6c6d364c39465101f25e5a91baf61
7
- data.tar.gz: 73987f5d252b7c2a4f0d901c941901f748536b7e16e017aede986899d1a1471246f7541b6c0aa35a1958b3c18e41bfdcb1795dfa7752296729991d5b0d1455ea
6
+ metadata.gz: 6e7507ee84bf00be48b9a5e35c9f06aacf083216242925889e2f1f0dfb24d87ba1a3ddbdd77bbf580f3000c5671873344dd32e98eea77f84bf175b4cf7c746be
7
+ data.tar.gz: 0cb0515ccd98c7f260f81d7e1ecb1f684d70d03609b849fd2141d4a4630946622cbc34a6ae1666f6ca5a226066f959280b08bde5ad81f6c02e7d59cf9da6f466
data/.travis.yml CHANGED
@@ -26,8 +26,4 @@ env:
26
26
  matrix:
27
27
  - "RAILS_VERSION=4.2.0"
28
28
  - "RAILS_VERSION=5.2.0"
29
- - "RAILS_VERSION=6.0.0.rc1"
30
-
31
- matrix:
32
- allow_failures:
33
- - "RAILS_VERSION=6.0.0.rc1"
29
+ - "RAILS_VERSION=6.0.0"
data/benchmarks/app.rb CHANGED
@@ -3,6 +3,7 @@ require "bundler/setup"
3
3
 
4
4
 
5
5
  require "active_model"
6
+ require "active_record"
6
7
  require "active_support"
7
8
  require "active_support/json"
8
9
 
data/docs/associations.md CHANGED
@@ -5,7 +5,7 @@ A serializer can define it's own associations - both `has_many` and `has_one` to
5
5
  For example:
6
6
 
7
7
  ```ruby
8
- class PostSerializer < Panko::Serailizer
8
+ class PostSerializer < Panko::Serializer
9
9
  attributes :title, :body
10
10
 
11
11
  has_one :author, serializer: AuthorSerializer
@@ -13,13 +13,27 @@ class PostSerializer < Panko::Serailizer
13
13
  end
14
14
  ```
15
15
 
16
+ # Associations with aliases
17
+
18
+ An association key name can be aliased with the `name` option.
19
+
20
+ For example:
21
+ the `actual_author` property will be converted to `alias_author`.
22
+ ```ruby
23
+ class PostSerializer < Panko::Serializer
24
+ attributes :title, :body
25
+
26
+ has_one :actual_author, serializer: AuthorSerializer, name: :alias_author
27
+ has_many :comments, each_serializer: CommentSerializer
28
+ end
29
+ ```
16
30
  ### Inference
17
31
 
18
32
  Panko can find the type of the serializer by looking at the realtionship name, so instead specifying
19
33
  the serializer at the above example, we can -
20
34
 
21
35
  ```ruby
22
- class PostSerializer < Panko::Serailizer
36
+ class PostSerializer < Panko::Serializer
23
37
  attributes :title, :body
24
38
 
25
39
  has_one :author
@@ -2,6 +2,7 @@
2
2
 
3
3
  static bool types_initialized = false;
4
4
  static VALUE ar_base_type = Qundef;
5
+ static VALUE hash_type = Qundef;
5
6
 
6
7
  VALUE init_types(VALUE v) {
7
8
  if (types_initialized == true) {
@@ -14,6 +15,8 @@ VALUE init_types(VALUE v) {
14
15
  rb_const_get_at(rb_cObject, rb_intern("ActiveRecord"));
15
16
  ar_base_type = rb_const_get_at(ar_type, rb_intern("Base"));
16
17
 
18
+ hash_type = rb_const_get_at(rb_cObject, rb_intern("Hash"));
19
+
17
20
  return Qundef;
18
21
  }
19
22
 
@@ -28,6 +31,12 @@ AttributesWriter create_attributes_writer(VALUE object) {
28
31
  .object_type = ActiveRecord,
29
32
  .write_attributes = active_record_attributes_writer};
30
33
  }
34
+
35
+ if (rb_obj_is_kind_of(object, hash_type) == Qtrue) {
36
+ return (AttributesWriter){.object_type = Hash,
37
+ .write_attributes = hash_attributes_writer};
38
+ }
39
+
31
40
  return (AttributesWriter){.object_type = Plain,
32
41
  .write_attributes = plain_attributes_writer};
33
42
 
@@ -5,8 +5,9 @@
5
5
  #include "active_record.h"
6
6
  #include "common.h"
7
7
  #include "plain.h"
8
+ #include "hash.h"
8
9
 
9
- enum ObjectType { Unknown = 0, ActiveRecord = 1, Plain = 2 };
10
+ enum ObjectType { Unknown = 0, ActiveRecord = 1, Plain = 2, Hash = 3 };
10
11
 
11
12
  typedef struct _AttributesWriter {
12
13
  enum ObjectType object_type;
@@ -0,0 +1,14 @@
1
+ #include "hash.h"
2
+
3
+ void hash_attributes_writer(VALUE obj, VALUE attributes,
4
+ EachAttributeFunc func, VALUE writer) {
5
+ long i;
6
+ for (i = 0; i < RARRAY_LEN(attributes); i++) {
7
+ volatile VALUE raw_attribute = RARRAY_AREF(attributes, i);
8
+ Attribute attribute = attribute_read(raw_attribute);
9
+
10
+ volatile VALUE value = rb_hash_aref(obj, attribute->name_str);
11
+
12
+ func(writer, attr_name_for_serialization(attribute), value);
13
+ }
14
+ }
@@ -0,0 +1,9 @@
1
+ #pragma once
2
+
3
+ #include "common.h"
4
+ #include "ruby.h"
5
+
6
+ void hash_attributes_writer(VALUE obj,
7
+ VALUE attributes,
8
+ EachAttributeFunc func,
9
+ VALUE writer);
@@ -102,6 +102,7 @@ module Panko
102
102
 
103
103
  @serialization_context = SerializationContext.create(options)
104
104
  @descriptor = Panko::SerializationDescriptor.build(self.class, options, @serialization_context)
105
+ @used = false
105
106
  end
106
107
 
107
108
  def context
@@ -126,7 +127,9 @@ module Panko
126
127
  private
127
128
 
128
129
  def serialize_with_writer(object, writer)
130
+ raise ArgumentError.new("Panko::Serializer instances are single-use") if @used
129
131
  Panko.serialize_object(object, writer, @descriptor)
132
+ @used = true
130
133
  writer
131
134
  end
132
135
  end
data/lib/panko/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Panko
4
- VERSION = "0.6.0"
4
+ VERSION = "0.7.0"
5
5
  end
@@ -30,5 +30,5 @@ Gem::Specification.new do |spec|
30
30
 
31
31
  spec.extensions << "ext/panko_serializer/extconf.rb"
32
32
 
33
- spec.add_dependency "oj", "~> 3.7.0"
33
+ spec.add_dependency "oj", "~> 3.9.0"
34
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: panko_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yosi Attias
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-01 00:00:00.000000000 Z
11
+ date: 2019-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 3.7.0
19
+ version: 3.9.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 3.7.0
26
+ version: 3.9.0
27
27
  description:
28
28
  email:
29
29
  - yosy101@gmail.com
@@ -73,6 +73,8 @@ files:
73
73
  - ext/panko_serializer/attributes_writer/attributes_writer.h
74
74
  - ext/panko_serializer/attributes_writer/common.c
75
75
  - ext/panko_serializer/attributes_writer/common.h
76
+ - ext/panko_serializer/attributes_writer/hash.c
77
+ - ext/panko_serializer/attributes_writer/hash.h
76
78
  - ext/panko_serializer/attributes_writer/plain.c
77
79
  - ext/panko_serializer/attributes_writer/plain.h
78
80
  - ext/panko_serializer/attributes_writer/type_cast/time_conversion.c
@@ -123,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
125
  - !ruby/object:Gem::Version
124
126
  version: '0'
125
127
  requirements: []
126
- rubygems_version: 3.0.3
128
+ rubygems_version: 3.0.6
127
129
  signing_key:
128
130
  specification_version: 4
129
131
  summary: High Performance JSON Serialization for ActiveRecord & Ruby Objects