-
Notifications
You must be signed in to change notification settings - Fork 18
What is HyperLoop
@catmando
Ruby HyperLoop is a client application framework based on React.js with a unique combination of features:
- Ruby is used throughout both on the client and on the server.
- Server side ActiveRecord models are automatically accessible from the client code.
- Complex operations are encapsulated as Actions which can run on the client, server or both.
- Client-Server communication is automatically and transparently handled by the framework.
- Integrates easily with existing React.js libraries and components
- Integrates easily with existing Rails applications
The goal of HyperLoop is to allow the developer to write code that is directed toward solving the user's needs in the most straight forward manner, without redundant code, unnecessary APIs, or artificial separation between client and server.
Here is a slice of an example application:
class BookList < React::Component::Base
# Display each book in our catalog unless its already in the cart basket.
# When the user clicks on a book, add it the Basket.
render(UL) do
Book.all.each do |book|
LI { "Add #{book.name}" }.on(:click) do
AddBookToBasket(book: book) do |outcome|
alert "Failed to add the book" unless outcome.success?
end
end unless acting_user.basket.include? book
end
end
end
class AddBookToBasket < HyperLoop::Action
# Add a book to the basket and add to users watchlist
param :book, type: Book
def execute
acting_user.basket << book
AddToActingUsersWatchList(book: params.book)
end
end
class AddToActingUsersWatchList < HyperLoop::ServerAction
# Add a book the the current acting_user's watch list, and
# send an initial email about the book.
# This Action can only be run on the server because it is going
# to sending mail, so it inherits from ServerAction.
# only clients with a logged in user can access this action.
allow_operation { acting_user }
param :book, type: Book
def execute
return if acting_user.watch_list.include? params.book
WatchListMailer.new_book_email WatchList.create(user: acting_user, book: params.book)
end
end
This is a complete example with the only things not shown are the definition of the WatchListMailer
and the ActiveRecord models. These would both be implemented in the standard Rails way.
A quick look at the code shows the power of HyperLoop
- Everything is written in Ruby.
- Our
BookList
is a Ruby Class that generates our HTML using React.js. -
BookList
directly scopes the ARBook
model and accesses its attributes and relationships. - Adding a book to the basket will automatically update the book list html.
- The relationship between the html output and the user action is clear.
- The
Action
class nicely encapsulates activities into reusable chunks of code. - Externally there no difference between an action executing on the server or the client.
- While this code will continuously move data between the server and client there are no APIs or even controllers needed.
- Security is enforced through policy regulations like
allow_operation
which can be applied to models as well as actions.
In addition what you cannot see from looking at the code is that any changes made to the database will be synchronized on clients that are observing that data. So for example we could have this component to display the most watched books:
class TopBooks < React::Component::Base
render(UL) do
Book.top_watched_books.each_with_index do |book, rank|
LI { "#{rank} #{book}" }
end
end
end
As books are added (or removed) from users' watch lists, the top_watched_books scope will change, and any browsers depending on that scope will be updated.
HyperLoop is here so you can write apps quickly, easily, in a great programming language, while you pay attention to what the user needs done.