Skip to content

Commit

Permalink
refactor: extract config settings repository
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Apr 16, 2019
1 parent e15da45 commit 30af8fb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 51 deletions.
60 changes: 60 additions & 0 deletions lib/pact_broker/config/repository.rb
Original file line number Diff line number Diff line change
@@ -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
60 changes: 9 additions & 51 deletions lib/pact_broker/config/save.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down

0 comments on commit 30af8fb

Please sign in to comment.