Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand context when the value is an URI and expasion was requested in the config #204

Merged
merged 2 commits into from Feb 19, 2019

Conversation

elrayle
Copy link
Contributor

@elrayle elrayle commented Feb 14, 2019

Adds:

  • expands context for URIs to optionally include label and id

Fixes:

  • group label improperly passed default to I18n
  • clean up some comments
  • extracts parts of longer methods into private methods to avoid rubocop complaints and/or to keep DRY

Examples:

Regular context (from previous PR)

{
    property: "Authoritative Label",
    values: [
        "Science films"
    ],
    selectable: true,
    drillable: false
},

Expanding URI values in context (with this PR)

{
    group: "Hierarchy",
    property: "Narrower",
    values: [
        {
            uri: "http://id.loc.gov/authorities/genreForms/gf2011026416",
            id: "gf2011026416",
            label: "Nature films"
        }
    ],
    selectable: true,
    drillable: true
},

@elrayle elrayle changed the title Expand context when the value is an URI and expasion was requested in… Expand context when the value is an URI and expasion was requested in the config Feb 14, 2019
@elrayle elrayle changed the title Expand context when the value is an URI and expasion was requested in the config [WIP] Expand context when the value is an URI and expasion was requested in the config Feb 14, 2019
@@ -68,7 +68,7 @@ def add_group(group_id, group_map)
return if groups.key? group_id
i18n_key = Qa::LinkedData::Config::Helper.fetch(group_map, :group_label_i18n, nil)
default = Qa::LinkedData::Config::Helper.fetch(group_map, :group_label_default, nil)
return groups[group_id] = I18n.t(i18n_key, default) if i18n_key.present?
return groups[group_id] = I18n.t(i18n_key, default: default) if i18n_key.present?
Copy link
Contributor Author

@elrayle elrayle Feb 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BUGFIX: passing default incorrectly to i18n

def drillable?
@drillable
end

def group?
group_id.present?
end
Copy link
Contributor Author

@elrayle elrayle Feb 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

READABILITY: Just moved group? up so all the ? methods are together. No code change.

output.present? ? output['property'].uniq : nil
rescue
'PARSE ERROR'
ldpath_evaluate(basic_program, graph, subject_uri)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DRY: Moved the ldpath evaluate process to a private method so it can be shared by multiple methods.

@@ -21,7 +21,7 @@ class << self
def map_context(graph:, context_map:, subject_uri:)
context = []
context_map.properties.each do |property_map|
values = property_map.values(graph, subject_uri)
values = values(property_map, graph, subject_uri)
Copy link
Contributor Author

@elrayle elrayle Feb 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RUBOCOP: Extracted to a private method to appease rubocop.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elrayle you may be sad later on when you try and find values. It is not a very distinct name. Is it really expanded_values?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by renaming method values to property_values.

context = context_mapper_service.map_context(graph: graph, context_map: context_map, subject_uri: subject) if context_map.present?
value_map[:context] = context
value_map
map_context(graph, sort_key, context_map, value_map, subject)
Copy link
Contributor Author

@elrayle elrayle Feb 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RUBOCOP: Extracted to a private method to appease rubocop.

end
search_matches << values unless sort_key.present? && values[sort_key].blank?
search_matches << values if result_subject? values, sort_key
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DRY: Extracted to a private method so the check for sort_key can happen here and in the context mapping code.


def map_context(graph, sort_key, context_map, value_map, subject)
return value_map if context_map.blank?
return value_map unless result_subject? value_map, sort_key
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PERFORMANCE: Added this check because we shouldn't take the time to map context if the current subject isn't an actual search result.

allow(I18n).to receive(:t).with('qa.linked_data.authority.locnames_ld4l_cache.dates', 'default_Dates').and_return('Dates')
allow(I18n).to receive(:t).with('qa.linked_data.authority.locnames_ld4l_cache.authoritative_label', 'default_Authoritative Label').and_return('Authoritative Label')
allow(I18n).to receive(:t).with('qa.linked_data.authority.locnames_ld4l_cache.birth_date', 'default_Birth').and_return('Birth')
allow(I18n).to receive(:t).with('qa.linked_data.authority.locnames_ld4l_cache.death_date', 'default_Death').and_return('Death')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DEADCODE: These weren't needed for the tests to pass.

@@ -75,6 +71,10 @@
describe '#group_label' do
context 'when map defines group_label_i18n key' do
context 'and i18n translation is defined in locales' do
before do
allow(I18n).to receive(:t).with('qa.linked_data.authority.locnames_ld4l_cache.dates', default: 'default_Dates').and_return('Dates')
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

READABILITY: Moved closer to test.

@elrayle elrayle changed the title [WIP] Expand context when the value is an URI and expasion was requested in the config Expand context when the value is an URI and expasion was requested in the config Feb 15, 2019
@elrayle elrayle self-assigned this Feb 15, 2019
output = program.evaluate subject_uri, graph
output.present? ? output['property'].uniq : nil
rescue
'PARSE ERROR'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elrayle I know this is not new code, but where does the string show up to the user? I'm wondering if it should be logged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Cam156 Are you asking about the 'PARSE ERROR' string? This is a stop gap from the ldpath gem failing to parse some ldpaths. So it would show up here...

{
    group: "Hierarchy",
    property: "Narrower",
    values: [ 'PARSE ERROR' ],
    selectable: true,
    drillable: true
},

Not ideal. Better is perhaps logging and returning nothing for the property that wouldn't parse or values: ['']

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elrayle I was just wondering if it was something people would notice. Rescuing errors and silently handling them is something I have done and regretted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With 2nd commit, reworked the results so that the error is in a separate field in the hash...

{
    group: "Hierarchy",
    property: "Narrower",
    values: [],
    selectable: true,
    drillable: true,
    error: "LDPATH PARSE ERROR (See log for more information)"
},

Copy link
Contributor

@carolyncole carolyncole left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elrayle A couple small suggestions, but nothing blocking

@@ -21,7 +21,7 @@ class << self
def map_context(graph:, context_map:, subject_uri:)
context = []
context_map.properties.each do |property_map|
values = property_map.values(graph, subject_uri)
values = values(property_map, graph, subject_uri)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elrayle you may be sad later on when you try and find values. It is not a very distinct name. Is it really expanded_values?

end

def ldpath_evaluate(program, graph, subject_uri)
return PARSE_ERROR_VALUE if program.blank?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elrayle Hate to cause issues, but did you mean nil? Is the empty string a valid value?

Copy link
Contributor Author

@elrayle elrayle Feb 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With 2nd commit, now returns an empty array when an error occurs. The value on error is set in ContextMapperService using the ContextPropertyMap::VALUE_ON_ERROR constant.

property_info = {}
property_info["group"] = context_map.group_label(property_map.group_id) if property_map.group?
property_info["property"] = property_map.label
property_info["values"] = values
property_info["selectable"] = property_map.selectable?
property_info["drillable"] = property_map.drillable?
property_info["error"] = error if error.present?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@carolyncole carolyncole merged commit 9bb5987 into master Feb 19, 2019
@carolyncole carolyncole deleted the ld_refactor_expansion_ldpath branch February 19, 2019 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants