2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-06 11:55:06 -04:00
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class AttributesFinder
|
2016-05-18 11:48:15 -04:00
|
|
|
def initialize(included_attributes:, excluded_attributes:, methods:)
|
2016-05-06 11:55:06 -04:00
|
|
|
@included_attributes = included_attributes || {}
|
|
|
|
@excluded_attributes = excluded_attributes || {}
|
2016-05-18 11:48:15 -04:00
|
|
|
@methods = methods || {}
|
2016-05-06 11:55:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def find(model_object)
|
|
|
|
parsed_hash = find_attributes_only(model_object)
|
|
|
|
parsed_hash.empty? ? model_object : { model_object => parsed_hash }
|
|
|
|
end
|
|
|
|
|
2016-05-09 11:58:43 -04:00
|
|
|
def parse(model_object)
|
|
|
|
parsed_hash = find_attributes_only(model_object)
|
|
|
|
yield parsed_hash unless parsed_hash.empty?
|
2016-05-06 11:55:06 -04:00
|
|
|
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
|
|
|
|
|
2016-05-18 11:48:15 -04:00
|
|
|
def find_method(value)
|
|
|
|
key = key_from_hash(value)
|
|
|
|
@methods[key].nil? ? {} : { methods: @methods[key] }
|
|
|
|
end
|
|
|
|
|
2018-05-03 05:02:26 -04:00
|
|
|
def find_excluded_keys(klass_name)
|
|
|
|
@excluded_attributes[klass_name.to_sym]&.map(&:to_s) || []
|
|
|
|
end
|
|
|
|
|
2016-05-06 11:55:06 -04:00
|
|
|
private
|
|
|
|
|
2016-05-09 11:58:43 -04:00
|
|
|
def find_attributes_only(value)
|
2016-05-18 11:48:15 -04:00
|
|
|
find_included(value).merge(find_excluded(value)).merge(find_method(value))
|
2016-05-09 11:58:43 -04:00
|
|
|
end
|
|
|
|
|
2016-05-06 11:55:06 -04:00
|
|
|
def key_from_hash(value)
|
|
|
|
value.is_a?(Hash) ? value.keys.first : value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-05-09 06:15:50 -04:00
|
|
|
end
|