module Tdc::Generators::DefinitionSourcable

Creates ghost methods for use in generators.

All ghost methods are named 'key'_definition or 'key'_definition_optional where 'key' is a key into the instance_definition hash.

Choose optional if the key may not be present in the instance_definition.

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
line_definition_optional
replenishment_parameters_definition

Public Instance Methods

configure_definition_source(definition) click to toggle source
# File lib/tdc/generators/definition_sourcable.rb, line 33
def configure_definition_source(definition)
  @definition_source = definition
end
source_definition_from(definition) click to toggle source
# File lib/tdc/generators/definition_sourcable.rb, line 24
        def source_definition_from(definition)
          class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
            def definition_source
              @definition_source ||= #{definition}
            end
          RUBY
        end

Private Instance Methods

ghost_definition?(method) click to toggle source
# File lib/tdc/generators/definition_sourcable.rb, line 53
def ghost_definition?(method)
  method.to_s.end_with?("_definition")
end
ghost_optional_definition?(method) click to toggle source
# File lib/tdc/generators/definition_sourcable.rb, line 57
def ghost_optional_definition?(method)
  method.to_s.end_with?("_definition_optional")
end
method_missing(method, *args) click to toggle source
Calls superclass method
# File lib/tdc/generators/definition_sourcable.rb, line 39
def method_missing(method, *args)
  if ghost_definition?(method)
    definition_source.fetch(method.to_s.gsub(/_definition$/, ""))
  elsif ghost_optional_definition?(method)
    definition_source[method.to_s.gsub(/_definition_optional$/, "")]
  else
    super
  end
end
respond_to_missing?(method, include_all = false) click to toggle source
Calls superclass method
# File lib/tdc/generators/definition_sourcable.rb, line 49
def respond_to_missing?(method, include_all = false)
  ghost_definition?(method) || ghost_optional_definition?(method) ? true : super
end