Sha256: c59513751e6d361d77b521a62bdc35e8832fa1c443fdfe16060d88b8e5f883ca

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module CanTango::Ability::Executor
  class Modal < Base    
    def initialize candidate, modes, options = {}
      super candidate, options
      extract_modes modes
      execute
    end

    def calculate_rules
      @rules = modes.inject([]) do |result, mode|
        result = result + modal_rules(mode)
        result
      end
      normalize_rules!
    end

    def execute
      return if executed?
      clear_rules!
      calculate_rules
    rescue Exception => e
      debug e.message      
    ensure
      @executed = true
      rules
    end

    def finder
      @finder ||= CanTango::Ability::Mode::Finder.new self
    end

    protected

    def extract_modes modes
      raise ArgumentError, "Modes must defined!" if !modes
      modes = modes.kind_of?(Hash) ? modes_from_option(modes) : modes
      modes = [modes].flatten
      raise ArgumentError, "Modes must be a list of modes to execute!" if modes.blank?
      @modes = modes
    end

    def modes_from_option options
      options[:modes] || options[:mode]
    end

    def modal_rules mode
      mode?(mode) ? executor(mode).execute : []
    end

    def executor mode
      @executor ||= finder.executor_for(mode)
    end

    def mode? mode
      modes.include?(mode)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cantango-core-0.1.7 lib/cantango/ability/executor/modal.rb