Class: Feta::Query

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/feta/query.rb

Overview

A query to the keeper server

Instance Method Summary collapse

Constructor Details

#initializeQuery

Returns a new instance of Query



34
35
36
37
38
39
# File 'lib/feta/query.rb', line 34

def initialize
  @products = []
  @actor = nil
  @role = nil
  @feature_id = nil
end

Instance Method Details

#each {|Feta::Feature| ... } ⇒ Object

Note:

Requires Feta.client to be set

Iterates through the result of the current query.

Yields:



92
93
94
95
96
# File 'lib/feta/query.rb', line 92

def each
  ret = Feta.client.search(self)
  return ret.each if not block_given?
  ret.each { |feature| yield feature }
end

#only_actor(actor) ⇒ Object

Queries only features where this actor is present

Parameters:

  • actor (String)

    Actor email



43
44
45
46
# File 'lib/feta/query.rb', line 43

def only_actor(actor)
  @actor = actor
  self
end

#only_product(product) ⇒ Object

Queries only features for these product

Parameters:

  • product (String)

    Product name



57
58
59
# File 'lib/feta/query.rb', line 57

def only_product(product)
  only_products([product])
end

#only_products(products) ⇒ Object

Queries only features for these products

Parameters:

  • products (Array<String>)

    Product name list



63
64
65
66
67
# File 'lib/feta/query.rb', line 63

def only_products(products)
  @products = []
  @products = products.flatten
  self
end

#to_xqueryString

Converts the query to an xpath expression

Returns:

  • (String)

    xpath expression representing the query



77
78
79
80
81
82
83
84
85
# File 'lib/feta/query.rb', line 77

def to_xquery
  id_query = @feature_id ? "@k:id=#{@feature_id}" : ""
  role_query = @role ? " and role='#{@role}'" : ""
  actor_query = @actor ? "actor[person/email='#{@actor}'#{role_query}]" : ""
  prods = @products.collect {|p| "product/name='#{p}'"}.join(" or ")
  product_query = (!@products.empty?) ? "productcontext[#{prods}]" : ""
  conditions = [id_query, actor_query, product_query].reject{ |x| x.empty? }.join(' and ')
  query = "/feature[#{conditions}]"
end

#with_id(id) ⇒ Object



69
70
71
72
# File 'lib/feta/query.rb', line 69

def with_id(id)
  @feature_id = id
  self
end

#with_role(role) ⇒ Object

Restrict the actor's role to role

Parameters:

  • role (String)

    Actor's role



50
51
52
53
# File 'lib/feta/query.rb', line 50

def with_role(role)
  @role = role
  self
end