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.



16
17
18
19
20
# File 'lib/oculus/query.rb', line 16

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

Instance Attribute Details

#authorObject

Returns the value of attribute author.



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

def author
  @author
end

#errorObject

Returns the value of attribute error.



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

def error
  @error
end

#finished_atObject

Returns the value of attribute finished_at.



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

def finished_at
  @finished_at
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#queryObject

Returns the value of attribute query.



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

def query
  @query
end

#resultsObject

Returns the value of attribute results.



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

def results
  @results
end

#starredObject

Returns the value of attribute starred.



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

def starred
  @starred
end

#started_atObject

Returns the value of attribute started_at.



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

def started_at
  @started_at
end

#thread_idObject

Returns the value of attribute thread_id.



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

def thread_id
  @thread_id
end

Class Method Details

.create(attributes) ⇒ Object



72
73
74
75
76
# File 'lib/oculus/query.rb', line 72

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

.find(id) ⇒ Object



78
79
80
# File 'lib/oculus/query.rb', line 78

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

Instance Method Details

#attributesObject



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

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)


55
56
57
# File 'lib/oculus/query.rb', line 55

def complete?
  !!finished_at
end

#execute(connection) ⇒ Object



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

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



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

def save
  Oculus.data_store.save_query(self)
end

#succeeded?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/oculus/query.rb', line 59

def succeeded?
  complete? && !error
end

#to_csvObject



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

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