From 232378ea5e02231971ee90c30e6f67e8b55bee68 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Tue, 10 Aug 2021 18:48:11 +0200 Subject: [PATCH] Add a few utility methods --- src/invidious/user/preferences.cr | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/invidious/user/preferences.cr b/src/invidious/user/preferences.cr index 82e381ba..b346ebca 100644 --- a/src/invidious/user/preferences.cr +++ b/src/invidious/user/preferences.cr @@ -66,6 +66,43 @@ class Preferences property volume : Int32 = 100 + def initialize + end + + # Duplicate (make a perfect copy of) the current object + def dup + other = Preferences.new + + {% for ivar in @type.instance_vars %} + other.{{ ivar.id }} = {{ ivar.id }} + {% end %} + + return other + end + + # :nodoc: + def text_dump(include_defaults = false) + {% for ivar in @type.instance_vars %} + name = {{ ivar.id.stringify }} + value = {{ ivar.id }} + default = {{ ivar.default_value }} + + puts "#{name} = #{value}" if (include_defaults || value != default) + {% end %} + end + + # :nodoc: + def text_dump(other : Preferences) + # Macro that dumps, as text, all the contents of that struct + {% for ivar in @type.instance_vars %} + name = {{ ivar.id.stringify }} + val1 = self.{{ ivar.id }} + val2 = other.{{ ivar.id }} + + puts "#{name} = #{val1}" if val1 != val2 + {% end %} + end + module BoolToString def self.to_json(value : String, json : JSON::Builder) json.string value