Class: Oculus::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/oculus/query.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Query

Returns a new instance of Query.



14
15
16
17
18
# File 'lib/oculus/query.rb', line 14

def initialize(attributes = {})
  attributes.each do |attr, value|
    send("#{attr}=", value)
  end
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



5
6
7
# File 'lib/oculus/query.rb', line 5

def author
  @author
end

#errorObject

Returns the value of attribute error.



8
9
10
# File 'lib/oculus/query.rb', line 8

def error
  @error
end

#finished_atObject

Returns the value of attribute finished_at.



10
11
12
# File 'lib/oculus/query.rb', line 10

def finished_at
  @finished_at
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/oculus/query.rb', line 3

def id
  @id
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/oculus/query.rb', line 4

def name
  @name
end

#queryObject

Returns the value of attribute query.



6
7
8
# File 'lib/oculus/query.rb', line 6

def query
  @query
end

#resultsObject

Returns the value of attribute results.



7
8
9
# File 'lib/oculus/query.rb', line 7

def results
  @results
end

#starredObject

Returns the value of attribute starred.



11
12
13
# File 'lib/oculus/query.rb', line 11

def starred
  @starred
end

#started_atObject

Returns the value of attribute started_at.



9
10
11
# File 'lib/oculus/query.rb', line 9

def started_at
  @started_at
end

#thread_idObject

Returns the value of attribute thread_id.



12
13
14
# File 'lib/oculus/query.rb', line 12

def thread_id
  @thread_id
end

Class Method Details

.create(attributes) ⇒ Object



70
71
72
73
74
# File 'lib/oculus/query.rb', line 70

def create(attributes)
  query = new(attributes)
  query.save
  query
end

.find(id) ⇒ Object



76
77
78
# File 'lib/oculus/query.rb', line 76

def find(id)
  new(Oculus.data_store.load_query(id))
end

Instance Method Details

#attributesObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/oculus/query.rb', line 20

def attributes
  attrs = {
    :name        => name,
    :author      => author,
    :query       => query,
    :started_at  => started_at,
    :finished_at => finished_at,
    :starred     => starred,
    :thread_id   => thread_id
  }
  attrs[:error] = error if error
  attrs
end

#complete?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/oculus/query.rb', line 53

def complete?
  !!finished_at
end

#execute(connection) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/oculus/query.rb', line 34

def execute(connection)
  self.started_at = Time.now
  self.thread_id  = connection.thread_id
  self.save
  results = connection.execute(query)
rescue Connection::Error => e
  error = e.message
ensure
  reload
  self.results = results if results
  self.error   = error   if error
  self.finished_at = Time.now
  self.save
end

#saveObject



49
50
51
# File 'lib/oculus/query.rb', line 49

def save
  Oculus.data_store.save_query(self)
end

#succeeded?Boolean

Returns:

  • (Boolean)


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

def succeeded?
  complete? && !error
end

#to_csvObject



61
62
63
64
65
66
67
# File 'lib/oculus/query.rb', line 61

def to_csv
  CSV.generate do |csv|
    results.each do |row|
      csv << row
    end
  end
end