deserializer 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
  SHA1:
3
- metadata.gz: 8e98ac2dd5ac22cf71de5fe17aec446999b93dec
4
- data.tar.gz: 423a1b3fcc3b5a8497c9aa14de5d570c969085ef
3
+ metadata.gz: d7ead03cc6f7509656922f6a300a7416730840dc
4
+ data.tar.gz: f10fff12d9341c2c51cfcf83d4374e1cfbf82d7a
5
5
  SHA512:
6
- metadata.gz: a617f0e4c52e8397fd287f0d776db9ffbf57f094ab029747eb499a2062a8ebbbc92288fc4a01aba316d2fb86eb477af298afc4d361cf49f055641f7dd666208f
7
- data.tar.gz: b24342df65559de3e0fa3b8f7dc9d48974e3403d560e6da805d18d88873e2b9ead8aeb17c8ffe34d44490960ce7ac9a3249a58889e0f619886b233d197b904c6
6
+ metadata.gz: f922a56a0beab0caffce4555722f93c0a41c4dbcd6d41fde5bd30875346409f49e12d8d20e316f324ff026b6c5b2fd3e0112f2a73495c8dd506b43722ae6c459
7
+ data.tar.gz: 37cd5ed9ea2990d5af586cf309a19e4bbed380b3881802dd6f33abe2e890e2af28fb5270e882db1caa9b72cec6ec176f403b5d8577a41d9e28efe80ec210eb82
@@ -1,13 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- deserializer (0.5.0)
4
+ deserializer (0.6.0)
5
5
  activesupport
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (4.2.4)
10
+ activesupport (4.2.5)
11
11
  i18n (~> 0.7)
12
12
  json (~> 1.7, >= 1.7.7)
13
13
  minitest (~> 5.1)
@@ -1,6 +1,8 @@
1
1
  require "deserializer/version"
2
2
  require 'active_support'
3
3
  require 'active_support/concern'
4
+ require 'active_support/core_ext/object/blank'
5
+ require 'active_support/core_ext/class/attribute'
4
6
 
5
7
  module Deserializer
6
8
  autoload :Attribute, 'deserializer/attribute'
@@ -7,24 +7,24 @@ module Deserializer
7
7
  # deserializer interface functions
8
8
 
9
9
  def attributes( *attrs )
10
- self.__attrs ||= []
10
+ self.__attrs = __attrs.dup
11
11
  attrs.each do |attr|
12
- __attrs << Attribute::Attribute.new( Attribute::ValueAttribute, attr, {} )
12
+ attribute( attr, {} )
13
13
  end
14
14
  end
15
15
 
16
16
  def attribute( name, opts = {} )
17
- self.__attrs ||= []
18
- __attrs << Attribute::Attribute.new( Attribute::ValueAttribute, name, opts )
17
+ attribute = Attribute::Attribute.new( Attribute::ValueAttribute, name, opts )
18
+ self.__attrs = __attrs.merge attribute.key => attribute
19
19
  end
20
20
 
21
21
  def has_one( name, opts = {} )
22
22
  unless opts[:deserializer]
23
23
  raise DeserializerError, class: self, message: "has_one associations need a deserilaizer"
24
24
  end
25
-
26
- self.__attrs ||= []
27
- __attrs << Attribute::Attribute.new( Attribute::HasOneAssociation, name, opts )
25
+
26
+ attribute = Attribute::Attribute.new( Attribute::HasOneAssociation, name, opts )
27
+ self.__attrs = __attrs.merge attribute.key => attribute
28
28
  end
29
29
 
30
30
  def has_many( name, opts = {} )
@@ -32,8 +32,8 @@ module Deserializer
32
32
  raise DeserializerError, class: self, message: "has_many associations need a deserilaizer"
33
33
  end
34
34
 
35
- self.__attrs ||= []
36
- __attrs << Attribute::Attribute.new( Attribute::HasManyAssociation, name, opts )
35
+ attribute = Attribute::Attribute.new( Attribute::HasManyAssociation, name, opts )
36
+ self.__attrs = __attrs.merge attribute.key => attribute
37
37
  end
38
38
 
39
39
  def belongs_to( *args )
