Class: Glimmer::DataBinding::ElementBinding

Inherits:
Object
  • Object
show all
Includes:
Observable, Observer
Defined in:
lib/glimmer/data_binding/element_binding.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element, property, translator: nil) ⇒ ElementBinding

Returns a new instance of ElementBinding.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/glimmer/data_binding/element_binding.rb', line 11

def initialize(element, property, translator: nil)
  @element = element
  if (property_parts = property.to_s.match(Glimmer::Web::ElementProxy::REGEX_CLASS_NAME_SUB_PROPERTY))
    @property, @sub_property = property_parts.to_a.drop(1)
  elsif (property_parts = property.to_s.match(Glimmer::Web::ElementProxy::REGEX_STYLE_SUB_PROPERTY))
    @property, @sub_property = property_parts.to_a.drop(1)
    @sub_property = @sub_property.gsub('_', '-')
  else
    @property = property
  end
  @translator = translator

  if @element.respond_to?(:remove)
    unregister_handler = lambda { |dispose_event| unregister_all_observables }
    @element.handle_observation_request('on_remove', unregister_handler)
  end
end

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element.



10
11
12
# File 'lib/glimmer/data_binding/element_binding.rb', line 10

def element
  @element
end

#propertyObject (readonly)

Returns the value of attribute property.



10
11
12
# File 'lib/glimmer/data_binding/element_binding.rb', line 10

def property
  @property
end

#sub_propertyObject (readonly)

Returns the value of attribute sub_property.



10
11
12
# File 'lib/glimmer/data_binding/element_binding.rb', line 10

def sub_property
  @sub_property
end

#translatorObject (readonly)

Returns the value of attribute translator.



10
11
12
# File 'lib/glimmer/data_binding/element_binding.rb', line 10

def translator
  @translator
end

Instance Method Details

#call(value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/glimmer/data_binding/element_binding.rb', line 29

def call(value)
  evaluated_property_value = evaluate_property
  converted_value = @translator&.call(value, evaluated_property_value) || value
  if converted_value != evaluated_property_value
    if @sub_property
      if @property.to_s == 'class_name'
        @element.class_name_included(@sub_property, converted_value)
      elsif @property.to_s == 'style'
        @element.style_property(@sub_property, converted_value)
      end
    else
      @element.send("#{@property}=", converted_value)
    end
  end
end

#evaluate_propertyObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/glimmer/data_binding/element_binding.rb', line 45

def evaluate_property
  if @sub_property
    if @property.to_s == 'class_name'
      @element.class_name_included(@sub_property)
    elsif @property.to_s == 'style'
      @element.style_property(@sub_property)
    end
  else
    @element.send(@property)
  end
end