panko_serializer 0.1.2 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d166380b6a032ffaf299ff6aff8dea9dabcb7354
4
- data.tar.gz: 3fd7c899d24982bada152680444403b767ff2522
3
+ metadata.gz: ba52569ddbbd4f0de8ea0cd554f7df5fdf78340e
4
+ data.tar.gz: ae21f470b06c1576077e2f56211fb00b36c91118
5
5
  SHA512:
6
- metadata.gz: 7055dea074e595a3d6e01c1acbdd3f18b6d89419c1750d51918e2ba4f90264acb5d985b6a42fe472fa28185b19b938cb8f906a0b69900d4cd40d5b57d71c6326
7
- data.tar.gz: 9df158a3287ef8f036cfbf7417003b5e2aba2f0546cbeedfdb398be08db211525f6735cea09db5861c3e467f5a6948e0f1d39824b9aca881690f7ed6cb0e2f73
6
+ metadata.gz: 9acdd06de3e6268bbacdcdc97e897357534253728cbac9f8311682ccd5c468abccce8d89226cb7d1c7b811c67ff4a103951b5030128fdf0865f0dfd62823e43f
7
+ data.tar.gz: a4aa910864f1aa16ac0a57f4004a8b9698f06233e2ad83c8193d2b7fb22cbd26a17871248e34c2e84fe78e888456392c375ef8c73e4cec0ba00faf8d1885a00c
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # Panko
2
2
 
