Sha256: 1eece03e26f76211a3f57c706d2e5724f7307d7bd5a5150d2a68341629bb7ff3

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

module Checked
  module DSL
    
    # ============ Demand ==============
    
    %w{ String Array File_Path Hash Bool }.each { |name|
      eval %~
        def #{name}!( *args )
          #{name.downcase}!(*args).check!
        end
      ~
    }
    
    %w{ True False }.each { |bool|
      eval %~
        def #{bool}! *args
          bool!(*args).#{bool.downcase}!
        end
      ~
    }
    
    %w{ var array bool file_path string symbol hash }.each { |klass|
      eval %~
        def #{klass}! *args
          raise "No block allowed here." if block_given?
          check_it( '#{klass}!', *args )
        end
      
        def #{klass}? *args
          raise "No block allowed here." if block_given?
          check_it( 'ask', *args )
        end
      
        def #{klass} *args
          raise "No block allowed here." if block_given?
          check_it( 'clean',  *args )
        end
      ~
    }
    
    alias_method :demand!, :var!
    alias_method :ask?, :var?

    # ============= Ask ================

    def check_it namespace, *args
      args.unshift(nil) if args.size == 1
      ::Checked::DSL::Obj.new( namespace, *args )
    end

    def _main_class_ unk
      case unk
      when String
        'string'
      when Hash
        'hash'
      when Array
        'array'
      when Symbol
        'symbol'
      when TrueClass, FalseClass
        'bool'
      else
        raise ArgumentError, "Unknown class: #{unk.inspect}"
      end
    end

    def any? target, *args
      raise "No block allowed." if block_given?
      
      args.map { |a|
        send "#{klass}?", target, a
        check_it( 'ask', _main_class_(target), nil, target).send( a ).request.response.body
      }.compact == [true]
    end

    # ============ Clean ===============
    
  end # === module DSL
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
Checked-1.2.3 lib/Checked/Base/DSL.rb
Checked-1.2.2 lib/Checked/Base/DSL.rb
Checked-1.2.1 lib/Checked/Base/DSL.rb
Checked-1.2.0 lib/Checked/Base/DSL.rb
Checked-1.1.0 lib/Checked/Base/DSL.rb