Skip to content

Commit

Permalink
Merge pull request #89 from kirillseva/master
Browse files Browse the repository at this point in the history
resource helper refactor
  • Loading branch information
robertzk committed Dec 14, 2015
2 parents f0d99ee + 462b7cf commit 221b1a8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: syberia
Type: Package
Title: Syberia
Version: 0.4.4.2
Version: 0.4.4.3
Description: Syberia provides an opinionated unified framework for
fast iteration on classifier development and deployment. It is
founded on convention over configuration and aims to solve the
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Version 0.4.4.3

* Allow `resource` to be called in tests to fetch other resources.

# Version 0.4.4.1

* Small changes to bring compatibility with stagerunner 0.5.0
Expand Down
31 changes: 18 additions & 13 deletions R/director.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ syberia_project <- local({
#'
#' The initial parsers that are derived from the syberia configuration
#' directory (\code{"config/"}) will be added to the director object here.
#' In particular, the \code{"config/routes.R"} file should specify the
#' controllers for various resource types, which will reside in
#' In particular, the \code{"config/routes.R"} file should specify the
#' controllers for various resource types, which will reside in
#' \code{"lib/controllers"} and be simple files with one function without
#' any arguments. This function will have the following present
#' when executed:
Expand All @@ -44,7 +44,7 @@ syberia_project <- local({
#' resource, then this will be the non-idempotent version. An
#' idempotent resource is a file which is located in a directory whose
#' name is the same as the filename without extension.
#'
#'
#' For example, \code{"models/infection/infection.R"} is an idempotent resource
#' because the \code{"infection.R"} file is present in the \code{"infection"}
#' directory and--ignoring file extension--they are identical. This kind of
Expand Down Expand Up @@ -79,7 +79,7 @@ syberia_project <- local({
#' value's \code{$value} key. Thus, if you had a file \code{"model.R"} with code
#' \code{column <- 'Sepal.Length'; lm(iris$Sepal.Width ~ iris[[column]])}
#' then we can access the \code{lm} model using \code{source('model.R')$value}.
#'
#'
#' This "file return value" is precisely the \code{output} that is available
#' to the controller for a resource.}
#'
Expand All @@ -100,7 +100,7 @@ syberia_project <- local({
#' 'data' = 'data')}
#'
#' where we have two kinds of resources: classifiers and data sources.
#'
#'
#' It is up to you how to define what these resources "do". The
#' \code{lib/classifiers} and \code{data} directories (in the root of your
#' syberia project) can have arbitrary code, and the \code{resource},
Expand Down Expand Up @@ -144,7 +144,7 @@ bootstrap_syberia_project <- function(project) {
#' Run custom bootstrapping and startup actions.
#'
#' When a syberia project is first loaded, it is "bootstrapped" by performing
#' several startup procedures: registering the config/application file,
#' several startup procedures: registering the config/application file,
#' registering the controllers, registering the routes, and setting up
#' the tests so that \code{test_project} works correctly. The user
#' can specify additional actions to perform using \code{custom_bootstrap}
Expand Down Expand Up @@ -238,7 +238,7 @@ register_tests <- function(project) {
#' should contain a list whose names give the route prefixes and whose
#' values are the controller names for parsing resources that begin with
#' these prefixes. For example, if the file contains
#'
#'
#' \code{list('models' = 'models', 'lib/adapters' = 'adapters')}
#'
#' then files in the directory \code{"models"} (relative to the root of the
Expand All @@ -252,7 +252,7 @@ register_tests <- function(project) {
#' See the function \code{bootstrap_syberia_project} to understand what
#' things are available in a controller function.
#'
#' @seealso \code{bootstrap_syberia_project}.
#' @seealso \code{bootstrap_syberia_project}.
routes_parser <- function() {
error <- function(...) {
stop("In your ", crayon::red("config/routes.R"), " file in the ",
Expand Down Expand Up @@ -307,7 +307,8 @@ routes_parser <- function() {
#' @param director director
#' @param source_args list
#' @param source function
default_tests_preprocessor <- function(resource_object, director, source_args, source) {
#' @param resource character
default_tests_preprocessor <- function(resource_object, director, source_args, source, resource) {
tested_resource <- gsub("^test\\/", "", resource)
if (!director$exists(tested_resource)) {
# TODO: (RK) Figure out how this interacts with virtual resources.
Expand All @@ -318,8 +319,13 @@ default_tests_preprocessor <- function(resource_object, director, source_args, s

context(tested_resource)
tested_resource_object <- director$resource(tested_resource)
source_args$local$resource <-
function() tested_resource_object$value(recompile. = TRUE)
source_args$local$resource <- function(path) {
if (missing(path)) {
tested_resource_object$value(recompile. = TRUE)
} else {
director$resource(path)
}
}
source()
}

Expand All @@ -331,7 +337,6 @@ default_tests_preprocessor <- function(resource_object, director, source_args, s
default_tests_environment_preprocessor <- function(director, source_args, source) {
# Provide access to the director for people with hardcore test setup
# and teardown hooks.
source_args$local$director <- director
source_args$local$director <- director
source()
}

0 comments on commit 221b1a8

Please sign in to comment.