Sha256: b9333f7475987caa04d87f33f2ad2dff7d5582a598a7502cf94e038237f383aa

Contents?: true

Size: 766 Bytes

Versions: 1

Compression:

Stored size: 766 Bytes

Contents

module Cases
  class Case
    attr_accessor :result
    attr_reader :method, :event, :options, :block

    def self.all
      @_all ||= {}
    end

    def initialize(method, event, options = {}, &block)
      @method = method
      @event = event
      @block = block
      @options = options

      register
    end

    def ==(other)
      method == other.method &&
        event == other.event
    end

    def execute(object, result)
      return if !result.send(event)

      self.result = block.call(options[:on_self] ? object : result)
    end

    private

    def register
      cases << self if !registered?
    end

    def registered?
      cases.any? { |kase| kase == self }
    end

    def cases
      self.class.all[method] ||= []
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cases-1.0.0 lib/cases/case.rb