Sha256: 47ae98f3116704160fea9f35b078cf38f968e60188370403f7b57646c76e5377

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module Fixtury
  class Definition

    attr_reader :name
    attr_reader :schema

    attr_reader :callable
    attr_reader :enhancements

    def initialize(schema: nil, name:, &block)
      @name = name
      @schema = schema
      @callable = block
      @enhancements = []
    end

    def enhance(&block)
      @enhancements << block
    end

    def enhanced?
      @enhancements.any?
    end

    def call(store: nil, execution_context: nil)
      maybe_set_store_context(store: store) do
        value = run_callable(store: store, callable: callable, execution_context: execution_context, value: nil)
        enhancements.each do |e|
          value = run_callable(store: store, callable: e, execution_context: execution_context, value: value)
        end
        value
      end
    end

    protected

    def maybe_set_store_context(store:)
      return yield unless store

      store.with_relative_schema(schema) do
        yield
      end
    end

    def run_callable(store:, callable:, execution_context:, value:)
      execution_context ||= self

      args = []
      args << value unless value.nil?
      if callable.arity > args.length
        raise ArgumentError, "A store store must be provided if the definition expects it." unless store

        args << store
      end

      if args.length.positive?
        execution_context.instance_exec(*args, &callable)
      else
        execution_context.instance_eval(&callable)
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fixtury-0.1.0.rc1 lib/fixtury/definition.rb
fixtury-0.1.0.beta2 lib/fixtury/definition.rb
fixtury-0.1.0.beta lib/fixtury/definition.rb
fixtury-0.1.0.alpha2 lib/fixtury/definition.rb
fixtury-0.1.0.alpha lib/fixtury/definition.rb