Module: Browser::DelegateNative
- Included in:
- Element
- Defined in:
- lib/browser/delegate_native.rb
Instance Method Summary collapse
-
#initialize(native) ⇒ Object
Provides a default initializer.
-
#method_missing(message, *args, &block) ⇒ Object
Fall back to native properties.
- #property_for_message(message) ⇒ Object
- #respond_to_missing?(message, include_all) ⇒ Boolean
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(message, *args, &block) ⇒ Object
Fall back to native properties. If the message sent to this element is not recognized, it checks to see if it is a property of the native element. It also checks for variations of the message name, such as:
:supported? => [:supported, :isSupported]
If a property with the specified message name is found and it is a function, that function is invoked with ‘args`. Otherwise, the property is returned as is.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/browser/delegate_native.rb', line 18 def method_missing , *args, &block property_name = () property = `#@native[#{property_name}]` # translate setting a property if .end_with? '=' return `#@native[#{property_name}] = args[0]` end # If the native element doesn't have this property, bubble it up super unless `#{property_name} in #@native` if `property === false` return false elsif `typeof(property) === 'number' && isNaN(property)` return nil else property = `property == null ? nil : property` end # If it's a method, call it. Otherwise, return it. if `typeof(property) === 'function'` `property.apply(#@native, args)` else property end end |
Instance Method Details
#initialize(native) ⇒ Object
Provides a default initializer. This should be overridden in all but the simplest cases.
5 6 7 |
# File 'lib/browser/delegate_native.rb', line 5 def initialize native @native = native end |
#property_for_message(message) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/browser/delegate_native.rb', line 53 def = .gsub(/_\w/) { |match| `match[1]`.upcase } .sub(/=$/, '') # translate `supported?` to `supported` or `isSupported` if .end_with? '?' = .chop property_type = `typeof(#@native[camel_cased_message])` if property_type == 'undefined' = "is#{[0].upcase}#{[1..-1]}" end end end |
#respond_to_missing?(message, include_all) ⇒ Boolean
46 47 48 49 50 51 |
# File 'lib/browser/delegate_native.rb', line 46 def respond_to_missing? , include_all return true if .end_with? '=' return true if () false end |