Sha256: 62650049f8301a26f14765a7aa6a4fc79bd60e3c48857c10ca91e0f35fefa435

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

module BloodContracts
  class BaseContract
    extend Dry::Initializer
    option :action

    class << self
      def rules
        @rules ||= Set.new
      end

      def contract_rule(name, &block)
        define_method("_#{name}", block)
        rules << name
      end
    end

    def call(data)
      return yield(data) if block_given?
      action.call(data)
    end

    def contract
      @contract ||= Hash[
        self.class.rules.map { |name| [name, {check: method("_#{name}")}] }
      ]
    end

    def build_storage(name)
      s = Storage.new(contract_name: name)
      s.input_writer  = method(:input_writer)  if defined? input_writer
      s.output_writer = method(:output_writer) if defined? output_writer
      s.input_serializer  = input_serializer   if defined? input_serializer
      s.output_serializer = output_serializer  if defined? output_serializer
      s.meta_serializer   = meta_serializer    if defined? meta_serializer
      s
    end

    def to_contract_suite(name: self.class.to_s.pathize)
      Suite.new(storage: build_storage(name), contract: contract)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
blood_contracts-0.2.1 lib/blood_contracts/base_contract.rb
blood_contracts-0.2.0 lib/blood_contracts/base_contract.rb