Sha256: 6838f5e8d4c0c70c06794726fa06e4fd4b82e6fc4a3972bf191a88fbb78c4003

Contents?: true

Size: 1.87 KB

Versions: 5

Compression:

Stored size: 1.87 KB

Contents

# -*- encoding : utf-8 -*-
module Dao
  class Api
    class << Api
      def modes(*modes)
        @modes ||= []
        modes.flatten.compact.map{|mode| Api.add_mode(mode)} unless modes.empty?
        @modes
      end

      def modes=(*modes)
        modes(*modes)
      end

      def add_mode(mode)
        mode = Mode.for(mode)

        unless modes.include?(mode)
          module_eval(<<-__, __FILE__, __LINE__ - 1)
            def #{ mode }(*args, &block)
              if args.empty?
                if catching_results?
                  if self.mode.case_of?(Mode.for(#{ mode.inspect }))
                    mode(#{ mode.inspect }, &block)
                    return!
                  end
                else
                  mode(#{ mode.inspect }, &block)
                end
              else
                mode(#{ mode.inspect }) do
                  call(*args, &block)
                end
              end
            end

            def #{ mode }?(&block)
              mode?(#{ mode.inspect }, &block)
            end
          __

          modes.push(mode)
          mode
        else
          false
        end
      end
    end

    def mode=(mode)
      @mode = Mode.for(mode)
    end

    def mode(*args, &block)
      @mode ||= Mode.default

      if args.empty? and block.nil?
        @mode
      else
        if block
          mode = self.mode
          self.mode = args.shift
          begin
            return(instance_eval(&block))
          ensure
            self.mode = mode
          end
        else
          self.mode = args.shift
          return(self)
        end
      end
    end

    def mode?(mode, &block)
      condition = mode.case_of?(self.mode)
      if block.nil?
        condition
      else
        send(mode, &block) if condition
      end
    end
  end

  Api.modes = Mode.list

  Api.before_initializer do |*args|
    @mode = Mode.default
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dao-5.1.1 lib/dao/api/modes.rb
dao-5.0.0 lib/dao/api/modes.rb
dao-4.6.4 lib/dao/api/modes.rb
dao-4.4.4 lib/dao/api/modes.rb
dao-4.4.2 lib/dao/api/modes.rb