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 +4 -4
- data/.travis.yml +1 -5
- data/benchmarks/app.rb +1 -0
- data/docs/associations.md +16 -2
- data/ext/panko_serializer/attributes_writer/attributes_writer.c +9 -0
- data/ext/panko_serializer/attributes_writer/attributes_writer.h +2 -1
- data/ext/panko_serializer/attributes_writer/hash.c +14 -0
- data/ext/panko_serializer/attributes_writer/hash.h +9 -0
- data/lib/panko/serializer.rb +3 -0
- data/lib/panko/version.rb +1 -1
- data/panko_serializer.gemspec +1 -1
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c4f169a6a48bba339628ae84995ec2ee3bf66ef059e941be74a89dbe6ec4e983
|
4
|
+
data.tar.gz: dff287d42fe253ef3647f0555e47be7fea55ecc2cf1025cb6b0e55a734ca7806
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e7507ee84bf00be48b9a5e35c9f06aacf083216242925889e2f1f0dfb24d87ba1a3ddbdd77bbf580f3000c5671873344dd32e98eea77f84bf175b4cf7c746be
|
7
|
+
data.tar.gz: 0cb0515ccd98c7f260f81d7e1ecb1f684d70d03609b849fd2141d4a4630946622cbc34a6ae1666f6ca5a226066f959280b08bde5ad81f6c02e7d59cf9da6f466
|
data/.travis.yml
CHANGED
data/benchmarks/app.rb
CHANGED
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::
|
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::
|
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
|
+
}
|
data/lib/panko/serializer.rb
CHANGED
@@ -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
data/panko_serializer.gemspec
CHANGED
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.
|
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-
|
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.
|
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.
|
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.
|
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
|