Class: Oculus::Query
- Inherits:
-
Object
- Object
- Oculus::Query
- Defined in:
- lib/oculus/query.rb
Instance Attribute Summary collapse
-
#author ⇒ Object
Returns the value of attribute author.
-
#error ⇒ Object
Returns the value of attribute error.
-
#finished_at ⇒ Object
Returns the value of attribute finished_at.
-
#id ⇒ Object
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#query ⇒ Object
Returns the value of attribute query.
-
#results ⇒ Object
Returns the value of attribute results.
-
#starred ⇒ Object
Returns the value of attribute starred.
-
#started_at ⇒ Object
Returns the value of attribute started_at.
-
#thread_id ⇒ Object
Returns the value of attribute thread_id.
Class Method Summary collapse
Instance Method Summary collapse
- #attributes ⇒ Object
- #complete? ⇒ Boolean
- #execute(connection) ⇒ Object
-
#initialize(attributes = {}) ⇒ Query
constructor
A new instance of Query.
- #save ⇒ Object
- #succeeded? ⇒ Boolean
- #to_csv ⇒ Object
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
#author ⇒ Object
Returns the value of attribute author.
5 6 7 |
# File 'lib/oculus/query.rb', line 5 def @author end |
#error ⇒ Object
Returns the value of attribute error.
8 9 10 |
# File 'lib/oculus/query.rb', line 8 def error @error end |
#finished_at ⇒ Object
Returns the value of attribute finished_at.
10 11 12 |
# File 'lib/oculus/query.rb', line 10 def finished_at @finished_at end |
#id ⇒ Object
Returns the value of attribute id.
3 4 5 |
# File 'lib/oculus/query.rb', line 3 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/oculus/query.rb', line 4 def name @name end |
#query ⇒ Object
Returns the value of attribute query.
6 7 8 |
# File 'lib/oculus/query.rb', line 6 def query @query end |
#results ⇒ Object
Returns the value of attribute results.
7 8 9 |
# File 'lib/oculus/query.rb', line 7 def results @results end |
#starred ⇒ Object
Returns the value of attribute starred.
11 12 13 |
# File 'lib/oculus/query.rb', line 11 def starred @starred end |
#started_at ⇒ Object
Returns the value of attribute started_at.
9 10 11 |
# File 'lib/oculus/query.rb', line 9 def started_at @started_at end |
#thread_id ⇒ Object
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
#attributes ⇒ Object
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 => , :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
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. ensure reload self.results = results if results self.error = error if error self.finished_at = Time.now self.save end |
#save ⇒ Object
49 50 51 |
# File 'lib/oculus/query.rb', line 49 def save Oculus.data_store.save_query(self) end |
#succeeded? ⇒ Boolean
57 58 59 |
# File 'lib/oculus/query.rb', line 57 def succeeded? complete? && !error end |
#to_csv ⇒ Object
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 |