Skip to content

Commit

Permalink
Further refactor diffing code
Browse files Browse the repository at this point in the history
Only diff in one direction, vary argument order to change results.
Easier to read (I hope).
  • Loading branch information
duncanjbrown committed Jul 21, 2022
1 parent 5f8277c commit d96489f
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions lib/dfe/analytics/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,26 @@ def self.allowlist
DfE::Analytics.allowlist
end

def self.database
DfE::Analytics.all_entities_in_application
.reduce({}) do |list, entity|
attrs = DfE::Analytics.model_for_entity(entity).attribute_names
list[entity] = attrs

list
end
end

def self.generate_blocklist
diff_model_attributes_against(allowlist)[:missing]
diff_between(database, allowlist)
end

def self.unlisted_fields
diff_model_attributes_against(allowlist, blocklist)[:missing]
diff_between(database, allowlist, blocklist)
end

def self.surplus_fields
diff_model_attributes_against(allowlist)[:surplus]
diff_between(allowlist, database)
end

def self.conflicting_fields
Expand All @@ -33,25 +43,24 @@ def self.conflicting_fields
end
end

def self.diff_model_attributes_against(*lists)
DfE::Analytics.all_entities_in_application
.reduce({ missing: {}, surplus: {} }) do |diff, entity|
attributes_considered = lists.map do |list|
# for each list of model attrs, look up the attrs for this model
list.fetch(entity, [])
end.reduce(:concat)

model = DfE::Analytics.model_for_entity(entity)

missing_attributes = model.attribute_names - attributes_considered
surplus_attributes = attributes_considered - model.attribute_names

diff[:missing][entity] = missing_attributes if missing_attributes.any?
# extract and concatenate the fields associated with an entity in 1 or
# more entity->field lists
def self.extract_entity_attributes_from_lists(entity, *lists)
lists.map do |list|
list.fetch(entity, [])
end.reduce(:|)
end

diff[:surplus][entity] = surplus_attributes if surplus_attributes.any?
# returns keys and values present in leftmost list and not present in any
# of the other lists
def self.diff_between(primary_list, *lists_to_compare)
primary_list.reduce({}) do |diff, (entity, attrs_in_primary_list)|
attrs_in_lists_to_compare = extract_entity_attributes_from_lists(entity, *lists_to_compare)
differing_attrs = attrs_in_primary_list - attrs_in_lists_to_compare
diff[entity] = differing_attrs if differing_attrs.any?

diff
end
diff
end
end
end
end
Expand Down

0 comments on commit d96489f

Please sign in to comment.