Giter Site home page Giter Site logo

awesome_loader's Introduction

awesome_loader Build Status

So you've created your bespoke Ruby application without Rails. Then you thought, "Bollocks, I have to manually require all my application files, and in a certain order! And I have to explicitly define all my submodules - they don't magically appear based on the directory structure like they did in Rails. There has to be a better way!" Well now there is.

Status

This is an actively used and maintained project. It may look dead, but that's because there's nothing left to do.

Install

Add to your Gemfile.

gem 'awesome_loader'

Basic Usage

Let's say you have a fairly simple layout, somewhat inspired by Rails.

  • app/models/widget.rb contains class Widget
  • app/models/billing/line_item.rb contains class Billing::LineItem
  • app/helpers/app_helpers.rb contains module AppHelpers

Given those files and their contents, this is all you have to tell awesome_loader. Awesome, right?

AwesomeLoader.autoload do
  paths %w(app ** *.rb)
end

Advanced Usage

Maybe your app structure is more complicated. That's fine too. Note the root_depth: 2 argument. That's saying, "Only start creating modules for dirs after the first 2 levels." That means app and app/* won't get any modules, but deeper directories, like app/models/billing, will. 2 is the default, as you can see in the above example.

AwesomeLoader.autoload root_depth: 2 do
  # These files have top-level Routes and Entities modules
  paths %w(app routes ** *.rb), root_depth: 1
  paths %w(app entities ** *.rb), root_depth: 1

  # Load everything else using the default root_depth
  paths %w(app ** *.rb)

  # Load your app's initializers. Any classes/modules in them will be autoloaded.
  require %w(config initializers *.rb)
end

For more details and options, check out the documentation.

Eager Loading

If you're running a threaded server like Puma or Thin, it's usually considered best practice to load everything up-front (at least in production), instead of lettings things load while other threads might be running. The eager_load option will ensure that all files are loaded before the block exits.

AwesomeLoader.autoload eager_load: is_prod? do
  paths %w(app ** *.rb)
end

License

MIT License. See LICENSE for details.

Copyright

Copyright (c) 2017 Jordan Hollinger

awesome_loader's People

Contributors

jhollinger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.