File tree Expand file tree Collapse file tree 2 files changed +43
-6
lines changed Expand file tree Collapse file tree 2 files changed +43
-6
lines changed Original file line number Diff line number Diff line change 1
1
module ActiveModel
2
2
module Deserializer
3
3
4
- def self . included ( base )
5
- base . wrap_parameters format : [ :json ]
4
+ def deserialize ( resource )
5
+ # Defining the Resource and Attributes
6
+ @resource = resource
7
+ @model = resource . to_s . camelize . constantize
8
+ @params = params
9
+ self
6
10
end
7
11
8
- def deserialize ( params )
9
- # TODO deserializaion based on Adapter and Serializer
10
- params
12
+ def object
13
+ # Return the existing instance of the Resource or initialize a new one
14
+ @model . find_or_initialize_by ( _params )
15
+ end
16
+
17
+ private
18
+
19
+ def _params
20
+ # Automatically permitting the attributes that should be deserialized
21
+ @serializer = get_serializer_for ( @model )
22
+ @params . require ( @resource ) . permit ( @serializer . _deserialize )
23
+ end
24
+
25
+ # (WIP)Replicated code just for testing for now
26
+ def serializers_cache
27
+ @serializers_cache ||= ThreadSafe ::Cache . new
28
+ end
29
+
30
+ def get_serializer_for ( klass )
31
+ serializers_cache . fetch_or_store ( klass ) do
32
+ serializer_class_name = "#{ klass . name } Serializer"
33
+ serializer_class = serializer_class_name . safe_constantize
34
+
35
+ if serializer_class
36
+ serializer_class
37
+ elsif klass . superclass
38
+ get_serializer_for ( klass . superclass )
39
+ end
40
+ end
11
41
end
12
42
13
43
end
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ class Serializer
10
10
11
11
class << self
12
12
attr_accessor :_attributes
13
+ attr_accessor :_deserialize
13
14
attr_accessor :_attributes_keys
14
15
attr_accessor :_associations
15
16
attr_accessor :_urls
@@ -24,13 +25,19 @@ class << self
24
25
25
26
def self . inherited ( base )
26
27
base . _attributes = self . _attributes . try ( :dup ) || [ ]
27
- base . _attributes_keys = self . _attributes_keys . try ( :dup ) || { }
28
28
base . _associations = self . _associations . try ( :dup ) || { }
29
+ base . _attributes_keys = self . _attributes_keys . try ( :dup ) || { }
30
+ base . _deserialize = self . _attributes . try ( :dup ) || [ ]
29
31
base . _urls = [ ]
30
32
serializer_file = File . open ( caller . first [ /^[^:]+/ ] )
31
33
base . _cache_digest = Digest ::MD5 . hexdigest ( serializer_file . read )
32
34
end
33
35
36
+ def self . deserialize ( *attrs )
37
+ @_deserialize . concat attrs
38
+ @_deserialize . uniq!
39
+ end
40
+
34
41
def self . attributes ( *attrs )
35
42
attrs = attrs . first if attrs . first . class == Array
36
43
@_attributes . concat attrs
You can’t perform that action at this time.
0 commit comments