2016-04-06 16:12:41 +00:00
|
|
|
module Projects
|
|
|
|
module ImportExport
|
|
|
|
module ImportExportReader
|
|
|
|
extend self
|
|
|
|
|
|
|
|
def project_tree
|
|
|
|
{ only: atts_only[:project], include: build_hash(tree) }
|
|
|
|
end
|
|
|
|
|
2016-04-12 13:32:25 +00:00
|
|
|
def tree
|
|
|
|
config[:project_tree]
|
|
|
|
end
|
|
|
|
|
2016-04-07 11:19:57 +00:00
|
|
|
private
|
|
|
|
|
2016-04-06 16:12:41 +00:00
|
|
|
def config
|
|
|
|
@config ||= YAML.load_file('app/services/projects/import_export/import_export.yml')
|
|
|
|
end
|
|
|
|
|
|
|
|
def atts_only
|
|
|
|
config[:attributes_only]
|
|
|
|
end
|
|
|
|
|
|
|
|
def atts_except
|
|
|
|
config[:attributes_except]
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_hash(array)
|
2016-04-07 16:14:42 +00:00
|
|
|
array.map do |el|
|
|
|
|
if el.is_a?(Hash)
|
|
|
|
process_include(el)
|
|
|
|
else
|
|
|
|
only_except_hash = check_only_and_except(el)
|
|
|
|
only_except_hash.empty? ? el : { el => only_except_hash }
|
|
|
|
end
|
|
|
|
end
|
2016-04-06 16:12:41 +00:00
|
|
|
end
|
|
|
|
|
2016-04-07 11:19:57 +00:00
|
|
|
def process_include(hash, included_classes_hash = {})
|
2016-04-06 16:12:41 +00:00
|
|
|
hash.values.flatten.each do |value|
|
2016-04-08 13:07:12 +00:00
|
|
|
current_key = hash.keys.first
|
|
|
|
value = process_current_class(hash, included_classes_hash, value)
|
2016-04-07 11:19:57 +00:00
|
|
|
if included_classes_hash[current_key]
|
|
|
|
add_class(current_key, included_classes_hash, value)
|
|
|
|
else
|
|
|
|
add_new_class(current_key, included_classes_hash, value)
|
|
|
|
end
|
2016-04-06 16:12:41 +00:00
|
|
|
end
|
|
|
|
included_classes_hash
|
|
|
|
end
|
|
|
|
|
2016-04-07 11:19:57 +00:00
|
|
|
def process_current_class(hash, included_classes_hash, value)
|
|
|
|
value = value.is_a?(Hash) ? process_include(hash, included_classes_hash) : value
|
2016-04-08 13:07:12 +00:00
|
|
|
only_except_hash = check_only_and_except(hash.keys.first)
|
|
|
|
included_classes_hash[hash.keys.first] ||= only_except_hash unless only_except_hash.empty?
|
|
|
|
value
|
2016-04-07 11:19:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_new_class(current_key, included_classes_hash, value)
|
2016-04-08 13:07:12 +00:00
|
|
|
new_hash = { include: value }
|
2016-04-07 11:19:57 +00:00
|
|
|
new_hash.merge!(check_only_and_except(value))
|
|
|
|
included_classes_hash[current_key] = new_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_class(current_key, included_classes_hash, value)
|
2016-04-07 16:14:42 +00:00
|
|
|
only_except_hash = check_only_and_except(value)
|
|
|
|
value = { value => only_except_hash } unless only_except_hash.empty?
|
2016-04-07 11:19:57 +00:00
|
|
|
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
|
|
|
|
|
2016-04-06 16:12:41 +00:00
|
|
|
def check_only(value)
|
2016-04-07 11:19:57 +00:00
|
|
|
key = key_from_hash(value)
|
2016-04-06 16:12:41 +00:00
|
|
|
atts_only[key].nil? ? {} : { only: atts_only[key] }
|
|
|
|
end
|
2016-04-07 11:19:57 +00:00
|
|
|
|
|
|
|
def check_except(value)
|
|
|
|
key = key_from_hash(value)
|
|
|
|
atts_except[key].nil? ? {} : { except: atts_except[key] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def key_from_hash(value)
|
|
|
|
value.is_a?(Hash) ? value.keys.first : value
|
|
|
|
end
|
2016-04-06 16:12:41 +00:00
|
|
|
end
|
|
|
|
end
|
2016-04-08 13:07:12 +00:00
|
|
|
end
|