Class: TFS::QueryEngine

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ClassHelpers
Defined in:
lib/tfs/query_engine.rb

Constant Summary collapse

VALID_CLASSES =

Classes we currently support queries on

[
  TFS::Builds,
  TFS::Changesets,
  TFS::Projects,
  TFS::WorkItems,
  TFS::Changes
]
DEFAULT_LIMIT =

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

50

Constants included from ClassHelpers

ClassHelpers::SPECIAL_CASES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassHelpers

#base_class, #method_name_from_class, #odata_class_from_method_name

Constructor Details

#initialize(for_class, connection, params = "") ⇒ QueryEngine

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')


35
36
37
38
39
40
# File 'lib/tfs/query_engine.rb', line 35

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



88
89
90
91
92
# File 'lib/tfs/query_engine.rb', line 88

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

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/tfs/query_engine.rb', line 14

def type
  @type
end

Instance Method Details

#countObject

Return a count of records rather than records



67
68
69
70
# File 'lib/tfs/query_engine.rb', line 67

def count
  @native_query = @native_query.count
  self
end

#limit(count) ⇒ Object

Limit records returned



49
50
51
52
# File 'lib/tfs/query_engine.rb', line 49

def limit(count)
  @native_query = @native_query.top(count)
  self
end

#order_by(query) ⇒ Object

Ordering. Must be a valid field



55
56
57
58
# File 'lib/tfs/query_engine.rb', line 55

def order_by(query)
  @native_query = @native_query.order_by(query)
  self
end

#page(start) ⇒ Object

For pagination. Skips supplied number of records



73
74
75
76
# File 'lib/tfs/query_engine.rb', line 73

def page(start)
  @native_query = @native_query.skip(start)
  self
end

#rawObject

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



44
45
46
# File 'lib/tfs/query_engine.rb', line 44

def raw
  @native_query
end

#runObject

Required to execute the query



79
80
81
# File 'lib/tfs/query_engine.rb', line 79

def run
  @connection.execute
end

#to_queryObject

Returns the actual query string (does not run query)



84
85
86
# File 'lib/tfs/query_engine.rb', line 84

def to_query
  @native_query.query
end

#where(filter) ⇒ Object

Filtering, use odata query syntax



61
62
63
64
# File 'lib/tfs/query_engine.rb', line 61

def where(filter)
  @native_query = @native_query.filter(filter)
  self
end