Class: Canis::Variable

Inherits:
Object show all
Defined in:
lib/canis/core/widgets/rwidget.rb

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

Example

x = Variable.new

If x is passed as the variable for a RadioButton group, then this keeps the value of the button that is on.

y = Variable.new false

z = Variable.new Hash.new

If z is passed as variable to create several Checkboxes, and each has the name property set, then this hash will contain the status of each checkbox with name as key.

Since:

  • 1.2.0

Instance Method Summary collapse

Constructor Details

#initialize(value = "") ⇒ Variable

{{{

Since:

  • 1.2.0



2855
2856
2857
2858
2859
2860
# File 'lib/canis/core/widgets/rwidget.rb', line 2855

def initialize value=""
  @update_command = []
  @args = []
  @value = value
  @klass = value.class.to_s
end

Instance Method Details

#[](key) ⇒ Object

Since:

  • 1.2.0



2940
2941
2942
# File 'lib/canis/core/widgets/rwidget.rb', line 2940

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. NOTE: I have removed text_variable from widget to simplify, so this seems to be dead. @ private

Since:

  • 1.2.0



2868
2869
2870
2871
2872
# File 'lib/canis/core/widgets/rwidget.rb', line 2868

def add_dependent obj
  $log.debug " ADDING DEPENDE #{obj}"
  @dependents ||= []
  @dependents << obj
end

#get_value(val = nil) ⇒ Object

value of the variable

Since:

  • 1.2.0



2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
# File 'lib/canis/core/widgets/rwidget.rb', line 2884

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

#inspectObject

Since:

  • 1.2.0



2937
2938
2939
# File 'lib/canis/core/widgets/rwidget.rb', line 2937

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.

Since:

  • 1.2.0



2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
# File 'lib/canis/core/widgets/rwidget.rb', line 2900

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

#sourceObject

in order to run some method we don’t yet support

Since:

  • 1.2.0



2945
2946
2947
# File 'lib/canis/core/widgets/rwidget.rb', line 2945

def source
  @value
end

#to_sObject

Since:

  • 1.2.0



2948
2949
2950
# File 'lib/canis/core/widgets/rwidget.rb', line 2948

def to_s
  inspect
end

#update_command(*args, &block) ⇒ Object Also known as: command

install trigger to call whenever a value is updated

Since:

  • 1.2.0



2876
2877
2878
2879
2880
# File 'lib/canis/core/widgets/rwidget.rb', line 2876

def update_command *args, &block
  $log.debug "Variable: update command set " # #{args}"
  @update_command << block
  @args << args
end

#valueObject

Since:

  • 1.2.0



2933
2934
2935
2936
# File 'lib/canis/core/widgets/rwidget.rb', line 2933

def value
  raise "Please use set_value for hash/array: #{@klass}" if @klass=='Hash' #or @klass=='Array'
  @value
end

#value=(val) ⇒ Object

Since:

  • 1.2.0



2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
# File 'lib/canis/core/widgets/rwidget.rb', line 2923

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