DannyBen/jobly

View on GitHub
lib/jobly/module_functions.rb

Summary

Maintainability
A
0 mins
Test Coverage
require 'logger'

module Jobly
  class << self
    attr_reader :logger

    def configure
      yield self
    end

    def load_custom_config
      require config_file if File.exist? config_file
    end

    def default_options
      {
        root:              Dir.pwd,
        environment:       ENV['JOBLY_ENVIRONMENT'] || 'development',
        api_url:           ENV['JOBLY_API_URL'] || 'http://localhost:3000/do',
        app_path:          ENV['JOBLY_APP_PATH'] || 'app',
        jobs_path:         ENV['JOBLY_JOBS_PATH'] || 'jobs',
        config_path:       ENV['JOBLY_CONFIG_PATH'] || 'config',
        redis_url:         ENV['JOBLY_REDIS_URL'] || 'redis://localhost:6379/0',
        status_expiration: ENV['JOBLY_STATUS_EXPIRATION']&.to_i || 30,
        jobs_namespace:    ENV['JOBLY_JOBS_NAMESPACE'],
        slack_webhook:     ENV['JOBLY_SLACK_WEBHOOK'],
        slack_channel:     ENV['JOBLY_SLACK_CHANNEL'] || '#general',
        slack_user:        ENV['JOBLY_SLACK_USER'] || 'Jobly',
        log:               ENV['JOBLY_LOG'],
        log_level:         ENV['JOBLY_LOG_LEVEL'] || 'info',
        auth:              ENV['JOBLY_AUTH'],
        secret:            ENV['JOBLY_SECRET'] ||
          'change this cookie to a string that is at least 64 characters long',
        shell_dry_run:     ENV['JOBLY_SHELL_DRY_RUN'],
        mounts:            nil,
      }
    end

    def method_missing(method, args = nil, &)
      key = method.to_s
      assign = key[-1] == '='
      key = key.chomp('=') if assign
      key = key.to_sym

      if options.has_key? key
        assign ? options[key] = args : options[key]
      else
        super
      end
    end

    def respond_to_missing?(method, include_private = false)
      key = method.to_s.chomp('=').to_sym
      options.has_key?(key) ? true : super
    end

    def options
      @options ||= default_options.dup
    end

    def log=(target)
      options[:log] = target
      @logger = if target.is_a? Logger
        target
      elsif target
        Log.new target, :jobly
      end

      @logger.level = log_level if @logger && @logger.respond_to?(:level)
    end

    def full_app_path
      File.expand_path app_path, root
    end

    def full_jobs_path
      File.expand_path jobs_path, root
    end

    def full_config_path
      File.expand_path config_path, root
    end

    def config_file
      File.expand_path 'jobly.rb', full_config_path
    end

    def custom_config?
      File.exist? config_file
    end
  end
end