From c2b816011c10e81f3ea6a39e9982c3b6986a070e Mon Sep 17 00:00:00 2001 From: samiullah Date: Thu, 15 Sep 2022 10:47:00 +0430 Subject: [PATCH 1/5] Initialize attributes --- app/serializer.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/serializer.rb b/app/serializer.rb index 258be50..03c741c 100644 --- a/app/serializer.rb +++ b/app/serializer.rb @@ -1,2 +1,7 @@ class Serializer + attribute :id + attribute :title + attribute :body + attribute :date + end From 2a08b1e2430a25fe39cdfb4ff0492fd56cd7ac39 Mon Sep 17 00:00:00 2001 From: samiullah Date: Thu, 15 Sep 2022 10:48:03 +0430 Subject: [PATCH 2/5] Create serializer method --- app/serializer.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/app/serializer.rb b/app/serializer.rb index 03c741c..b8bc285 100644 --- a/app/serializer.rb +++ b/app/serializer.rb @@ -3,5 +3,43 @@ class Serializer attribute :title attribute :body attribute :date + + def initialize(option = {}) + self.id = option[:id] + + begin + self.body = option[:body] + rescue + # Ignored + end + + begin + self.title = option[:title] + rescue + # Ignored + end + + begin + self.date = option[:date].strftime("%d-%m-%Y") + rescue + # Ignored + end +end + +def serialize + if self.class.name == 'PostSerializer' + { + id: @id, + title: @title, + date: @date, + } + else + { + id: @id, + body: @body + } + end + + end end From f0af7f90d53cf59079c3727674078c92ae58a813 Mon Sep 17 00:00:00 2001 From: samiullah Date: Thu, 15 Sep 2022 10:50:26 +0430 Subject: [PATCH 3/5] Change attribute to attr_writer --- app/comment_serializer.rb | 4 ++-- app/post_serializer.rb | 6 +++--- app/serializer.rb | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/comment_serializer.rb b/app/comment_serializer.rb index d27c604..32f14e9 100644 --- a/app/comment_serializer.rb +++ b/app/comment_serializer.rb @@ -1,6 +1,6 @@ require_relative "serializer" class CommentSerializer < Serializer - attribute :id - attribute :body + attr_writer :id + attr_writer :body end diff --git a/app/post_serializer.rb b/app/post_serializer.rb index a616290..640e5df 100644 --- a/app/post_serializer.rb +++ b/app/post_serializer.rb @@ -1,9 +1,9 @@ require_relative "serializer" class PostSerializer < Serializer - attribute :id - attribute :title - attribute :date do + attr_writer :id + attr_writer :title + attr_writer :date do object.date.strftime("%d-%m-%Y") end end diff --git a/app/serializer.rb b/app/serializer.rb index b8bc285..a84b319 100644 --- a/app/serializer.rb +++ b/app/serializer.rb @@ -1,8 +1,8 @@ class Serializer - attribute :id - attribute :title - attribute :body - attribute :date + attr_writer :id + attr_writer :title + attr_writer :body + attr_writer :date def initialize(option = {}) self.id = option[:id] From 8dd1200d3124206e50cebdf34e4705e1ce1e59e5 Mon Sep 17 00:00:00 2001 From: samiullah Date: Mon, 19 Sep 2022 13:57:51 +0430 Subject: [PATCH 4/5] Uncommit comment_serializer and post_serializer --- app/comment_serializer.rb | 4 ++-- app/post_serializer.rb | 6 +++--- app/serializer.rb | 43 +-------------------------------------- 3 files changed, 6 insertions(+), 47 deletions(-) diff --git a/app/comment_serializer.rb b/app/comment_serializer.rb index 32f14e9..353061e 100644 --- a/app/comment_serializer.rb +++ b/app/comment_serializer.rb @@ -1,6 +1,6 @@ require_relative "serializer" class CommentSerializer < Serializer - attr_writer :id - attr_writer :body + attribute :id + attribute :body end diff --git a/app/post_serializer.rb b/app/post_serializer.rb index 640e5df..a616290 100644 --- a/app/post_serializer.rb +++ b/app/post_serializer.rb @@ -1,9 +1,9 @@ require_relative "serializer" class PostSerializer < Serializer - attr_writer :id - attr_writer :title - attr_writer :date do + attribute :id + attribute :title + attribute :date do object.date.strftime("%d-%m-%Y") end end diff --git a/app/serializer.rb b/app/serializer.rb index a84b319..f89da42 100644 --- a/app/serializer.rb +++ b/app/serializer.rb @@ -1,45 +1,4 @@ class Serializer - attr_writer :id - attr_writer :title - attr_writer :body - attr_writer :date - - def initialize(option = {}) - self.id = option[:id] - - begin - self.body = option[:body] - rescue - # Ignored - end - - begin - self.title = option[:title] - rescue - # Ignored - end - - begin - self.date = option[:date].strftime("%d-%m-%Y") - rescue - # Ignored - end -end - -def serialize - if self.class.name == 'PostSerializer' - { - id: @id, - title: @title, - date: @date, - } - else - { - id: @id, - body: @body - } - end - - end + end From 18e767892129cfb738203cf043a2be7ed821ae2a Mon Sep 17 00:00:00 2001 From: samiullah Date: Mon, 19 Sep 2022 14:33:11 +0430 Subject: [PATCH 5/5] Fix serializer class --- app/serializer.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/serializer.rb b/app/serializer.rb index f89da42..7549506 100644 --- a/app/serializer.rb +++ b/app/serializer.rb @@ -1,4 +1,17 @@ + class Serializer - -end + attr_accessor :values + def initialize(attr) + self.values = attr + end + def self.attribute(*args) + @attr = args unless args.empty? + end + def serialize + values.date = values.date.strftime("%d-%m-%Y") unless values.instance_of?(Comment) + hash_values = values.to_h + hash_values.delete(:title) unless values.instance_of?(Post) + hash_values + end +end \ No newline at end of file