Module: DOM::ElementAccessor
- Included in:
- Element
- Defined in:
- opal/fron/dom/modules/element_accessor.rb
Overview
Element accessor methods
Instance Method Summary collapse
-
#attribute_accessor(attribute, options = {}) ⇒ Object
Defines a attributes accessor that is delegated to the underlying elements gieven attribute.
-
#element_accessor(property, options = {}) ⇒ Object
Defines a property accessor that is delegated to the underlying element.
-
#element_method(method) ⇒ Object
Defines a method that is delegated to the underlying element.
Instance Method Details
#attribute_accessor(attribute, options = {}) ⇒ Object
Defines a attributes accessor that is delegated to the underlying elements gieven attribute.
Options:
-
as - Use this instead of the attribute as identifier
-
default - Return this value if the attribute is null or undefined
-
coerce - Coerse the string value of the attribute with this method
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'opal/fron/dom/modules/element_accessor.rb', line 24 def attribute_accessor(attribute, = {}) = { as: attribute, default: nil, coerce: nil }.merge!() define_method [:as] do value = self[attribute] || [:default] next value unless [:coerce] value.send([:coerce]) end define_method "#{[:as]}=" do |value| self[attribute] = value send('_attribute_changed', attribute, value) if respond_to?(:_attribute_changed) end end |
#element_accessor(property, options = {}) ⇒ Object
Defines a property accessor that is delegated to the underlying element.
Options:
-
as - Use this instead of the property as identifier
-
default - Return this value if the property is null or undefined
48 49 50 51 52 53 54 55 56 57 58 |
# File 'opal/fron/dom/modules/element_accessor.rb', line 48 def element_accessor(property, = {}) = { as: property, default: nil }.merge!() define_method [:as] do `#{@el}[#{property}] || #{[:default]}` end define_method "#{[:as]}=" do |value| `#{@el}[#{property}] = #{value}` end end |
#element_method(method) ⇒ Object
Defines a method that is delegated to the underlying element.
8 9 10 11 12 |
# File 'opal/fron/dom/modules/element_accessor.rb', line 8 def element_method(method) define_method method do `#{@el}[#{method}]()` end end |