Skip to content

Commit

Permalink
Refactor the context related code to its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
Moohan committed Apr 19, 2024
1 parent 29079e4 commit 2985f44
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
17 changes: 17 additions & 0 deletions R/add_context.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
add_context <- function(data, id, name, created_date, modified_date) {
# Catch if the resource has never been modified
if (is.null(modified_date)) {
modified_date <- NA_character_
}

data_with_context <- dplyr::mutate(
data,
"res_id" = id,
"res_name" = name,
"res_created_date" = created_date,
"res_modified_date" = modified_date,
.before = dplyr::everything()
)

return(data_with_context)
}
19 changes: 5 additions & 14 deletions R/get_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,12 @@ get_dataset <- function(dataset_name,
all_data <- purrr::pmap(
list(
"data" = all_data,
"ids" = selection_ids,
"names" = purrr::map_chr(content$result$resources[res_index], ~ .x$name),
"created_dates" = purrr::map_chr(content$result$resources[res_index], ~ .x$created),
"modified_dates" = purrr::map_chr(content$result$resources[res_index], ~ .x$last_modified)
"id" = selection_ids,
"name" = purrr::map_chr(content$result$resources[res_index], ~ .x$name),
"created_date" = purrr::map_chr(content$result$resources[res_index], ~ .x$created),
"modified_date" = purrr::map_chr(content$result$resources[res_index], ~ .x$last_modified)
),
function(data, ids, names, created_dates, modified_dates) {
dplyr::mutate(
data,
"res_id" = ids,
"res_name" = names,
"res_created_date" = created_dates,
"res_modified_date" = modified_dates,
.before = dplyr::everything()
)
}
add_context
)
}

Expand Down
16 changes: 5 additions & 11 deletions R/get_resource.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,12 @@ get_resource <- function(res_id,
res_created_date <- context_content$result$created
res_modified_date <- context_content$result$modified

# Catch if the resource has never been modified
if (is.null(res_modified_date)) {
res_modified_date <- NA_character_
}

data <- data %>%
dplyr::mutate(
res_id = res_id,
res_name = res_name,
res_created_date = res_created_date,
res_modified_date = res_modified_date,
.before = dplyr::everything()
add_context(
id = res_id,
name = res_name,
created_date = res_created_date,
modified_date = res_modified_date
)
}

Expand Down

0 comments on commit 2985f44

Please sign in to comment.