Class: Autoproj::QueryBase

Inherits:
Object
  • Object
show all
Defined in:
lib/autoproj/query_base.rb

Overview

Base implementation for the query classes SourcePackageQuery and OSPackageQuery

Direct Known Subclasses

OSPackageQuery, SourcePackageQuery

Defined Under Namespace

Classes: All, And, Or

Constant Summary collapse

EXACT =

Match priorities

4
PARTIAL =
3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields, value, partial) ⇒ QueryBase

Returns a new instance of QueryBase.

[View source]

35
36
37
38
39
40
# File 'lib/autoproj/query_base.rb', line 35

def initialize(fields, value, partial)
    @fields = fields
    @value  = value
    @value_rx = Regexp.new(Regexp.quote(value), true)
    @partial = partial
end

Instance Attribute Details

#fieldsObject (readonly)

The call chain to be matched (i.e. autobuild.name becomes

‘autobuild’, ‘name’

10
11
12
# File 'lib/autoproj/query_base.rb', line 10

def fields
  @fields
end

#valueObject (readonly)

The expected value


12
13
14
# File 'lib/autoproj/query_base.rb', line 12

def value
  @value
end

Class Method Details

.allAll

Get a query that matches anything

Returns:

[View source]

31
32
33
# File 'lib/autoproj/query_base.rb', line 31

def self.all
    All.new
end

.parse(str, allowed_fields: [], default_fields: Hash.new) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse a single field in a query (i.e. a FIELDVALUE string)

This is NOT meant to be used directly. Subclasses are supposed to redefine .parse to create the relevant match object.

[View source]

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/autoproj/query_base.rb', line 66

def self.parse(str, allowed_fields: [], default_fields: Hash.new)
    if (parsed = /[=~]/.match(str))
        field = parsed.pre_match
        value = parsed.post_match
        partial = (parsed[0] == "~")
    else
        raise ArgumentError, "invalid query string '#{str}', expected FIELD and VALUE separated by either = or ~"
    end

    field = default_fields[field] || field

    # Validate the query key
    unless allowed_fields.include?(field)
        raise ArgumentError, "'#{field}' is not a known query key"
    end

    fields = field.split(".")
    [fields, value, partial]
end

.parse_query(query, *args) ⇒ Object

Parse a complete query

[View source]

87
88
89
90
91
92
93
94
95
96
97
# File 'lib/autoproj/query_base.rb', line 87

def self.parse_query(query, *args)
    query = query.split(":")
    query = query.map do |str|
        parse(str, *args)
    end
    if query.size == 1
        query.first
    else
        And.new(query)
    end
end

Instance Method Details

#match(pkg) ⇒ Boolean

Checks if a package matches against the query

If the package matches, the returned value can be one of:

EXACT

this is an exact match

PARTIAL

the expected value can be found in the package field. The match is done in a case-insensitive way

If partial? is not set (i.e. if FIELD=VALUE was used), then only EXACT or false can be returned.

Parameters:

  • pkg (String)

    the osdep package name

Returns:

  • (Boolean)

    true if it does, false otherwise

Raises:

  • (NotImplementedError)
[View source]

56
57
58
# File 'lib/autoproj/query_base.rb', line 56

def match(pkg)
    raise NotImplementedError
end