Class: WindowBlessing::EventedVariable

Inherits:
Object
  • Object
show all
Includes:
Evented, Tools
Defined in:
lib/window_blessing/evented_variable.rb

Overview

There are two events to subscribe to on evented variables:

on :change

Subscribe if you need to update the Model when the value changes
NOTE: a :refresh event is fired before every :change event
Ex: if the user change a Slider, this event is fired allowing you to respond to that change

on :refresh

Subscribe if you only need to update the View when the value changes
Ex: Sliders subscribe to :refresh to update their view when the value changes
    If you want to update the position of the slider, but not trigger any :change events, call .refresh(value)

both :change and :refresh events only fire if the value actually changed

Instance Method Summary collapse

Methods included from Tools

#buffer, #clone_value, #color, #fill_line, #gen_array2d, #gray_screen_color, #log, #overlapping_span, #overlay2d, #overlay_span, #range_length, #resize2d, #rgb_screen_color, #subarray2d, #window

Methods included from Evented

#event_manager, #handle_event, #on

Constructor Details

#initialize(value) ⇒ EventedVariable

Returns a new instance of EventedVariable.



20
21
22
# File 'lib/window_blessing/evented_variable.rb', line 20

def initialize(value)
  @value = value
end

Instance Method Details

#before_filter(&block) ⇒ Object

set a block to be called to processes the set value block should return the processed value



26
27
28
# File 'lib/window_blessing/evented_variable.rb', line 26

def before_filter(&block)
  @before_filter = block
end

#getObject



34
# File 'lib/window_blessing/evented_variable.rb', line 34

def get; clone_value(@value) end

#inspectObject



30
31
32
# File 'lib/window_blessing/evented_variable.rb', line 30

def inspect
  "<#{self.class}:#{object_id} value:#{@value.inspect}>"
end

#refresh(value) ⇒ Object

update the value & only trigger :refresh events subscribe to :refresh events if you need to know when the value changes, but you shouldn’t change any model-state because of it if you are changing model-state, subscribe to :change



47
48
49
50
51
52
53
# File 'lib/window_blessing/evented_variable.rb', line 47

def refresh(value)
  value = @before_filter.call(value,@value) if @before_filter
  old_value = @value
  @value = value
  handle_event :type => :refresh, :old_value => old_value, :value => value if old_value != value
  old_value
end

#set(value) ⇒ Object

update the value & trigger :change and :refresh events returns the old value



38
39
40
41
42
# File 'lib/window_blessing/evented_variable.rb', line 38

def set(value)
  old_value = refresh(value)
  handle_event :type => :change, :old_value => old_value, :value => @value if old_value != @value
  old_value
end