Sha256: 3c9599c1f38205619c25ea6ad7e23f55daa1c2192a64ca5a990801a9cfc0c5cf

Contents?: true

Size: 960 Bytes

Versions: 2

Compression:

Stored size: 960 Bytes

Contents

module Wordmove
  class Guardian
    attr_reader :movefile, :environment, :action, :logger

    def initialize(options: nil, action: nil)
      @movefile = Wordmove::Movefile.new(options[:config])
      @environment = @movefile.environment(options).to_sym
      @action = action
      @logger = Logger.new(STDOUT).tap { |l| l.level = Logger::DEBUG }
    end

    def allows(task)
      if forbidden?(task)
        logger.warn("You tried to #{action} #{task}, but is forbidden by configuration. Skipping")
      end

      !forbidden?(task)
    end

    private

    def forbidden?(task)
      return false unless forbidden_tasks[task].present?
      forbidden_tasks[task] == true
    end

    def forbidden_tasks
      environment_options = movefile.fetch(false)[environment]
      return {} unless environment_options.key?(:forbid)
      return {} unless environment_options[:forbid].key?(action)

      environment_options[:forbid][action]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wordmove-3.0.1 lib/wordmove/guardian.rb
wordmove-3.0.0 lib/wordmove/guardian.rb