Sha256: 9cafee96b88b63898d9da2475e18fb85abe9f07495501df15dc93b5d6573301a

Contents?: true

Size: 1.34 KB

Versions: 6

Compression:

Stored size: 1.34 KB

Contents

module ActiveScaffold
  # Exposes a +configure+ method that accepts a block and runs all contents of the block in two contexts,
  # as opposed to the normal one. First, everything gets evaluated as part of the object including Configurable.
  # Then, as a failover, missing methods and variables are evaluated in the original binding of the block.
  #
  # Note that this only works with "barewords". Constants, instance variables, and class variables are not currently supported in both contexts.
  #
  # May add the given functionality at both the class and instance level. For the former, use +extend+, and for the latter, use +include+.
  module Configurable
    def configure(&configuration_block)
      return unless configuration_block

      @configuration_binding = configuration_block.binding.eval('self')
      ret = instance_exec(self, &configuration_block)
      @configuration_binding = nil
      ret
    end

    def method_missing(name, *args)
      if @configuration_binding&.respond_to?(name, true) # rubocop:disable Lint/RedundantSafeNavigation
        @configuration_binding.send(name, *args)
      else
        super
      end
    end

    def respond_to_missing?(name, include_all = false)
      if defined? @configuration_binding
        @configuration_binding&.respond_to?(name, include_all)
      else
        super
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
active_scaffold-4.0.2 lib/active_scaffold/configurable.rb
active_scaffold-4.0.1 lib/active_scaffold/configurable.rb
active_scaffold-4.0.0 lib/active_scaffold/configurable.rb
active_scaffold-4.0.0.rc3 lib/active_scaffold/configurable.rb
active_scaffold-4.0.0.rc2 lib/active_scaffold/configurable.rb
active_scaffold-4.0.0.rc1 lib/active_scaffold/configurable.rb