Sha256: 137cc74229ec6060b2ce58b27a80b435d92b217e630ef26aa4afda1b59e159ae

Contents?: true

Size: 1.16 KB

Versions: 10

Compression:

Stored size: 1.16 KB

Contents

require 'rest-core/middleware'

class RestCore::Defaults
  def self.members; [:defaults]; end
  include RestCore::Middleware

  # the use of singleton_class is making serialization hard!
  # def initialize app, defaults
  #   super
  #   singleton_class.module_eval do
  #     defaults.each{ |(key, value)|
  #       define_method(key) do |env|
  #         if value.respond_to?(:call)
  #           value.call
  #         else
  #           value
  #         end
  #       end
  #     }
  #   end
  # end

  def method_missing msg, *args, &block
    env = args.first
    if env.kind_of?(Hash) && (d = defaults(env)) && d.key?(msg)
      defaults(env)[msg]
    else
      super
    end
  end

  def respond_to? msg
    # since psych would call respond_to? before setting up
    # instance variables when restoring ruby objects, we might
    # be accessing undefined ivars in that case even all ivars are
    # defined in initialize. we can't avoid this because we can't
    # use singleton_class (otherwise we can't serialize this)
    return super unless instance_variable_defined?(:@defaults)
    if (d = defaults({})) && d.key?(msg)
      true
    else
      super
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rest-core-2.0.2 lib/rest-core/middleware/defaults.rb
rest-core-2.0.1 lib/rest-core/middleware/defaults.rb
rest-core-2.0.0 lib/rest-core/middleware/defaults.rb
rest-core-1.0.3 lib/rest-core/middleware/defaults.rb
rest-core-1.0.2 lib/rest-core/middleware/defaults.rb
rest-core-1.0.1 lib/rest-core/middleware/defaults.rb
rest-core-1.0.0 lib/rest-core/middleware/defaults.rb
rest-core-0.8.2 lib/rest-core/middleware/defaults.rb
rest-core-0.8.1 lib/rest-core/middleware/defaults.rb
rest-core-0.8.0 lib/rest-core/middleware/defaults.rb