@@ -45,8 +45,8 @@ module Deserializer
45
45
  raise DeserializerError, class: self, message: "nested associations need a deserilaizer"
46
46
  end
47
47
 
48
- self.__attrs ||= []
49
- __attrs << Attribute::Attribute.new( Attribute::NestedAssociation, name, opts )
48
+ attribute = Attribute::Attribute.new( Attribute::NestedAssociation, name, opts )
49
+ self.__attrs = __attrs.merge attribute.key => attribute
50
50
  end
51
51
  end
52
52
  end
@@ -18,15 +18,6 @@ module Deserializer
18
18
  if target == object.object
19
19
  return value
20
20
 
21
- # has_one :thing, deserializer: ThingDeserializer
22
- # has_one :gniht, deserializer: GnihtDeserializer
23
- #
24
- # def thing
25
- # object[:gniht]
26
- # end
27
- elsif target.is_a? Hash
28
- return target.merge value
29
-
30
21
  # has_one :thing, deserializer: GnihtDeserializer
31
22
  #
32
23
  # def thing
@@ -5,7 +5,7 @@ module Deserializer
5
5
  def value( params )
6
6
  value = params[key]
7
7
 
8
- if opts[:ignore_empty] && empty?(value)
8
+ if opts[:ignore_empty] && value.blank?
9
9
  return :ignore
10
10
  end
11
11
 
@@ -19,15 +19,6 @@ module Deserializer
19
19
 
20
20
  value
21
21
  end
22
-
23
- private
24
-
25
- def empty?(value)
26
- !value ||
27
- value == "" ||
28
- value == {} ||
29
- value == []
30
- end
31
22
  end
32
23
  end
33
24
  end
@@ -4,17 +4,18 @@ module Deserializer
4
4
  ## atribute, has_one, nested, etc associations
5
5
  include Deserializer::Attributable
6
6
 
7
+ class_attribute :__attrs
8
+ self.__attrs = {}
7
9
  class << self
8
- attr_accessor :__attrs
9
-
10
+
10
11
  # deserializer usage functions
11
12
 
12
13
  def from_params( params = {} )
13
- self.new( params ).deserialize
14
+ new( params ).deserialize
14
15
  end
15
16
 
16
17
  def permitted_params
17
- self.__attrs.map(&:key)
18
+ __attrs.keys
18
19
  end
19
20
  end
20
21
 
@@ -25,7 +26,7 @@ module Deserializer
25
26
  object ||= {}
26
27
 
27
28
  # deserialize
28
- self.class.__attrs.each do |attr|
29
+ self.class.__attrs.each do |_, attr|
29
30
  object.merge!( attr.to_hash( params, self ) ) do |key, old_value, new_value|
30
31
  # in the case that 2 has_ones merge into the same key. Not sure i want to support this
31
32
  if old_value.is_a?( Hash ) && new_value.is_a?( Hash )
@@ -1,3 +1,3 @@
1
1
  module Deserializer
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -127,4 +127,11 @@ end
127
127
  class HasManyDeserializer < Deserializer::Base
128
128
  attribute :id
129
129
  has_many :attributes, deserializer: AttributeDeserializer
130
+ end
131
+
132
+ class InheritedDeserializer < HasOneWithTargetDeserializer
133
+ attributes :this,
134
+ :that
135
+
136
+ attribute :created_by
130
137
  end
@@ -174,4 +174,14 @@ class DeserializerTest < Minitest::Test
174
174
  def test_nested_handles_no_input
175
175
  assert_equal ({nested_object: {}}), NestableDeserializer.from_params( {} )
176
176
  end
177
+
178
+ def test_can_inherit_from_other_deserializer
179
+ base_permitted_params = HasOneWithTargetDeserializer.permitted_params
180
+
181
+ assert_equal [:external, :thing], base_permitted_params, "inheritance broke the base class"
182
+
183
+ inherited_permitted_params = base_permitted_params + [:this, :that, :created_by]
184
+
185
+ assert_equal inherited_permitted_params, InheritedDeserializer.permitted_params
186
+ end
177
187
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deserializer
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
  - Greg Orlov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-18 00:00:00.000000000 Z
11
+ date: 2016-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport