You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can now easily split your class definitions so that you have an isomorphic (i.e. runs on the server and client) definition, and a server-only definition.
This is especially useful for Server Operations and Active Record models.
Here is a sample directory structure, containing an application component, a model called Person, and an operation called PalindromeChecker. We also have a model called Orderbut it is only visible on the server. More on that later.
Note that the person.rb and palindrome_checker.rb files exist in two places: one in the hyperstack directory, and the other in the outer app directory.
Files in the hyperstackmodels, operations and shared directories will be executed on both the client and the server. This is how a method can be understood by both the client and the server.
Files in the outer app subdirectory are only recognized by the Rails server. So any information in those files, will not be seen on the client.
As a side note files in the hyperstack/components, /libs, and in fact, any other directories except /models, /operations, and /shared directories are only seen on the client. So there three kinds of files: Server-only (those in the outer app directories), server and client (those in hyperstack/models, /operations, and /shared directories, and client-only (any other hyperstack subdirectories)
Here is how we can use this difference to nicely structure our code that is sharing information between the server and client. We can put class definitions that are only relevant to the server in the outer app directories, and put class definitions that are used by both the client and server in the hyperstack directories.
So for example here are the two PalindromeChecker files:
All the client-side needs to know is that PalindromeChecker is a subclass of Hyperstack::ServerOp, and that it accepts a single param - a string to be checked. This definition is also available to the server, and will ensure that the type signature matches between the isomorphic (client and server) definition and the server-only definition.
The server-side file has all the implementation details including the acting_user declaration which can be used to validate who can be calling this operation. (In this case, we don't require an acting_user hence nils are allowed)
Here we have a helper method and some scopes defined in the isomorphic definition: These are available to both the client and the server.
However the server-side file also has relationships with another model, and because these are defined on the server-side they are not visible to the client.
In order for this to work, you do need to add a small snippet of code to your initializers/hyperstack.rb file which trains Rails to look in both directories for any constant definition. This code will be released into Hyperstack soon, but in the meantime feel free to use this snippet and clean up your classes:
# Add to your `initializers/hyperstack.rb filemoduleActiveSupportmoduleDependenciesHYPERSTACK_DIR='hyperstack'class << selfaliasoriginal_require_or_loadrequire_or_load# before requiring_or_loading a file, first check if# we have the same file in the server side directory# and add that as a dependencydefrequire_or_load(file_name,const_path=nil)add_server_side_dependency(file_name)original_require_or_load(file_name,const_path)end# search the filename path from the end towards the beginning# for the HYPERSTACK_DIR directory. If found, remove it from# the filename, and if a ruby file exists at that location then# add it as a dependencydefadd_server_side_dependency(file_name)path=File.expand_path(file_name.chomp('.rb')).split(File::SEPARATOR).reversehs_index=path.find_index(HYPERSTACK_DIR)returnunlesshs_index# no hyperstack directory herenew_path=(path[0..hs_index - 1] + path[hs_index + 1..-1]).reverseload_path=new_path.join(File::SEPARATOR)returnunlessFile.exist?"#{load_path}.rb"require_dependencyload_pathendendendend
The text was updated successfully, but these errors were encountered:
You can now easily split your class definitions so that you have an isomorphic (i.e. runs on the server and client) definition, and a server-only definition.
This is especially useful for Server Operations and Active Record models.
Here is a sample directory structure, containing an application component, a model called Person, and an operation called PalindromeChecker. We also have a model called Orderbut it is only visible on the server. More on that later.
Note that the
person.rb
andpalindrome_checker.rb
files exist in two places: one in thehyperstack
directory, and the other in the outer app directory.Files in the
hyperstack
models
,operations
andshared
directories will be executed on both the client and the server. This is how a method can be understood by both the client and the server.Files in the outer app subdirectory are only recognized by the Rails server. So any information in those files, will not be seen on the client.
As a side note files in the
hyperstack
/components
,/libs
, and in fact, any other directories except/models
,/operations
, and/shared
directories are only seen on the client. So there three kinds of files: Server-only (those in the outerapp
directories), server and client (those inhyperstack
/models
,/operations
, and/shared
directories, and client-only (any otherhyperstac
k subdirectories)Here is how we can use this difference to nicely structure our code that is sharing information between the server and client. We can put class definitions that are only relevant to the server in the outer
app
directories, and put class definitions that are used by both the client and server in thehyperstack
directories.So for example here are the two PalindromeChecker files:
All the client-side needs to know is that
PalindromeChecker
is a subclass ofHyperstack::ServerOp
, and that it accepts a single param - a string to be checked. This definition is also available to the server, and will ensure that the type signature matches between the isomorphic (client and server) definition and the server-only definition.The server-side file has all the implementation details including the acting_user declaration which can be used to validate who can be calling this operation. (In this case, we don't require an
acting_user
hence nils are allowed)Let's look at an Active Record example:
Here we have a helper method and some scopes defined in the isomorphic definition: These are available to both the client and the server.
However the server-side file also has relationships with another model, and because these are defined on the server-side they are not visible to the client.
In order for this to work, you do need to add a small snippet of code to your
initializers/hyperstack.rb
file which trains Rails to look in both directories for any constant definition. This code will be released into Hyperstack soon, but in the meantime feel free to use this snippet and clean up your classes:The text was updated successfully, but these errors were encountered: