class SimpleContracts::Sampler
Constants
- DEFAULT_PERIOD_SIZE
- PATH_TEMPLATE
Public Class Methods
new(contract, period_size: nil)
click to toggle source
# 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
Public Instance Methods
read(path = nil, rule: nil, period: nil)
click to toggle source
to use in interactive Ruby session
# 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)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
capture(rule)
click to toggle source
# File lib/simple_contracts/sampler.rb, line 41 def capture(rule) FileUtils.mkdir_p(File.dirname(sample_path(rule))) File.write(sample_path(rule), @context.serialize) end
current_period()
click to toggle source
# File lib/simple_contracts/sampler.rb, line 46 def current_period Time.now.to_i / (@period_size || 1).to_i end
default_period_size()
click to toggle source
# File lib/simple_contracts/sampler.rb, line 50 def default_period_size Integer(ENV["CONTRACT_#{@contract_name}_SAMPLE_PERIOD_SIZE"] || DEFAULT_PERIOD_SIZE) end
need_sample?(path)
click to toggle source
# File lib/simple_contracts/sampler.rb, line 37 def need_sample?(path) !File.exist?(path) end
root_path()
click to toggle source
# File lib/simple_contracts/sampler.rb, line 54 def root_path ENV["CONTRACT_ROOT_PATH"] || File.join("/tmp", "contracts") end