This is the base Moonshine Manifest class, which provides a simple system for loading moonshine recpies from plugins, a template helper, and parses several configuration files:
config/moonshine.yml
The contents of config/moonshine.yml are expected to serialize into a hash, and are loaded into the manifest’s Configatron::Store.
config/database.yml
The contents of your database config are parsed and are available at configuration[:database].
If you’d like to create another ‘default rails stack’ using other tools that what Moonshine::Manifest::Rails uses, subclass this and go nuts.
Methods
public class
public instance
Public class methods
The current deployment target. Best when used with capistrano-ext’s multistage settings.
# File lib/moonshine/manifest.rb, line 65 65: def self.deploy_stage 66: ENV['DEPLOY_STAGE'] || 'undefined' 67: end
Load a Moonshine Plugin
class MyManifest < Moonshine::Manifest # Evals vendor/plugins/moonshine_my_app/moonshine/init.rb plugin :moonshine_my_app # Evals lib/my_recipe.rb plugin 'lib/my_recipe.rb' ... end
# File lib/moonshine/manifest.rb, line 30 30: def self.plugin(name = nil) 31: if name.is_a?(Symbol) 32: path = File.join(rails_root, 'vendor', 'plugins', 'moonshine_' + name.to_s, 'moonshine', 'init.rb') 33: else 34: path = name 35: end 36: Kernel.eval(File.read(path), binding, path) 37: true 38: end
The current Rails environment
# File lib/moonshine/manifest.rb, line 50 50: def self.rails_env 51: ENV["RAILS_ENV"] || 'production' 52: end
The working directory of the Rails application this manifests describes.
# File lib/moonshine/manifest.rb, line 41 41: def self.rails_root 42: @rails_root ||= File.expand_path(ENV["RAILS_ROOT"] || Dir.getwd) 43: end
Public instance methods
The current environment’s database configuration
# File lib/moonshine/manifest.rb, line 60 60: def database_environment 61: configuration[:database][rails_env.to_sym] 62: end
The current deployment target. Best when used with capistrano-ext’s multistage settings.
# File lib/moonshine/manifest.rb, line 70 70: def deploy_stage 71: self.class.deploy_stage 72: end
Only run tasks on the specified deploy_stage.
You can call it with the exact stage you want to run on:
on_stage(:my_stage) do puts "I'm on my_stage" end
Or you can pass an array of stages to run on:
on_stage(:my_stage, :my_other_stage) do puts "I'm on one of my stages" end
Or you can run a task unless it is on a stage:
on_stage(:unless => :my_stage) do puts "I'm not on my_stage" end
Or you can run a task unless it is on one of several stages:
on_stage(:unless => [:my_stage, :my_other_stage]) do puts "I'm not on my stages" end
# File lib/moonshine/manifest.rb, line 99 99: def on_stage(*args) 100: options = args.extract_options! 101: if_opt = options[:if] 102: unless_opt = options[:unless] 103: 104: unless if_opt || unless_opt 105: if_opt = args 106: end 107: 108: if if_opt && if_opt.is_a?(Array) && if_opt.map {|x| x.to_s}.include?(deploy_stage) 109: yield 110: elsif if_opt && (if_opt.is_a?(String) || if_opt.is_a?(Symbol)) && deploy_stage == if_opt.to_s 111: yield 112: elsif unless_opt && unless_opt.is_a?(Array) && !unless_opt.map {|x| x.to_s}.include?(deploy_stage) 113: yield 114: elsif unless_opt && (unless_opt.is_a?(String) || unless_opt.is_a?(Symbol)) && deploy_stage != unless_opt.to_s 115: yield 116: end 117: end
The current Rails environment
# File lib/moonshine/manifest.rb, line 55 55: def rails_env 56: self.class.rails_env 57: end
# File lib/moonshine/manifest.rb, line 45 45: def rails_root 46: self.class.rails_root 47: end
Render the ERB template located at pathname. If a template exists with the same basename at RAILS_ROOT/app/manifests/templates, it is used instead. This is useful to override templates provided by plugins to customize application configuration files.
# File lib/moonshine/manifest.rb, line 123 123: def template(pathname, b = binding) 124: template_contents = nil 125: basename = pathname.index('/') ? pathname.split('/').last : pathname 126: if File.exist?(File.expand_path(File.join(rails_root, 'app', 'manifests', 'templates', basename))) 127: template_contents = File.read(File.expand_path(File.join(rails_root, 'app', 'manifests', 'templates', basename))) 128: elsif File.exist?(File.expand_path(pathname)) 129: template_contents = File.read(File.expand_path(pathname)) 130: else 131: raise LoadError, "Can't find template #{pathname}" 132: end 133: ERB.new(template_contents).result(b) 134: end