Sha256: 507a35e983a2961b9d3b1c07af9317c10dd798d5166920101b6601b3bc00445a

Contents?: true

Size: 1.07 KB

Versions: 6

Compression:

Stored size: 1.07 KB

Contents

module Tdc
  module Generators
    #
    # Abstract class for defining generators that define a single model instance.
    #
    # See also StandardGenerator.
    #
    class SingularGenerator < Tdc::Generators::ConfigurableGenerator
      def with_definition(additional_definitions)
        @additional_definitions = additional_definitions.stringify_keys.reject { |_, v| v == :missing_definition }

        self
      end

      def generate
        configure_instance_definition(singular_instance_definition.merge(additional_definitions))

        run_resolvers_and_generate_instance
      end

      private

      def additional_definitions
        @additional_definitions || {}
      end

      def singular_instance_definition
        all_instance_definitions = instance_definitions

        if all_instance_definitions.many?
          raise Tdc::FatalError, "A singular generator only generates a single model instance"
        end

        # Delete the tag so that the models do not need to filter it out.
        all_instance_definitions.first.except("tag")
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
tdc-0.3.4 lib/tdc/generators/singular_generator.rb
tdc-0.3.3 lib/tdc/generators/singular_generator.rb
tdc-0.3.2 lib/tdc/generators/singular_generator.rb
tdc-0.3.1 lib/tdc/generators/singular_generator.rb
tdc-0.3.0 lib/tdc/generators/singular_generator.rb
tdc-0.2.4 lib/tdc/generators/singular_generator.rb