Class: Hyphy::Sampler

Inherits:
Object
  • 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.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/hyphy/sampler.rb', line 8

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

  @dataset = []
  @metadata_callbacks = {}
end

Instance Attribute Details

#datasetObject

Returns the value of attribute dataset.



3
4
5
# File 'lib/hyphy/sampler.rb', line 3

def dataset
  @dataset
end

#metadata_callbacksObject (readonly)

Returns the value of attribute metadata_callbacks.



4
5
6
# File 'lib/hyphy/sampler.rb', line 4

def 
  @metadata_callbacks
end

#orm_adapterObject (readonly)

Returns the value of attribute orm_adapter.



4
5
6
# File 'lib/hyphy/sampler.rb', line 4

def orm_adapter
  @orm_adapter
end

Instance Method Details

#add_metadata(name, &block) ⇒ Object



43
44
45
# File 'lib/hyphy/sampler.rb', line 43

def (name, &block)
  @metadata_callbacks[name] = block
end

#apply_filter(filter_class, opts = {}) ⇒ Object



61
62
63
64
# File 'lib/hyphy/sampler.rb', line 61

def apply_filter(filter_class, opts={})
  filter = filter_class.new(@dataset, opts)
  filter.filter
end

#beginObject



47
48
49
# File 'lib/hyphy/sampler.rb', line 47

def begin
  @subscriber = @orm_adapter.subscribe_to_sql_notifications(method(:sample))
end

#log_sql(statement, start_time, end_time, orm_adapter) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/hyphy/sampler.rb', line 21

def log_sql(statement, start_time, end_time, orm_adapter)
  sql_statement = Hyphy::SQLStatement.new(:statement => statement,
                                          :start_time => start_time,
                                          :end_time => end_time,
                                          :orm_adapter => orm_adapter,
                                          :trace => caller)
  @dataset << sql_statement
  sql_statement
end

#process_metadata(sql_statement) ⇒ Object



31
32
33
34
35
# File 'lib/hyphy/sampler.rb', line 31

def (sql_statement)
  @metadata_callbacks.each do |key, value_block|
    sql_statement.[key] = value_block.call
  end
end

#profileObject



55
56
57
58
59
# File 'lib/hyphy/sampler.rb', line 55

def profile
  self.begin
  yield
  self.stop
end

#sample(statement, start_time, end_time) ⇒ Object



37
38
39
40
41
# File 'lib/hyphy/sampler.rb', line 37

def sample(statement, start_time, end_time)
  sql_statement = log_sql(statement, start_time, end_time, @orm_adapter)
  (sql_statement)
  sql_statement
end

#stopObject



51
52
53
# File 'lib/hyphy/sampler.rb', line 51

def stop
  @orm_adapter.unsubscribe_to_sql_notifications(@subscriber)
end