Sinatra Best Practices: Part One

Posted on by in Web

While Sinatra’s one-file approach may work well for your one-off, smaller
application – it can quickly become a mess as you add on multiple routes,
route-handlers, helpers, and configuration. So what’s a programmer to do?

In reading Sinatra’s documentation I’ve found a few morsels that have enabled
us to split our otherwise-monolithic (I realize this term is becoming a cliché,
but given the single-file nature of Sinatra-based web-applications I feel like
it’s appropriate) applications into smaller, more manageable pieces.

The “Classical” Sinatra Application – Our Baseline

We’ll start with something paired down for simplicity’s sake. I’ll leave it as
an exercise to the reader to determine what this application does and instead
focus on code-style and organization.

Use Sinatra’s “Modular” Style

According to the Sinatra docs: “When a classic style
application is run, all Sinatra::Application public class methods are exported
to the top-level.” Also, using the classical style prevents you from running
more than
one Sinatra application per Ruby process – all
calls to these top-level methods are handled by Sinatra::Application,
functioning as a singleton. We can avoid these potentially-confusing scoping
problems by reorganizing our application into what Sinatra calls the “modular”
style, like so:

This app will need to be started with rackup via a config file (I called it
“config.ru”) that looks something like:

Reduce Duplication via Lambdas

One thing you may find yourself wanting to do is bind multiple routes to the
same handler. While there’s nothing keeping you from factoring this shared code
into a method invoked in each route-handler’s block – it would be nice if we
had some clean, concise way to remove the duplicate blocks entirely. Using
lambdas passed as blocks, you can visually separate routes from their handlers
and share the handlers across routes with ease.

Break Your Code Into Multiple Files

As your application grows larger (in line count) you’ll most likely want some
way of grouping together pieces of like-functionality into separate files which
are then required by your main Sinatra-application’s file. This can be achieved
using the “helpers” and “register” methods, like so:

Takeaway

These are just a few tips and tricks that you can use in your next
Sinatra-based project. We’ll keep posting as we learn new ways to simplify and
organize our code – so stay tuned!

In the mean time, I encourage you to review the Sinatra
documentation
and the excellent Sinatra
Explained
project by Zheng Jia.

Update

Be sure to check out Sinatra Best Practices: Part Two.

Credits

This article was co-written by Travis Herrick and Erin Swenson-Healey.


Feedback

  Comments: 10


  1. This article reads well enough, but I need to better understand some of the code. For example, how are you doing authentication? What does the login.erb look like? Sorry, I’m not a complete n00b, but pretty close 🙂


  2. Really cool article and direct to the point. Good job!


  3. Nice Article. Thank you.


  4. Lot of thanks! it’s great!


  5. Предлагаем полностью рабочее ПО/We offer a ready solution for SCANIA XCOM 2.23 ALL MODULES (CRACK – Dongle emulator/Custom license/Patch). Полная поддержка наших решений. Тестирование перед оплатой/Full support for our solutions. Testing before payment. Контакты/Contacts: nodongle24 /@/ gmail.com (remove spaces and /)

    FLEXlm License Manager, Rockey 4 Dongle Emulator, купить, ROCKEY 6 Dongle Emulator, Actikey Dongle Emulator, Dinkey Dongle Emulator, Tiny+ Dongle Emulator, Hardlock Dongle Emulator, Senselock Basic Dongle Emulator, Guardant Code Dongle Emulator, price, Rockey 2 Dongle Emulator, Biteblock3 Dongle Emulator, Guardant Net II Dongle Emulator, Sg-Lock Dongle Emulator.


  6. Оформление медкнижки без медосмотра – Купить больничный лист, Санитарна книжка в течении дня.


  7. online tech magazines – auto magazine, it magazines online.


  8. Имеется такая услуга – добровольное медицинское обслуживание .
    Она предполагает, что вы вносите небольшую сумму за абонемент и ходит на прием в течение года не платя за каждый прием.
    Однако опросы показали, что лишь 3% жителей города знают о такой услуге.
    Почему так происходит?
    Потому что частным клиникам намного выгодней сдирать с людей деньги за каждое посещение.
    А если какой-нибудь сотрудник клиники попытается рассказать про добровольное медицинское обслуживание клиенту – это сулит ему увольнением.
    Информация о ДМО уже спровоцировала кучу возмущений, сразу после того как информацию об услуге рассекретил один возмущенный врач.
    Его уволили “по собственному желанию”, после того, как он посоветовал ДМО постоянному клиенту.
    Самое удивительное, что официальные положения по ДМО находились в открытом доступе, просто находили на эту информацию единицы.
    Как отстоять свои права?
    О правилах оказания такой услуги и обязанностях клиник можно узнать, просто вбив в Яндекс фразу: “добровольное медицинское обслуживание”.
    Именно обслуживание, а не страхование.

    34j5c6h86

Your feedback