GithubHelp home page GithubHelp logo

mrpowers / turf Goto Github PK

View Code? Open in Web Editor NEW
5.0 5.0 1.0 22 KB

Set application variables for the development, test, and production environments

License: MIT License

Ruby 98.45% Shell 1.55%

turf's Introduction

Turf

Turf lets you control the value of variables in different environments and makes it easy to override values locally. It's easy to set speak_in_chat to true when RAILS_ENV equals "production" and false otherwise. Turf is similar to the Rails secrets.yml file, but more powerful because it can execute Ruby code and return arrays, hashes, etc.

How it works

Turf looks for methods in the following order:

  1. The Turf::Local class.
  2. The Turf::Test, Turf::Development, or Turf::Production class. Turf uses the development environment by default, but this can be overridden by setting RAILS_ENV to "production" or "test".
  3. The Turf::Default class.

I recommend defining the Turf classes in the /config/turf directory.

Examples

ENV["RAILS_ENV"] = "production"

class Turf::Local
  def something
    "something in local"
  end
end

class Turf::Development
  def blah
    "blah in development"
  end
end

class Turf::Production
  def something
    "something in production"
  end

  def blah
    "blah in production"
  end
end

class Turf::Default
  def four
    2 + 2
  end
end

# Turf::Local is the first place Turf looks for a
# matching method
Turf.something # => "something in local"

# The RAILS_ENV is set to production, so Turf looks
# in Turf::Production second if the method is not
# found in Turf::Local
# Turf::Development is ignored in production
Turf.blah # => "blah in production"

# Turf::Default is the last place to look
Turf.four # => 4

# Turf raises an exception when it can't find
# a matching method
Turf.hi_there # => raises an exception

Setup

Add this line to your application's Gemfile:

gem 'turf'

Require turf:

require 'turf'

Suggested Setup

Include the Turf setup rake task in your project's Rakefile:

load "tasks/setup.rake"

Run the rake task to create the classes in your project:

bundle exec rake turf:setup

Ruby Projects (see below for Rails Projects)

Require all the files in the /lib/#{project_name}.rb file:

require_relative "../config/turf/default.rb"

def require_all(pattern)
  Dir.glob("#{Turf.root}/#{pattern}/**/*.rb").sort.each { |path| require path }
end

require_all("config/turf")

RAILS_ENV is used to manage the environment for compatibility with other gems

Set the RAILS_ENV to "develoment" at the top of the /lib/#{project_name}.rb file:

ENV['RAILS_ENV'] ||= "development"

Set the RAILS_ENV to "test" in the spec_helper.rb file:

ENV['RAILS_ENV'] = 'test'

Set the RAILS_ENV to production on the remote host.

Rails Projects

Require all the Turf files in the config/application.rb file:

Dir.glob("#{Rails.root}/config/turf/**/*.rb").each { |path| require path }

That's it!

.gitignore Turf::Local

Application secrets can be stored in Turf::Local and the file can be gitignored so these secrets are not exposed in source control. Add this line (/config/turf/local.rb) to your .gitignore file and scp the local.rb file to the remote host when changes are made.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/MrPowers/turf.

License

The gem is available as open source under the terms of the MIT License.

turf's People

Contributors

mrpowers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

eclosson

turf's Issues

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.