Sha256: 956c4c09bed0304be9f0f0284b5ec6b9c882d6f162479edb643d1d9ffc8ad6c4

Contents?: true

Size: 1.46 KB

Versions: 7

Compression:

Stored size: 1.46 KB

Contents

module Tdc
  module Generators
    #
    # Creates ghost methods for use in generators.
    #
    # All ghost methods are named 'key'_definition where 'key' is a key into the instance_definition hash.
    #
    # Example:
    #
    # If an instance definition had "line" and "replenishment_parameters" keys then the following
    # ghost methods could be used to refer to the value associated with those keys:
    #
    #  line_definition
    #  replenishment_parameters_definition
    #
    module DefinitionSourcable
      extend ActiveSupport::Concern

      class_methods do
        def source_definition_from(definition)
          class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
            def definition_source
              @definition_source ||= #{definition}
            end
          RUBY
        end
      end

      def configure_definition_source(definition)
        @definition_source = definition
      end

      private

      def method_missing(method, *args)
        key = transform_method_to_definition_source_key(method)

        definition_source&.key?(key) ? definition_source.fetch(key) : super
      end

      def respond_to_missing?(method, include_all = false) # rubocop:disable Style/OptionalBooleanParameter
        key = transform_method_to_definition_source_key(method)

        definition_source&.key?(key) ? true : super
      end

      def transform_method_to_definition_source_key(method)
        method.to_s.gsub(/_definition$/, "")
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
tdc-0.4.2 lib/tdc/generators/definition_sourcable.rb
tdc-0.4.1 lib/tdc/generators/definition_sourcable.rb
tdc-0.4.0 lib/tdc/generators/definition_sourcable.rb
tdc-0.3.9 lib/tdc/generators/definition_sourcable.rb
tdc-0.3.8 lib/tdc/generators/definition_sourcable.rb
tdc-0.3.7 lib/tdc/generators/definition_sourcable.rb
tdc-0.3.6.1 lib/tdc/generators/definition_sourcable.rb