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.



1896
1897
1898
# File 'lib/como.rb', line 1896

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

Class Method Details

.check(opt, &rule) ⇒ Object



1891
1892
1893
1894
# File 'lib/como.rb', line 1891

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

Instance Method Details

#all(*args) ⇒ Object

All are given.



1974
1975
1976
# File 'lib/como.rb', line 1974

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

#any(*args) ⇒ Object

At least one is given.



1969
1970
1971
# File 'lib/como.rb', line 1969

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

#follow(*args) ⇒ Object

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



1955
1956
1957
1958
1959
1960
1961
# File 'lib/como.rb', line 1955

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

#getScore(*args) ⇒ Object

Get given count.



1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
# File 'lib/como.rb', line 1902

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.



1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
# File 'lib/como.rb', line 1923

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.



1979
1980
1981
# File 'lib/como.rb', line 1979

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

#meh(*args) ⇒ Object

Dont care.



1984
1985
1986
# File 'lib/como.rb', line 1984

def meh( *args )
    true
end

#noneObject

Special condition when no options are given.



1917
1918
1919
# File 'lib/como.rb', line 1917

def none
    @opt.givenCount == 0
end

#one(*args) ⇒ Object

One of list given.



1964
1965
1966
# File 'lib/como.rb', line 1964

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