class TFS::QueryEngine

Constants

DEFAULT_LIMIT

Default pagination ‘#all` limit for all other actions, it limits by default to 20 (API level limit)

VALID_CLASSES

Classes we currently support queries on

Attributes

type[R]

Public Class Methods

new(for_class, connection, params="") click to toggle source

A new query requres a base class (of one of the above ‘VALID_CLASSES`), a connection (from `client.connection`) and can take additional selection parameters

TFS::QueryEngine(TFS::Projects, TFS.client.connection, 'project-name')
# File lib/tfs/query_engine.rb, line 40
def initialize(for_class, connection, params="")
  check_type(for_class)
  @type, @connection = for_class, connection

  @native_query = @connection.send(base_class(for_class), normalize(params))
end

Public Instance Methods

count() click to toggle source

Return a count of records rather than records

# File lib/tfs/query_engine.rb, line 72
def count
  @native_query = @native_query.count
  self
end
limit(count) click to toggle source

Limit records returned

# File lib/tfs/query_engine.rb, line 54
def limit(count)
  @native_query = @native_query.top(count)
  self
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/tfs/query_engine.rb, line 93
def method_missing(method_name, *args, &block)
  return super unless @type.send "#{method_name}?".to_sym
  @native_query.navigate(odata_class_from_method_name(method_name))
  self
end
order_by(query) click to toggle source

Ordering. Must be a valid field

# File lib/tfs/query_engine.rb, line 60
def order_by(query)
  @native_query = @native_query.order_by(query)
  self
end
page(start) click to toggle source

For pagination. Skips supplied number of records

# File lib/tfs/query_engine.rb, line 78
def page(start)
  @native_query = @native_query.skip(start)
  self
end
raw() click to toggle source

Returns the underlying ‘OData::QueryBuilder` object in case you want to work directly against the odata adapter

# File lib/tfs/query_engine.rb, line 49
def raw
  @native_query
end
run() click to toggle source

Required to execute the query

# File lib/tfs/query_engine.rb, line 84
def run
  @connection.execute
end
to_query() click to toggle source

Returns the actual query string (does not run query)

# File lib/tfs/query_engine.rb, line 89
def to_query
  @native_query.query
end
where(filter) click to toggle source

Filtering, use odata query syntax

# File lib/tfs/query_engine.rb, line 66
def where(filter)
  @native_query = @native_query.filter(filter)
  self
end

Private Instance Methods

check_type(for_class) click to toggle source
# File lib/tfs/query_engine.rb, line 100
def check_type(for_class)
  raise TypeError, "#{for_class.to_s} is not a valid query type." unless VALID_CLASSES.include? for_class
end
format_parameter(param) click to toggle source
# File lib/tfs/query_engine.rb, line 118
def format_parameter(param)
  "'#{param}'"
end
normalize(params) click to toggle source
# File lib/tfs/query_engine.rb, line 104
def normalize(params)
  args = params.first
  case args
  when String
    format_parameter(args)
  when Array
    args.map {|item| format_parameter(item) }.join(",")
  when Hash
    args.map {|k,v| "#{k.to_s.capitalize}=#{format_parameter(v)}"}.join(',')
  else
    args
  end
end