module BranchableCDNAssets::CheckBefore

this abstracts creating filter methods not offering inheritance currently

check_before :check, params={}

to only check for specific methods, pass an array of methods to the :methods key

check_before :check, methods: ["foo", "bar"]

Public Class Methods

extended(base) click to toggle source
# File lib/branchable_cdn_assets/check_before.rb, line 30
def self.extended base
  base.class_eval do

    # @param action [Symbol] the method to execute
    # @param *args [Array] arguments to pass to the check method
    def with_check action, *args
      self.class.before_checks.select do |c|
        c[:methods].nil? || c[:methods].include?(action)
      end.each do |c|
        send c[:check]
      end

      public_send(action, *args)
    end

  end
end

Public Instance Methods

before_checks() click to toggle source
# File lib/branchable_cdn_assets/check_before.rb, line 15
def before_checks
  @_before_checks ||= []
end
check_before(check, params={}) click to toggle source

define a before check for a specific method

@param method [Symbol] the method to check on @param check [Symbol] the check to run @param params [Hash] additional parameters for the check @return [Void]

# File lib/branchable_cdn_assets/check_before.rb, line 25
def check_before check, params={}
  before_checks << params.merge( check: check )
end
with_check(action, *args) click to toggle source

@param action [Symbol] the method to execute @param *args [Array] arguments to pass to the check method

# File lib/branchable_cdn_assets/check_before.rb, line 35
def with_check action, *args
  self.class.before_checks.select do |c|
    c[:methods].nil? || c[:methods].include?(action)
  end.each do |c|
    send c[:check]
  end

  public_send(action, *args)
end