Module: QML::Reactive::Bindable

Included in:
QtProperty, Property
Defined in:
lib/qml/reactive/bindable.rb

Defined Under Namespace

Modules: Resolver

Instance Method Summary collapse

Instance Method Details

#bind { ... } ⇒ Object

Sets a property binding. The block is re-evaluated when any of other properties used in it is updated and the result is used as the new property value.

Examples:

p1 = Property.new
p2 = Property.new
p1.bind { p2.value + 1 }
p2.value = 10
p1.value #=> 11

Yields:



58
59
60
61
62
63
64
65
66
# File 'lib/qml/reactive/bindable.rb', line 58

def bind(&block)
  unbind
  value, sources = Resolver.eval_and_resolve(&block)
  set_value(value, false)
  @connections = sources.map do |source|
    fail Error, 'Recursive binding' if source == self
    source.changed.connect { set_value(block.call, false) }
  end
end

#initialize(*args, &block) ⇒ Object



5
6
7
8
# File 'lib/qml/reactive/bindable.rb', line 5

def initialize(*args, &block)
  super
  @connections = []
end

#unbindObject



68
69
70
# File 'lib/qml/reactive/bindable.rb', line 68

def unbind
  @connections.each(&:disconnect)
end

#valueObject



25
26
27
28
# File 'lib/qml/reactive/bindable.rb', line 25

def value
  touch
  super
end

#value=(new_value, unbind = true) ⇒ Object Also known as: set_value

Sets a new value. The old property binding is discarded.



13
14
15
16
17
18
19
20
# File 'lib/qml/reactive/bindable.rb', line 13

def value=(new_value, unbind = true)
  self.unbind if unbind
  unless value == new_value
    super(new_value)
    changed.emit(new_value)
  end
  new_value
end