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::Branches,
  TFS::Builds,
  TFS::Changesets,
  TFS::Projects,
  TFS::WorkItems,
  TFS::Changes,
  TFS::AreaPaths,
  TFS::ChangesetMerges
]
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')


40
41
42
43
44
45
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



93
94
95
96
97
# 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

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



16
17
18
# File 'lib/tfs/query_engine.rb', line 16

def type
  @type
end

Instance Method Details

#countObject

Return a count of records rather than records



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

def count
  @native_query = @native_query.count
  self
end

#limit(count) ⇒ Object

Limit records returned



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

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

#order_by(query) ⇒ Object

Ordering. Must be a valid field



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

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

#page(start) ⇒ Object

For pagination. Skips supplied number of records



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

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



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

def raw
  @native_query
end

#runObject

Required to execute the query



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

def run
  @connection.execute
end

#to_queryObject

Returns the actual query string (does not run query)



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

def to_query
  @native_query.query
end

#where(filter) ⇒ Object

Filtering, use odata query syntax



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

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