serdee 0.1.0 → 0.2.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
  SHA1:
3
- metadata.gz: d32e12c6af56fcee2b79e0d1c3390410459d5c23
4
- data.tar.gz: 0e8003cbd25ad282db4749c35119326c3cb23f0e
3
+ metadata.gz: f831af6244168e3b41b10a9d8125de04f8aae255
4
+ data.tar.gz: 4c8880ec3892de9eb91b7ba40e8483e40bf76df5
5
5
  SHA512:
6
- metadata.gz: 97c96e37529521be72bd5ffe824a6f0cc11f07e64e6c838ae7719c32989cfe2b12be649d11b52a4f8144eee88f33dfe8620051d7b8ccff352a5b128d8598f7fe
7
- data.tar.gz: '0818da033b2801fcf157d2be3f753c5bb61b87385131694c4ae3ba734086d54063bf9a7ac56793a5c68bb9beb7c95809e16255607a8a5ac56f2810db18ad02fb'
6
+ metadata.gz: fc1e84c9985bde67bce5382f7c05ea7f8b216328c8b11207d66ff79028a54e9e44389f392579ef5234fa5d5b6315b96e3b38a9bc1096df51f3c034034cb2fc94
7
+ data.tar.gz: 06fd5fe404699eea0e0d4497f8bf3c3c856099bbbde09d32722ebf45ced15649226cb85da79bed5c3d63bdee6b3f3ea3b9abfab84f5bb01ca0057e94a956c7f3
@@ -3,7 +3,8 @@ require_relative "./unified_message"
3
3
 
4
4
  class RequestMessage
5
5
  include Serdee::Attributes
6
- set_key_transform :camel_lower
6
+ serialize_key { |key| key.camelize(:lower) }
7
+ deserialize_key { |key| key.underscore }
7
8
 
8
9
  nested :request, UnifiedMessage
9
10
  end
@@ -3,7 +3,8 @@ require_relative "./unified_message"
3
3
  require_relative "./response_code"
4
4
  class ResponseMessage
5
5
  include Serdee::Attributes
6
- set_key_transform :camel_lower
6
+ serialize_key { |key| key.camelize(:lower) }
7
+ deserialize_key { |key| key.underscore }
7
8
 
8
9
  attr_accessor :request_id
9
10
 
@@ -4,7 +4,8 @@ require_relative "./response_code"
4
4
 
5
5
  class UnifiedMessage
6
6
  include Serdee::Attributes
7
- set_key_transform :camel_lower
7
+ serialize_key { |key| key.camelize(:lower) }
8
+ deserialize_key { |key| key.underscore }
8
9
 
9
10
  PROCESSING_CODES = {
10
11
  balance_inquiry: "315400",
@@ -32,8 +32,8 @@ module Serdee
32
32
 
33
33
  def as_json
34
34
  self.class.insert_to(self, {}).deep_transform_keys! do |key|
35
- if self.class.key_transform
36
- self.class.key_transform.call(key.to_s)
35
+ if self.class.serialize_key
36
+ self.class.serialize_key.call(key.to_s)
37
37
  else
38
38
  key.to_s
39
39
  end
@@ -53,20 +53,23 @@ module Serdee
53
53
  @serializers ||= {}
54
54
  end
55
55
 
56
- def set_key_transform(transform_name = nil, &block)
57
- if transform_name
58
- mapping = {
59
- camel: :camelize.to_proc,
60
- camel_lower: ->(key) { key.camelize(:lower) },
61
- dash: :dasherize.to_proc,
62
- underscore: :underscore.to_proc
63
- }
56
+ def serialize_key(method = nil, &block)
57
+ if method
58
+ @serialize_key = ->(key) { self.send(method, key) }
59
+ elsif block
60
+ @serialize_key = block
61
+ else
62
+ @serialize_key || Serdee.serialize_key
63
+ end
64
+ end
64
65
 
65
- @key_transform = mapping[transform_name.to_sym]
66
+ def deserialize_key(method = nil, &block)
67
+ if method
68
+ @deserialize_key = ->(key) { self.send(method, key) }
66
69
  elsif block
67
- @key_transform = block
70
+ @deserialize_key = block
68
71
  else
69
- raise ArgumentError, "must supply at least transform_name or block"
72
+ @deserialize_key || Serdee.deserialize_key
70
73
  end
71
74
  end
72
75
 
@@ -106,7 +109,11 @@ module Serdee
106
109
  def of_json(json)
107
110
  allocate.tap do |obj|
108
111
  data = json.deep_transform_keys do |key|
109
- key.underscore.to_sym
112
+ if deserialize_key
113
+ deserialize_key.call(key).to_sym
114
+ else
115
+ key.to_sym
116
+ end
110
117
  end
111
118
  extract_to(data, obj)
112
119
  obj.deserialized_from = json
@@ -1,3 +1,3 @@
1
1
  module Serdee
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serdee
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Ong