Class: RestCore::Defaults

Inherits:
Object
  • Object
show all
Includes:
Middleware
Defined in:
lib/rest-core/middleware/defaults.rb

Constant Summary

Constant Summary

Constants included from Middleware

Middleware::UNRESERVED

Constants included from RestCore

ASYNC, CLIENT, DRY, FAIL, HIJACK, LOG, PROMISE, REQUEST_HEADERS, REQUEST_METHOD, REQUEST_PATH, REQUEST_PAYLOAD, REQUEST_QUERY, REQUEST_URI, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_KEY, RESPONSE_SOCKET, RESPONSE_STATUS, TIMER, Universal, VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Middleware

#call, contain_binary?, #error_callback, escape, #fail, #give_promise, #id, included, #log, merge_hash, percent_encode, request_uri, #run, string_keys

Methods included from RestCore

eagerload, id

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(msg, *args, &block) ⇒ Object

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



24
25
26
27
28
29
30
31
# File 'lib/rest-core/middleware/defaults.rb', line 24

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

Class Method Details

.membersObject



5
# File 'lib/rest-core/middleware/defaults.rb', line 5

def self.members; [:defaults]; end

Instance Method Details

#respond_to_missing?(msg, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rest-core/middleware/defaults.rb', line 33

def respond_to_missing? msg, include_private=false
  # 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