Class: SimpleContracts::Sampler

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_contracts/sampler.rb

Constant Summary collapse

PATH_TEMPLATE =
"%<contract_name>s/%<rule>s/%<period>i.dump"
DEFAULT_PERIOD_SIZE =

every hour

60 * 60

Instance Method Summary collapse

Constructor Details

#initialize(contract, period_size: nil) ⇒ Sampler

Returns a new instance of Sampler.



8
9
10
11
12
# File 'lib/simple_contracts/sampler.rb', line 8

def initialize(contract, period_size: nil)
  @context = contract
  @contract_name = contract.contract_name
  @period_size = period_size || default_period_size
end

Instance Method Details

#read(path = nil, rule: nil, period: nil) ⇒ Object

to use in interactive Ruby session

Raises:

  • (ArgumentError)


29
30
31
32
33
# File 'lib/simple_contracts/sampler.rb', line 29

def read(path = nil, rule: nil, period: nil)
  path ||= sample_path(rule, period)
  raise(ArgumentError, "Sample path should be defined") unless path
  @context.deserialize(File.read(path))
end

#sample!(rule) ⇒ Object



14
15
16
17
18
19
# File 'lib/simple_contracts/sampler.rb', line 14

def sample!(rule)
  path = sample_path(rule)
  return unless need_sample?(path)
  capture(rule)
  path
end

#sample_path(rule, period = current_period) ⇒ Object



21
22
23
24
25
26
# File 'lib/simple_contracts/sampler.rb', line 21

def sample_path(rule, period = current_period)
  File.join(
    root_path,
    PATH_TEMPLATE % {contract_name: @contract_name, rule: rule, period: period}
  )
end