Sha256: 75a68e306b035021ea0058539e48508e17251fb0b3000719699ef04802b46e59

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

require_relative 'observable'
require_relative 'observer'

class WidgetBinding
  include Glimmer
  include Observable
  include Observer

  attr_reader :widget, :property
  @@property_type_converters = {
    :text => Proc.new { |value| value.to_s },
    :items => Proc.new { |value| value.to_java :string},
    :visible => Proc.new { |value| !!value},
  }
  def initialize(model, property, translator = nil)
    @widget = model
    @property = property
    @translator = translator || proc {|value| value}
    add_contents(@widget) {
      on_widget_disposed { |dispose_event|
        unregister_all_observables
      }
    }
  end
  def update(value)
    converted_value = translated_value = @translator.call(value)
    converter = @@property_type_converters[@property.to_sym]
    converted_value = converter.call(translated_value) if converter
    @widget.widget.send("set#{@property.camelcase(:upper)}", converted_value) unless evaluate_property == converted_value
  end
  def evaluate_property
    @widget.widget.send(@property)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
glimmer-0.3.3 lib/command_handlers/models/widget_binding.rb