started refactoring import export reader - WIP

This commit is contained in:
James Lopez 2016-05-06 17:55:06 +02:00
parent b6ab4a3113
commit 8ac53eb5d0
4 changed files with 57 additions and 52 deletions

View file

@ -11,7 +11,7 @@ module Gitlab
end
def project_tree
Gitlab::ImportExport::ImportExportReader.project_tree
Gitlab::ImportExport::ImportExportReader.new.project_tree
end
def storage_path

View file

@ -0,0 +1,35 @@
module Gitlab
module ImportExport
class AttributesFinder
def initialize(included_attributes:, excluded_attributes:)
@included_attributes = included_attributes || {}
@excluded_attributes = excluded_attributes || {}
end
def find(model_object)
parsed_hash = find_attributes_only(model_object)
parsed_hash.empty? ? model_object : { model_object => parsed_hash }
end
def find_attributes_only(value)
find_included(value).merge(find_excluded(value))
end
def find_included(value)
key = key_from_hash(value)
@included_attributes[key].nil? ? {} : { only: @included_attributes[key] }
end
def find_excluded(value)
key = key_from_hash(value)
@excluded_attributes[key].nil? ? {} : { except: @excluded_attributes[key] }
end
private
def key_from_hash(value)
value.is_a?(Hash) ? value.keys.first : value
end
end
end
end

View file

@ -1,37 +1,27 @@
module Gitlab
module ImportExport
module ImportExportReader
extend self
class ImportExportReader
#FIXME
def project_tree
{ only: included_attributes[:project], include: build_hash(tree) }
def initialize(config: 'lib/gitlab/import_export/import_export.yml')
config = YAML.load_file('lib/gitlab/import_export/import_export.yml').with_indifferent_access
@tree = config[:project_tree]
@attributes_parser = Gitlab::ImportExport::AttributesFinder.new(included_attributes: config[:included_attributes],
excluded_attributes: config[:excluded_attributes])
end
def tree
config[:project_tree]
def project_tree
{ only: @attributes_parser.find_included(:project), include: build_hash(@tree) }
end
private
def config
@config ||= YAML.load_file('lib/gitlab/import_export/import_export.yml').with_indifferent_access
end
def included_attributes
config[:included_attributes] || {}
end
def excluded_attributes
config[:excluded_attributes] || {}
end
def build_hash(array)
array.map do |model_object|
def build_hash(model_list)
model_list.map do |model_object|
if model_object.is_a?(Hash)
process_include(model_object)
else
only_except_hash = check_only_and_except(model_object)
only_except_hash.empty? ? model_object : { model_object => only_except_hash }
@attributes_parser.find(model_object)
end
end
end
@ -51,48 +41,30 @@ module Gitlab
def process_current_class(hash, included_classes_hash, value)
value = value.is_a?(Hash) ? process_include(hash, included_classes_hash) : value
only_except_hash = check_only_and_except(hash.keys.first)
included_classes_hash[hash.keys.first] ||= only_except_hash unless only_except_hash.empty?
attributes_hash = @attributes_parser.find_attributes_only(hash.keys.first)
included_classes_hash[hash.keys.first] ||= attributes_hash unless attributes_hash.empty?
value
end
def add_new_class(current_key, included_classes_hash, value)
only_except_hash = check_only_and_except(value)
attributes_hash = @attributes_parser.find_attributes_only(value)
parsed_hash = { include: value }
unless only_except_hash.empty?
unless attributes_hash.empty?
if value.is_a?(Hash)
parsed_hash = { include: value.merge(only_except_hash) }
parsed_hash = { include: value.merge(attributes_hash) }
else
parsed_hash = { include: { value => only_except_hash } }
parsed_hash = { include: { value => attributes_hash } }
end
end
included_classes_hash[current_key] = parsed_hash
end
def add_to_class(current_key, included_classes_hash, value)
only_except_hash = check_only_and_except(value)
value = { value => only_except_hash } unless only_except_hash.empty?
attributes_hash = @attributes_parser.find_attributes_only(value)
value = { value => attributes_hash } unless attributes_hash.empty?
old_values = included_classes_hash[current_key][:include]
included_classes_hash[current_key][:include] = ([old_values] + [value]).compact.flatten
end
def check_only_and_except(value)
check_only(value).merge(check_except(value))
end
def check_only(value)
key = key_from_hash(value)
included_attributes[key].nil? ? {} : { only: included_attributes[key] }
end
def check_except(value)
key = key_from_hash(value)
excluded_attributes[key].nil? ? {} : { except: excluded_attributes[key] }
end
def key_from_hash(value)
value.is_a?(Hash) ? value.keys.first : value
end
end
end
end

View file

@ -17,8 +17,6 @@ describe Gitlab::ImportExport::ImportExportReader do
end
it 'should generate hash from project tree config' do
allow(described_class).to receive(:config).and_return(YAML.load_file(test_config).deep_symbolize_keys)
expect(described_class.project_tree).to eq(project_tree_hash)
expect(described_class.new(config: test_config).project_tree).to eq(project_tree_hash)
end
end