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

Returns a new instance of RuleCheck.



1981
1982
1983
# File 'lib/como.rb', line 1981

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

Class Method Details

.check(opt, &rule) ⇒ Object



1976
1977
1978
1979
# File 'lib/como.rb', line 1976

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

Instance Method Details

#all(*args) ⇒ Object

All are given.



2059
2060
2061
# File 'lib/como.rb', line 2059

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

#any(*args) ⇒ Object

At least one is given.



2054
2055
2056
# File 'lib/como.rb', line 2054

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

#follow(*args) ⇒ Object

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



2040
2041
2042
2043
2044
2045
2046
# File 'lib/como.rb', line 2040

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

#getScore(*args) ⇒ Object

Get given count.



1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
# File 'lib/como.rb', line 1987

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.



2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
# File 'lib/como.rb', line 2008

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.



2064
2065
2066
# File 'lib/como.rb', line 2064

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

#meh(*args) ⇒ Object

Dont care.



2069
2070
2071
# File 'lib/como.rb', line 2069

def meh( *args )
    true
end

#noneObject

Special condition when no options are given.



2002
2003
2004
# File 'lib/como.rb', line 2002

def none
    @opt.givenCount == 0
end

#one(*args) ⇒ Object

One of list given.



2049
2050
2051
# File 'lib/como.rb', line 2049

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