Sha256: a2542a45540245b005a1a8f1ee726bc3987a8abcb3497b11ccb178feb11673d5

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

module ActiveScripts
  module Recipes
    class Base < ActiveScripts::Base

      # INFO: ActiveScripts::Recipes::Base contains code that is
      #   shared between all recipe files.

      DATABASES    = ["mysql"]
      ENVIRONMENTS = [:development, :staging, :production]

      attr_accessor :environment

      def initialize
        @environment = :unknown
      end

      def self.packages
        assert_valid_user!
        new.packages
      end

      private

      def assert_valid_environment!
        unless ENVIRONMENTS.include?(@environment)
          raise ArgumentError,
            "Unknown recipe environment: #{@environment.inspect}. Valid recipe environments are: #{ENVIRONMENTS.map(&:inspect).join(', ')}"
        end
      end

      ENVIRONMENTS.each do |environment|
        define_method("environment_#{environment}?") do
          @environment == environment
        end
      end

      def find_or_initialize_databases!
        output, error, status = Open3.capture3("brew list")
        output = output.squish

        databases = []
        DATABASES.each { |database| databases.push(database) if output.include?(database) }

        if databases.empty?
          ask("   [?] Which database(s)? ", lambda { |str| str.strip.split(/,\s*/) }) { |q| q.default = "mysql" }
        else
          return(databases)
        end
      end

      def prompt_environment!
        @environment = choose("   [?] Which environment?", :development, :staging, :production)
      end

      def notify_environment_unavailable!
        say_warning("   [!] RecipeError")
        say_warning("     - The recipe environment is not available.")
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_scripts-0.1.0 lib/active_scripts/recipes/base.rb