Class: RubyCurses::Variable
Overview
Like Tk’s TkVariable, a simple proxy that can be passed to a widget. The widget will update the Variable. A variable can be used to link a field with a label or some other widget. This is the new version of Variable. Deleting old version on 2009-01-17 12:04
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#add_dependent(obj) ⇒ Object
This is to ensure that change handlers for all dependent objects are called so they are updated.
-
#get_value(val = nil) ⇒ Object
value of the variable.
-
#initialize(value = "") ⇒ Variable
constructor
A new instance of Variable.
- #inspect ⇒ Object
-
#set_value(val, key = "") ⇒ Object
update the value of this variable.
-
#source ⇒ Object
in order to run some method we don’t yet support.
- #to_s ⇒ Object
-
#update_command(*args, &block) ⇒ Object
(also: #command)
install trigger to call whenever a value is updated.
- #value ⇒ Object
- #value=(val) ⇒ Object
Constructor Details
#initialize(value = "") ⇒ Variable
Returns a new instance of Variable.
2587 2588 2589 2590 2591 2592 |
# File 'lib/rbhex/core/widgets/rwidget.rb', line 2587 def initialize value="" @update_command = [] @args = [] @value = value @klass = value.class.to_s end |
Instance Method Details
#[](key) ⇒ Object
2671 2672 2673 |
# File 'lib/rbhex/core/widgets/rwidget.rb', line 2671 def [](key) @value[key] end |
#add_dependent(obj) ⇒ Object
This is to ensure that change handlers for all dependent objects are called so they are updated. This is called from text_variable property of some widgets. If you use one text_variable across objects, all will be updated auto. User does not need to call. @ private
2599 2600 2601 2602 2603 |
# File 'lib/rbhex/core/widgets/rwidget.rb', line 2599 def add_dependent obj $log.debug " ADDING DEPENDE #{obj}" @dependents ||= [] @dependents << obj end |
#get_value(val = nil) ⇒ Object
value of the variable
2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 |
# File 'lib/rbhex/core/widgets/rwidget.rb', line 2615 def get_value val=nil if @klass == 'String' return @value elsif @klass == 'Hash' return @value[val] elsif @klass == 'Array' return @value[val] else return @value end end |
#inspect ⇒ Object
2668 2669 2670 |
# File 'lib/rbhex/core/widgets/rwidget.rb', line 2668 def inspect @value.inspect end |
#set_value(val, key = "") ⇒ Object
update the value of this variable. 2008-12-31 18:35 Added source so one can identify multiple sources that are updating. Idea is that mutiple fields (e.g. checkboxes) can share one var and update a hash through it. Source would contain some code or key relatin to each field.
2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 |
# File 'lib/rbhex/core/widgets/rwidget.rb', line 2631 def set_value val, key="" oldval = @value if @klass == 'String' @value = val elsif @klass == 'Hash' $log.debug " Variable setting hash #{key} to #{val}" oldval = @value[key] @value[key]=val elsif @klass == 'Array' $log.debug " Variable setting array #{key} to #{val}" oldval = @value[key] @value[key]=val else oldval = @value @value = val end return if @update_command.nil? @update_command.each_with_index do |comm, ix| comm.call(self, *@args[ix]) unless comm.nil? end @dependents.each {|d| d.fire_property_change(d, oldval, val) } unless @dependents.nil? end |
#source ⇒ Object
in order to run some method we don’t yet support
2676 2677 2678 |
# File 'lib/rbhex/core/widgets/rwidget.rb', line 2676 def source @value end |
#to_s ⇒ Object
2679 2680 2681 |
# File 'lib/rbhex/core/widgets/rwidget.rb', line 2679 def to_s inspect end |
#update_command(*args, &block) ⇒ Object Also known as: command
install trigger to call whenever a value is updated
2607 2608 2609 2610 2611 |
# File 'lib/rbhex/core/widgets/rwidget.rb', line 2607 def update_command *args, &block $log.debug "Variable: update command set " # #{args}" @update_command << block @args << args end |
#value ⇒ Object
2664 2665 2666 2667 |
# File 'lib/rbhex/core/widgets/rwidget.rb', line 2664 def value raise "Please use set_value for hash/array: #{@klass}" if @klass=='Hash' #or @klass=='Array' @value end |
#value=(val) ⇒ Object
2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 |
# File 'lib/rbhex/core/widgets/rwidget.rb', line 2654 def value= (val) raise "Please use set_value for hash/array" if @klass=='Hash' or @klass=='Array' oldval = @value @value=val return if @update_command.nil? @update_command.each_with_index do |comm, ix| comm.call(self, *@args[ix]) unless comm.nil? end @dependents.each {|d| d.fire_property_change(d, oldval, val) } unless @dependents.nil? end |