From 30af8fb051e71e90705ee8c1a4c590c7d81a4077 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 16 Apr 2019 19:20:17 +1000 Subject: [PATCH] refactor: extract config settings repository --- lib/pact_broker/config/repository.rb | 60 ++++++++++++++++++++++++++++ lib/pact_broker/config/save.rb | 60 +++++----------------------- 2 files changed, 69 insertions(+), 51 deletions(-) create mode 100644 lib/pact_broker/config/repository.rb diff --git a/lib/pact_broker/config/repository.rb b/lib/pact_broker/config/repository.rb new file mode 100644 index 000000000..1503e9441 --- /dev/null +++ b/lib/pact_broker/config/repository.rb @@ -0,0 +1,60 @@ +require 'pact_broker/config/setting' + +module PactBroker + module Config + class Repository + + def create_or_update_setting setting_name, setting_value + setting = Setting.find(name: setting_name.to_s) || Setting.new(name: setting_name.to_s) + setting.type = get_db_type(setting_value) + setting.value = get_db_value(setting_value) + setting.save + end + + def class_supported? setting_value + !!get_db_type(setting_value) + end + + def get_db_type setting_value + case setting_value + when true, false + 'boolean' + when String, nil + 'string' + when SpaceDelimitedStringList + 'space_delimited_string_list' + when Array, Hash + 'json' + when Integer + 'integer' + when Float + 'float' + else + nil + end + end + + def get_db_value setting_value + case setting_value + when String, Integer, Float, NilClass + setting_value + when TrueClass + "1" + when FalseClass + "0" + when SpaceDelimitedStringList + setting_value.to_s + when Array, Hash + setting_value.to_json + else + nil + end + end + + def get_value setting_name + configuration.send(setting_name) + end + + end + end +end \ No newline at end of file diff --git a/lib/pact_broker/config/save.rb b/lib/pact_broker/config/save.rb index 37a3dd861..df92d6b5f 100644 --- a/lib/pact_broker/config/save.rb +++ b/lib/pact_broker/config/save.rb @@ -1,6 +1,7 @@ require 'pact_broker/configuration' require 'pact_broker/logging' require 'pact_broker/config/setting' +require 'pact_broker/config/repository' module PactBroker module Config @@ -15,69 +16,26 @@ def self.call configuration, setting_names def initialize configuration, setting_names @configuration = configuration @setting_names = setting_names + @repository = Config::Repository.new end def call setting_names.each do | setting_name | - if class_supported?(setting_name) - create_or_update_setting(setting_name) + setting_value = get_value(setting_name) + if repository.class_supported?(setting_value) + create_or_update_setting(setting_name, setting_value) else - logger.warn "Could not save configuration setting \"#{setting_name}\" to database as the class #{get_value(setting_name).class} is not supported." + logger.warn "Could not save configuration setting \"#{setting_name}\" to database as the class #{setting_value.class} is not supported." end end end private - attr_reader :configuration, :setting_names + attr_reader :configuration, :setting_names, :repository - def create_or_update_setting setting_name - setting = Setting.find(name: setting_name.to_s) || Setting.new(name: setting_name.to_s) - setting.type = get_db_type(setting_name) - setting.value = get_db_value(setting_name) - setting.save - end - - def class_supported? setting_name - !!get_db_type(setting_name) - end - - def get_db_type setting_name - val = get_value(setting_name) - case val - when true, false - 'boolean' - when String, nil - 'string' - when SpaceDelimitedStringList - 'space_delimited_string_list' - when Array, Hash - 'json' - when Integer - 'integer' - when Float - 'float' - else - nil - end - end - - def get_db_value setting_name - val = get_value(setting_name) - case val - when String, Integer, Float, NilClass - val - when TrueClass - "1" - when FalseClass - "0" - when SpaceDelimitedStringList - val.to_s - when Array, Hash - val.to_json - else - nil - end + def create_or_update_setting setting_name, setting_value + repository.create_or_update_setting(setting_name, setting_value) end def get_value setting_name