Class: BloodContracts::Storages::FileBackend

Inherits:
BaseBackend
  • Object
show all
Defined in:
lib/blood_contracts/storages/file_backend.rb

Instance Method Summary collapse

Methods inherited from BaseBackend

#load_sample, #serialize_sample

Instance Method Details

#default_pathObject



14
15
16
# File 'lib/blood_contracts/storages/file_backend.rb', line 14

def default_path
  "./tmp/contract_tests/"
end

#describe_sample(tag, options, context) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/blood_contracts/storages/file_backend.rb', line 86

def describe_sample(tag, options, context)
  FileUtils.mkdir_p File.join(root, tag.to_s)

  reset_timestamp!
  name = sample_name(tag)
  File.open("#{name}.input", "w+") do |f|
    f << write(input_writer, context, options)
  end
  File.open("#{name}.output", "w+") do |f|
    f << write(output_writer, context, options)
  end
end

#find_all_samples(sample_name) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/blood_contracts/storages/file_backend.rb', line 49

def find_all_samples(sample_name)
  run, tag, sample = parse_sample_name(sample_name)
  run_path = path(run_name: run)
  files = Dir.glob("#{run_path}/#{tag.to_s}/#{sample}*")
  files.select { |f| f.end_with?(".output") }
       .map { |f| f.chomp(".output") }
end

#load_sample_chunk(dump_type, sample_name) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/blood_contracts/storages/file_backend.rb', line 71

def load_sample_chunk(dump_type, sample_name)
  run, tag, sample = parse_sample_name(sample_name)
  name = sample_name(tag, run_path: path(run_name: run), sample: sample)
  send("#{dump_type}_serializer")[:load].call(
    File.read("#{name}.#{dump_type}.dump")
  )
end

#parse_sample_name(sample_name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/blood_contracts/storages/file_backend.rb', line 26

def parse_sample_name(sample_name)
  path_items = sample_name.split("/")
  sample = path_items.pop
  tag = path_items.pop
  path_str = path_items.join("/")
  run_n_example_str = path_str.sub(default_path, '')
  if run_n_example_str.end_with?('*')
    [
      run_n_example_str.chomp("*"),
      tag,
      sample
    ]
  elsif run_n_example_str.end_with?(example_name)
    [
      run_n_example_str.chomp(example_name),
      tag,
      sample
    ]
  else
    %w(__no_match__ __no_match__ __no_match__)
  end
end

#path(run_name: name) ⇒ Object



57
58
59
# File 'lib/blood_contracts/storages/file_backend.rb', line 57

def path(run_name: name)
  File.join(default_path, run_name, example_name.to_s)
end

#reset_timestamp!Object



22
23
24
# File 'lib/blood_contracts/storages/file_backend.rb', line 22

def reset_timestamp!
  @timestamp = nil
end

#sample_exists?(sample_name) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
# File 'lib/blood_contracts/storages/file_backend.rb', line 65

def sample_exists?(sample_name)
  run, tag, sample = parse_sample_name(sample_name)
  name = sample_name(tag, run_path: path(run_name: run), sample: sample)
  File.exist?("#{name}.input")
end

#sample_name(tag, run_path: root, sample: timestamp) ⇒ Object



61
62
63
# File 'lib/blood_contracts/storages/file_backend.rb', line 61

def sample_name(tag, run_path: root, sample: timestamp)
  File.join(run_path, tag.to_s, sample)
end

#serialize_sample_chunk(type, tag, options, context) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/blood_contracts/storages/file_backend.rb', line 99

def serialize_sample_chunk(type, tag, options, context)
  return unless (dump_proc = send("#{type}_serializer")[:dump])
  name, data = sample_name(tag), options.send(type)
  File.open("#{name}.#{type}.dump", "w+") do |f|
    f << write(dump_proc, context, data)
  end
end

#suggestionObject



6
7
8
# File 'lib/blood_contracts/storages/file_backend.rb', line 6

def suggestion
  "#{path}/*/*"
end

#timestampObject



18
19
20
# File 'lib/blood_contracts/storages/file_backend.rb', line 18

def timestamp
  @timestamp ||= Time.current.to_s(:usec)[8..-3]
end

#unexpected_suggestionObject



10
11
12
# File 'lib/blood_contracts/storages/file_backend.rb', line 10

def unexpected_suggestion
  "#{path}/#{Storage::UNDEFINED_RULE}/*"
end

#write(writer, cntxt, options) ⇒ Object



79
80
81
82
83
84
# File 'lib/blood_contracts/storages/file_backend.rb', line 79

def write(writer, cntxt, options)
  writer = cntxt.method(writer) if cntxt && writer.respond_to?(:to_sym)
  writer.call(options).encode(
    "UTF-8", invalid: :replace, undef: :replace, replace: "?",
  )
end