Sha256: 1e198d051f977bf09a588d1f34addbecbc5d39f6fc953366e78facb095eb40a9

Contents?: true

Size: 1.9 KB

Versions: 31

Compression:

Stored size: 1.9 KB

Contents

class RailsOps::Hookup
  REQUEST_STORE_KEY = 'RailsOps::Hookup'.freeze
  CONFIG_PATH = 'config/hookup.rb'.freeze

  attr_reader :hooks

  def self.instance
    if defined?(Rails) && Rails.env.development?
      return RequestStore.store[REQUEST_STORE_KEY] ||= new
    else
      @instance ||= new
      return @instance
    end
  end

  def initialize
    @hooks = nil
    @drawn = false
    @config_loaded = false
  end

  def load_config
    unless @config_loaded
      @config_loaded = true

      if File.exist?(CONFIG_PATH)
        load Rails.root.join(CONFIG_PATH)
      else
        Rails.logger.debug "RailsOps could not find hookup #{CONFIG_PATH}, using empty hookup configuration."
        draw do
          # Empty
        end
      end
    end

    unless @drawn
      fail 'Hooks are not drawn.'
    end
  end

  def draw(&block)
    if @drawn
      fail "Hooks can't be drawn twice."
    end

    dsl = DSL.new(&block)
    dsl.validate!

    @hooks = dsl.hooks
    @drawn = true
  end

  def hooks_for(operation, event)
    load_config

    hooks = []

    @hooks.slice('*', operation.class.name).values.each do |hooks_by_event|
      hooks += hooks_by_event.slice('*', event).values.flatten || []
    end

    return hooks
  end

  def trigger_params
    {}
  end

  def trigger(operation, event, params)
    context = operation.context.spawn(operation)
    context.called_via_hook = true

    hooks_for(operation, event).each do |hook|
      if context.op_chain.collect(&:class).collect(&:to_s).include?(hook.target_operation)
        next
      end

      begin
        op_class = hook.target_operation.constantize
      rescue NameError
        fail "Could not find hook target operation #{hook.target_operation}."
      end

      op = op_class.new(context, params)

      begin
        op.run!
      rescue *op.validation_errors => e
        fail RailsOps::Exceptions::HookupOpValidationFailed, e
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
rails_ops-1.4.1 lib/rails_ops/hookup.rb
rails_ops-1.4.0 lib/rails_ops/hookup.rb
rails_ops-1.3.0 lib/rails_ops/hookup.rb
rails_ops-1.2.3 lib/rails_ops/hookup.rb
rails_ops-1.2.2 lib/rails_ops/hookup.rb
rails_ops-1.2.1 lib/rails_ops/hookup.rb
rails_ops-1.2.0 lib/rails_ops/hookup.rb
rails_ops-1.1.31 lib/rails_ops/hookup.rb
rails_ops-1.1.30 lib/rails_ops/hookup.rb
rails_ops-1.1.29 lib/rails_ops/hookup.rb
rails_ops-1.1.28 lib/rails_ops/hookup.rb
rails_ops-1.1.27 lib/rails_ops/hookup.rb
rails_ops-1.1.26 lib/rails_ops/hookup.rb
rails_ops-1.1.25 lib/rails_ops/hookup.rb
rails_ops-1.1.24 lib/rails_ops/hookup.rb
rails_ops-1.1.23 lib/rails_ops/hookup.rb
rails_ops-1.1.22 lib/rails_ops/hookup.rb
rails_ops-1.1.21 lib/rails_ops/hookup.rb
rails_ops-1.1.20 lib/rails_ops/hookup.rb
rails_ops-1.1.19 lib/rails_ops/hookup.rb