Code Monkey home page Code Monkey logo

zeroload's Introduction

README

Summary

Automatically autoload all constants for a module.
Reduces your requires to one line per file, plus external dependencies.
Enables requiring always only the precise file holding the constant you depend on (or top level if you depend on many).

Installation

gem install zeroload

Usage

Map your constants to filenames by replacing :: with /. For example, the constant Foo::BarBaz should be defined in file lib/Foo/BarBaz.rb. Note that the case matters! Each file should require the nesting namespace. That is, Foo/BarBaz.rb should require "Foo". All modules & classes always use zeroload.

Example

Given the files

  • lib/Foo.rb
  • lib/Foo/Bar.rb

The contents of lib/Foo.rb is:

require "Zeroload"

module Foo
  zeroload!

  # Code of Foo
end

And the contents of lib/Foo/bar.rb is:

require "Foo"

module Foo
  class Bar
    zeroload! # only necessary if there's stuff nested below 

    # Code of Foo::Bar
  end
end

Description

Automatically autoload all constants for a module. This requires a change from the current convention of putting class Foo::BarBaz into foo/barbaz.rb (or foo/bar_baz.rb in rails) to using Foo/BarBaz.rb instead.

Caveats

  • Q: But didn't have Kernel#autoload problems with multi-threading?
  • A: Yes, it did. They've since been resolved:
  • Q: But isn't Kernel#autoload deprecated?
  • A: Sadly, the state on this is not entirely clear. But it seems it is no longer deprecated.
  • Q: But with autoloaded constants, Module#constants will not report all constants.
  • A: That is not correct. Autoloaded (but not yet loaded) constants show up with Module#constants. But you can also use Zeroconf.preload! to load all constants at once.

zeroload's People

Contributors

apeiros avatar

Watchers

 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.