Module: DOM::ElementAccessor

Included in:
Element
Defined in:
opal/fron/dom/modules/element_accessor.rb

Overview

Element accessor methods

Instance Method Summary collapse

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

Parameters:

  • attribute (Symbol)

    The attribute

  • options (Hash) (defaults to: {})

    The options



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, options = {})
  options = { as: attribute, default: nil, coerce: nil }.merge!(options)

  define_method options[:as] do
    value = self[attribute] || options[:default]
    next value unless options[:coerce]
    value.send(options[:coerce])
  end

  define_method "#{options[: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

Parameters:

  • property (Symbol)

    The property

  • options (Hash) (defaults to: {})

    The options



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, options = {})
  options = { as: property, default: nil }.merge!(options)

  define_method options[:as] do
    `#{@el}[#{property}] || #{options[:default]}`
  end

  define_method "#{options[:as]}=" do |value|
    `#{@el}[#{property}] = #{value}`
  end
end

#element_method(method) ⇒ Object

Defines a method that is delegated to the underlying element.

Parameters:

  • method (Symbol)

    The name of the method



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