Sha256: bc6e0e038bf6dc54c89d38e3ba6c162f408d17e2c3d8b9769f10de8259e28dd4

Contents?: true

Size: 1.3 KB

Versions: 17

Compression:

Stored size: 1.3 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 = eval("self", configuration_block.binding)
      ret = instance_exec self, &configuration_block
      @configuration_binding = nil
      return ret
    end

    # this method will surely need tweaking. for example, i'm not sure if it should call super before or after it tries to eval with the binding.
    def method_missing(name, *args)
      begin
        super
      rescue NoMethodError, NameError
        if @configuration_binding.nil?
          raise $!
        else
          @configuration_binding.send(name, *args)
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
active_scaffold-3.4.17 lib/active_scaffold/configurable.rb
active_scaffold-3.4.16 lib/active_scaffold/configurable.rb
active_scaffold-3.4.14 lib/active_scaffold/configurable.rb
active_scaffold-3.4.13 lib/active_scaffold/configurable.rb
active_scaffold-3.4.12 lib/active_scaffold/configurable.rb
active_scaffold-3.4.11 lib/active_scaffold/configurable.rb
active_scaffold-3.4.10 lib/active_scaffold/configurable.rb
active_scaffold-3.4.9 lib/active_scaffold/configurable.rb
active_scaffold-3.4.8 lib/active_scaffold/configurable.rb
active_scaffold-3.4.7 lib/active_scaffold/configurable.rb
active_scaffold-3.4.5 lib/active_scaffold/configurable.rb
active_scaffold-3.4.4 lib/active_scaffold/configurable.rb
active_scaffold-3.4.3 lib/active_scaffold/configurable.rb
active_scaffold-3.4.2 lib/active_scaffold/configurable.rb
active_scaffold-3.4.1 lib/active_scaffold/configurable.rb
active_scaffold-3.4.0.1 lib/active_scaffold/configurable.rb
active_scaffold-3.4.0 lib/active_scaffold/configurable.rb