lib/blood_contracts/storage.rb in blood_contracts-0.1.0 vs lib/blood_contracts/storage.rb in blood_contracts-0.2.0

- old
+ new

@@ -1,80 +1,80 @@ +require_relative "./storages/base_backend.rb" +require_relative "./storages/file_backend.rb" +require_relative "./storages/serializer.rb" + module BloodContracts class Storage extend Dry::Initializer + extend Forwardable - # Split date and time, for more comfortable Dirs navigation - option :start_time, default: -> { Time.current.to_s(:number) } - option :custom_path, optional: true - option :root, default: -> { Rails.root.join(path) } - option :stats, default: -> { Hash.new(0) } - option :input_writer, optional: true - option :output_writer, optional: true + Serializer = BloodContracts::Storages::Serializer + FileBackend = BloodContracts::Storages::FileBackend - UNDEFINED_RULE = :__no_tag_match__ + option :contract_name - def default_path - "./tmp/contract_tests/" + DEFAULT_WRITER = -> (options) do + "INPUT:\n#{options.input}\n\n#{'=' * 90}\n\nOUTPUT:\n#{options.output}" end - def path - @path ||= File.join(default_path, custom_path.to_s, start_time) + option :input_writer, + ->(v) { valid_writer(v) }, default: -> { DEFAULT_WRITER } + option :output_writer, + ->(v) { valid_writer(v) }, default: -> { DEFAULT_WRITER } + + option :input_serializer, + ->(v) { Serializer.call(v) }, default: -> { default_serializer } + option :output_serializer, + ->(v) { Serializer.call(v) }, default: -> { default_serializer } + option :meta_serializer, + ->(v) { Serializer.call(v) }, default: -> { default_serializer } + + option :backend, default: -> { FileBackend.new(self, contract_name) } + + def_delegators :@backend, :sample_exists?, + :load_sample, :find_all_samples, + :serialize_sample, :describe_sample, + :suggestion, :unexpected_suggestion + + def self.valid_writer(writer) + return writer if writer.respond_to?(:call) || writer.respond_to?(:to_sym) + raise ArgumentError end - def input_writer=(writer) - fail ArgumentError unless writer.respond_to?(:call) || - writer.respond_to?(:to_sym) - @input_writer = writer + def input_serializer=(serializer) + @input_serializer = Serializer.call(serializer) end - def output_writer=(writer) - fail ArgumentError unless writer.respond_to?(:call) || - writer.respond_to?(:to_sym) - @output_writer = writer + def output_serializer=(serializer) + @output_serializer = Serializer.call(serializer) end - def run_name(tag) - run_name = File.join(root, "#{tag}/#{Time.current.to_s(:number)}") - FileUtils.mkdir_p File.join(root, "#{tag}") - run_name + def meta_serializer=(serializer) + @meta_serializer = Serializer.call(serializer) end - def write(writer, context, input, output) - return default_write_pattern(input, output) unless writer - writer = context.method(writer) if context && writer.respond_to?(:to_sym) - writer.call(input, output) + def input_writer=(writer) + @input_writer = self.class.valid_writer(writer) end - def default_write_pattern(input, output) - [ - "INPUT:", - input, - "\n#{'=' * 90}\n", - "OUTPUT:", - output - ].map(&:to_s).join("\n") + def output_writer=(writer) + @output_writer = self.class.valid_writer(writer) + end + UNDEFINED_RULE = :__no_tag_match__ + EXCEPTION_CAUGHT = :__exception_raised__ + + def default_serializer + { load: Oj.method(:load), dump: Oj.method(:dump) } end # Quick open: `vim -O tmp/contract_tests/<tstamp>/<tag>/<tstamp>.*` - def save_run(input:, output:, rules:, context:) - Array(rules).each do |rule_name| - stats[rule_name] += 1 - run_name = run_name(rule_name) + def store(options:, rules:, context:) + options = Hashie::Mash.new(options) - # TODO: Write to HTML - input_fname = "#{run_name}.input" - output_fname = "#{run_name}.output" - File.open(input_fname, "w+") do |f| - f << write(input_writer, context, input, output).encode( - 'UTF-8', invalid: :replace, undef: :replace, replace: '?' - ) - end - File.open(output_fname, "w+") do |f| - f << write(output_writer, context, input, output).encode( - 'UTF-8', invalid: :replace, undef: :replace, replace: '?' - ) - end + Array(rules).each do |rule_name| + describe_sample(rule_name, options, context) + serialize_sample(rule_name, options, context) end end end end