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.



2094
2095
2096
# File 'lib/como.rb', line 2094

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.



2087
2088
2089
2090
# File 'lib/como.rb', line 2087

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

Instance Method Details

#all(*args) ⇒ Object

All are given.



2172
2173
2174
# File 'lib/como.rb', line 2172

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

#any(*args) ⇒ Object

At least one is given.



2167
2168
2169
# File 'lib/como.rb', line 2167

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

#follow(*args) ⇒ Object

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



2153
2154
2155
2156
2157
2158
2159
# File 'lib/como.rb', line 2153

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

#getScore(*args) ⇒ Object

Get given count.



2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
# File 'lib/como.rb', line 2100

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.



2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
# File 'lib/como.rb', line 2121

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.



2177
2178
2179
# File 'lib/como.rb', line 2177

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

#meh(*args) ⇒ Object

Dont care.



2182
2183
2184
# File 'lib/como.rb', line 2182

def meh( *args )
    true
end

#noneObject

Special condition when no options are given.



2115
2116
2117
# File 'lib/como.rb', line 2115

def none
    @opt.givenCount == 0
end

#one(*args) ⇒ Object

One of list given.



2162
2163
2164
# File 'lib/como.rb', line 2162

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