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.



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

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.



2210
2211
2212
2213
# File 'lib/como.rb', line 2210

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

Instance Method Details

#all(*args) ⇒ Object

All are given.



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

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

#any(*args) ⇒ Object

At least one is given.



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

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

#follow(*args) ⇒ Object

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



2276
2277
2278
2279
2280
2281
2282
# File 'lib/como.rb', line 2276

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

#getScore(*args) ⇒ Object

Get given count.



2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
# File 'lib/como.rb', line 2223

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.



2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
# File 'lib/como.rb', line 2244

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.



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

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

#meh(*args) ⇒ Object

Dont care.



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

def meh( *args )
    true
end

#noneObject

Special condition when no options are given.



2238
2239
2240
# File 'lib/como.rb', line 2238

def none
    @opt.givenCount == 0
end

#one(*args) ⇒ Object

One of list given.



2285
2286
2287
# File 'lib/como.rb', line 2285

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