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.



2222
2223
2224
# File 'lib/como.rb', line 2222

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.



2215
2216
2217
2218
# File 'lib/como.rb', line 2215

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

Instance Method Details

#all(*args) ⇒ Object

All are given.



2300
2301
2302
# File 'lib/como.rb', line 2300

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

#any(*args) ⇒ Object

At least one is given.



2295
2296
2297
# File 'lib/como.rb', line 2295

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

#follow(*args) ⇒ Object

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



2281
2282
2283
2284
2285
2286
2287
# File 'lib/como.rb', line 2281

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

#getScore(*args) ⇒ Object

Get given count.



2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
# File 'lib/como.rb', line 2228

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.



2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
# File 'lib/como.rb', line 2249

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.



2305
2306
2307
# File 'lib/como.rb', line 2305

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

#meh(*args) ⇒ Object

Dont care.



2310
2311
2312
# File 'lib/como.rb', line 2310

def meh( *args )
    true
end

#noneObject

Special condition when no options are given.



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

def none
    @opt.givenCount == 0
end

#one(*args) ⇒ Object

One of list given.



2290
2291
2292
# File 'lib/como.rb', line 2290

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