Class: Dommy::Notification
- Inherits:
-
Object
- Object
- Dommy::Notification
- Includes:
- EventTarget
- Defined in:
- lib/dommy/notification.rb
Overview
‘Notification` polyfill. Real browsers prompt the user; dommy exposes the permission state as a class-level slot tests can toggle via `Notification.set_permission(“granted”)`.
Class Attribute Summary collapse
-
.permission ⇒ Object
readonly
Returns the value of attribute permission.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#icon ⇒ Object
readonly
Returns the value of attribute icon.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
- .__set_permission__(value) ⇒ Object
-
.request_permission(window, callback = nil) ⇒ Object
Asynchronous spec API: returns a Promise (here a value that ‘.await`-able).
Instance Method Summary collapse
- #__event_parent__ ⇒ Object
- #__js_call__(method, args) ⇒ Object
- #__js_get__(key) ⇒ Object
- #close ⇒ Object
-
#initialize(window, title, options = nil) ⇒ Notification
constructor
A new instance of Notification.
Methods included from EventTarget
#__deliver_event__, #add_event_listener, #dispatch_event, #invoke_listener, #remove_event_listener
Constructor Details
#initialize(window, title, options = nil) ⇒ Notification
Returns a new instance of Notification.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dommy/notification.rb', line 38 def initialize(window, title, = nil) @window = window @title = title.to_s opts = .is_a?(Hash) ? : {} @body = (opts["body"] || opts[:body] || "").to_s @icon = (opts["icon"] || opts[:icon] || "").to_s @tag = (opts["tag"] || opts[:tag] || "").to_s @data = opts["data"] || opts[:data] @closed = false end |
Class Attribute Details
.permission ⇒ Object (readonly)
Returns the value of attribute permission.
15 16 17 |
# File 'lib/dommy/notification.rb', line 15 def @permission end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
36 37 38 |
# File 'lib/dommy/notification.rb', line 36 def body @body end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
36 37 38 |
# File 'lib/dommy/notification.rb', line 36 def data @data end |
#icon ⇒ Object (readonly)
Returns the value of attribute icon.
36 37 38 |
# File 'lib/dommy/notification.rb', line 36 def icon @icon end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
36 37 38 |
# File 'lib/dommy/notification.rb', line 36 def tag @tag end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
36 37 38 |
# File 'lib/dommy/notification.rb', line 36 def title @title end |
Class Method Details
.__set_permission__(value) ⇒ Object
17 18 19 |
# File 'lib/dommy/notification.rb', line 17 def (value) @permission = value.to_s end |
.request_permission(window, callback = nil) ⇒ Object
Asynchronous spec API: returns a Promise (here a value that ‘.await`-able). Callbacks receive the current permission value.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/dommy/notification.rb', line 24 def (window, callback = nil) promise = PromiseValue.resolve(window, @permission) if callback.respond_to?(:__js_call__) callback.__js_call__("call", [@permission]) elsif callback.respond_to?(:call) callback.call(@permission) end promise end |
Instance Method Details
#__event_parent__ ⇒ Object
85 86 87 |
# File 'lib/dommy/notification.rb', line 85 def __event_parent__ nil end |
#__js_call__(method, args) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/dommy/notification.rb', line 72 def __js_call__(method, args) case method when "close" close when "addEventListener" add_event_listener(args[0], args[1], args[2]) when "removeEventListener" remove_event_listener(args[0], args[1]) when "dispatchEvent" dispatch_event(args[0]) end end |
#__js_get__(key) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/dommy/notification.rb', line 57 def __js_get__(key) case key when "title" @title when "body" @body when "icon" @icon when "tag" @tag when "data" @data end end |