3
- [![Build Status](https://travis-ci.org/yosiat/panko.svg?branch=master)](https://travis-ci.org/yosiat/panko)
3
+ [![Build Status](https://travis-ci.org/yosiat/panko_serializer.svg?branch=master)](https://travis-ci.org/yosiat/panko_serializer)
4
4
 
5
5
 
6
6
  ## License
7
7
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
8
8
 
9
+
@@ -7,7 +7,7 @@ require "pg"
7
7
 
8
8
 
9
9
  require_relative "../benchmarking_support"
10
- require_relative "../../lib/panko/panko"
10
+ require_relative "../../lib/panko/panko_serializer"
11
11
 
12
12
  def assert(type_name, from, to)
13
13
  raise "#{type_name} - #{from.class} is not equals to #{to.class}" unless from.to_json == to.to_json
@@ -3,6 +3,10 @@
3
3
  static ID attributes_id = 0;
4
4
  static ID types_id = 0;
5
5
  static ID values_id = 0;
6
+ static ID delegate_hash_id = 0;
7
+
8
+ static ID value_before_type_cast_id = 0;
9
+ static ID type_id = 0;
6
10
 
7
11
  VALUE read_attributes(VALUE obj) {
8
12
  return rb_ivar_get(obj, attributes_id);
@@ -30,24 +34,42 @@ void panko_read_types_and_value(VALUE attributes_hash,
30
34
  }
31
35
 
32
36
  VALUE panko_each_attribute(VALUE obj,
33
- SerializationDescriptor descriptor,
34
37
  VALUE attributes,
35
38
  EachAttributeFunc func,
36
39
  VALUE context) {
37
40
  VALUE attributes_hash = panko_read_lazy_attributes_hash(obj);
38
- if(attributes_hash == Qnil) {
41
+ if (attributes_hash == Qnil) {
39
42
  return Qnil;
40
43
  }
41
44
 
45
+ VALUE delegate_hash = rb_ivar_get(attributes_hash, delegate_hash_id);
46
+ bool tryToReadFromDelegateHash = RHASH_SIZE(delegate_hash) > 0;
47
+
42
48
  VALUE types, values;
43
49
  panko_read_types_and_value(attributes_hash, &types, &values);
44
50
 
45
51
  int i;
46
52
  for (i = 0; i < RARRAY_LEN(attributes); i++) {
47
- VALUE member = rb_sym2str(RARRAY_AREF(attributes, i));
53
+ VALUE member = RARRAY_AREF(attributes, i);
54
+
55
+ VALUE value = Qundef;
56
+ VALUE type_metadata = Qnil;
57
+
58
+ // First try to read from delegate hash,
59
+ // If the object was create in memory `User.new(name: "Yosi")`
60
+ // it won't exist in types/values
61
+ if (tryToReadFromDelegateHash) {
62
+ VALUE attribute_metadata = rb_hash_aref(delegate_hash, member);
63
+ if (attribute_metadata != Qnil) {
64
+ value = rb_ivar_get(attribute_metadata, value_before_type_cast_id);
65
+ type_metadata = rb_ivar_get(attribute_metadata, type_id);
66
+ }
67
+ }
48
68
 
49
- VALUE value = rb_hash_aref(values, member);
50
- VALUE type_metadata = rb_hash_aref(types, member);
69
+ if (value == Qundef) {
70
+ value = rb_hash_aref(values, member);
71
+ type_metadata = rb_hash_aref(types, member);
72
+ }
51
73
 
52
74
  func(obj, member, value, type_metadata, context);
53
75
  }
@@ -57,6 +79,9 @@ VALUE panko_each_attribute(VALUE obj,
57
79
 
58
80
  void panko_init_attributes_iterator(VALUE mPanko) {
59
81
  attributes_id = rb_intern("@attributes");
82
+ delegate_hash_id = rb_intern("@delegate_hash");
60
83
  values_id = rb_intern("@values");
61
84
  types_id = rb_intern("@types");
85
+ type_id = rb_intern("@type");
86
+ value_before_type_cast_id = rb_intern("@value_before_type_cast");
62
87
  }
@@ -1,4 +1,5 @@
1
1
  #include <ruby.h>
2
+ #include <stdbool.h>
2
3
 
3
4
  #include "serialization_descriptor.h"
4
5
 
@@ -9,7 +10,6 @@ typedef void (*EachAttributeFunc)(VALUE object,
9
10
  VALUE context);
10
11
 
11
12
  extern VALUE panko_each_attribute(VALUE object,
12
- SerializationDescriptor descriptor,
13
13
  VALUE attributes,
14
14
  EachAttributeFunc func,
15
15
  VALUE context);
@@ -53,8 +53,7 @@ void serialize_fields(VALUE subject,
53
53
  VALUE str_writer,
54
54
  SerializationDescriptor descriptor,
55
55
  VALUE context) {
56
- panko_each_attribute(subject, descriptor, descriptor->fields,
57
- panko_attributes_iter, str_writer);
56
+ panko_each_attribute(subject, descriptor->fields, panko_attributes_iter, str_writer);
58
57
 
59
58
  serialize_method_fields(subject, str_writer, descriptor, context);
60
59
  }
@@ -39,8 +39,7 @@ VALUE iso_ar_iso_datetime_string(const char* value) {
39
39
 
40
40
  VALUE output = Qnil;
41
41
  if (r >= 0) {
42
- char buf[21];
43
- sprintf(buf, "");
42
+ char buf[21] = "";
44
43
 
45
44
  append_region_part(buf, value, region, 1);
46
45
  strncat(buf, "-", 1);
@@ -16,6 +16,7 @@ module Panko
16
16
  serializer_only_filters,
17
17
  serializer_except_filters
18
18
  )
19
+ backend.fields = backend.fields.map &:to_s
19
20
 
20
21
  backend.method_fields = apply_fields_filters(
21
22
  method_fields,
@@ -34,23 +34,18 @@ module Panko
34
34
  def initialize(options = {})
35
35
  @descriptor = Panko::CACHE.fetch(self.class, options)
36
36
 
37
- @context = options.fetch(:context, nil)
38
- @object = nil
37
+ @context = options[:context]
39
38
  end
40
39
 
41
40
  attr_reader :object, :context
42
- attr_writer :object
43
41
 
44
- def serialize(object, writer = nil)
45
- Oj.load(serialize_to_json(object, writer))
42
+ def serialize(object)
43
+ Oj.load(serialize_to_json(object))
46
44
  end
47
45
 
48
- def serialize_to_json(object, writer = nil)
49
- @object = object
50
-
51
- writer ||= Oj::StringWriter.new(mode: :rails)
46
+ def serialize_to_json(object)
47
+ writer = Oj::StringWriter.new(mode: :rails)
52
48
  Panko::serialize_subject(object, writer, @descriptor, @context)
53
-
54
49
  writer.to_s
55
50
  end
56
51
  end
data/lib/panko/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Panko
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  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.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yosi Attias
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-13 00:00:00.000000000 Z
11
+ date: 2017-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler