Class: Como::RuleCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/como.rb

Overview

Set of methods which represent option combination checking. In effect this is a meta language (DSL) for option combinations.

Example:

RuleCheck.check( opt ) do
    one(
        incr( "gcov", "exclude", "refreshed" ),
        "manifest",
        "pairs",
        "files"
        )
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt, &rule) ⇒ RuleCheck

Build Rule checker.



2249
2250
2251
# File 'lib/como.rb', line 2249

def initialize( opt, &rule )
    @opt = opt
end

Class Method Details

.check(opt) { ... } ⇒ Object

Perform rule checking options.

Parameters:

  • opt (Object)

    Options to check.

Yields:

  • rule Checking conditions.



2242
2243
2244
2245
# File 'lib/como.rb', line 2242

def RuleCheck.check( opt, &rule )
    rc = RuleCheck.new( opt )
    rc.instance_eval( &rule )
end

Instance Method Details

#all(*args) ⇒ Object

All are given.



2327
2328
2329
# File 'lib/como.rb', line 2327

def all( *args )
    getScore( *args ) == args.length
end

#any(*args) ⇒ Object

At least one is given.



2322
2323
2324
# File 'lib/como.rb', line 2322

def any( *args )
    getScore( *args ) > 0
end

#follow(*args) ⇒ Object

Incremental options in order i.e. have to have all later if had first.



2308
2309
2310
2311
2312
2313
2314
# File 'lib/como.rb', line 2308

def follow( *args )
    if getScore( args[0] )
        getScore( *args ) == args.length
    else
        false
    end
end

#getScore(*args) ⇒ Object

Get given count.



2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
# File 'lib/como.rb', line 2255

def getScore( *args )

    score = 0
    args.each do |i|
        if i.class == TrueClass || i.class == FalseClass
            score += 1 if i
        else
            score += 1 if @opt.argById(i).given
        end
    end

    score
end

#incr(*args) ⇒ Object

Incremental options in order i.e. have to have previous to have later.



2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
# File 'lib/como.rb', line 2276

def incr( *args )

    # Opts given consecutive.
    consecutiveCnt = 0

    # Consecutive flag.
    consecutive = true

    # All given opts.
    givenCnt = 0

    i = 0
    while args[i]
        score = getScore( args[i] )

        # Still consecutive.
        consecutiveCnt += 1 if ( score == 1 ) && consecutive

        # No more consecutives.
        consecutive = false if ( score == 0 )

        # Count all given.
        givenCnt += score

        i += 1
    end

    ( consecutiveCnt == givenCnt ) && ( givenCnt > 0 )
end

#inv(*args) ⇒ Object

Logical inversion.



2332
2333
2334
# File 'lib/como.rb', line 2332

def inv( *args )
    getScore( *args ) == 0
end

#meh(*args) ⇒ Object

Dont care.



2337
2338
2339
# File 'lib/como.rb', line 2337

def meh( *args )
    true
end

#noneObject

Special condition when no options are given.



2270
2271
2272
# File 'lib/como.rb', line 2270

def none
    @opt.givenCount == 0
end

#one(*args) ⇒ Object

One of list given.



2317
2318
2319
# File 'lib/como.rb', line 2317

def one( *args )
    getScore( *args ) == 1
end