Class: Hyphy::Sampler
- Inherits:
-
Object
show all
- Defined in:
- lib/hyphy/sampler.rb
Defined Under Namespace
Classes: UnsupportedORMException
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(opts = {}) ⇒ Sampler
Returns a new instance of Sampler.
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/hyphy/sampler.rb', line 7
def initialize(opts={})
orm = opts[:orm] || :active_record
if orm == :active_record
@orm_adapter = Hyphy::ActiveRecordAdapter
else
raise UnsupportedORMException, 'ORM #{orm} is not supported'
end
@metadata_callbacks = {}
end
|
Instance Attribute Details
Returns the value of attribute metadata_callbacks.
3
4
5
|
# File 'lib/hyphy/sampler.rb', line 3
def metadata_callbacks
@metadata_callbacks
end
|
#orm_adapter ⇒ Object
Returns the value of attribute orm_adapter.
3
4
5
|
# File 'lib/hyphy/sampler.rb', line 3
def orm_adapter
@orm_adapter
end
|
Instance Method Details
37
38
39
|
# File 'lib/hyphy/sampler.rb', line 37
def add_metadata(name, &block)
@metadata_callbacks[name] = block
end
|
#begin ⇒ Object
41
42
43
|
# File 'lib/hyphy/sampler.rb', line 41
def begin
@orm_adapter.subscribe_to_sql_notifications(method(:sample))
end
|
#log_sql(statement, start_time, end_time) ⇒ Object
19
20
21
22
23
24
|
# File 'lib/hyphy/sampler.rb', line 19
def log_sql(statement, start_time, end_time)
Hyphy::SQLStatement.create(:statement => statement,
:start_time => start_time,
:end_time => end_time,
:trace_json => JSON(caller))
end
|
26
27
28
29
30
|
# File 'lib/hyphy/sampler.rb', line 26
def process_metadata(sql_statement)
@metadata_callbacks.each do |key, value_block|
sql_statement.add_metadata(key, value_block.call)
end
end
|
#sample(statement, start_time, end_time) ⇒ Object
32
33
34
35
|
# File 'lib/hyphy/sampler.rb', line 32
def sample(statement, start_time, end_time)
sql_statement = log_sql(statement, start_time, end_time)
process_metadata(sql_statement)
end
|