Class: RubyCurses::Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/rbcurse/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

Instance Method Summary collapse

Constructor Details

#initialize(value = "") ⇒ Variable

Returns a new instance of Variable.



2017
2018
2019
2020
2021
2022
# File 'lib/rbcurse/rwidget.rb', line 2017

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

Instance Method Details

#[](key) ⇒ Object



2099
2100
2101
# File 'lib/rbcurse/rwidget.rb', line 2099

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



2028
2029
2030
2031
2032
# File 'lib/rbcurse/rwidget.rb', line 2028

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

#get_value(val = nil) ⇒ Object

value of the variable



2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
# File 'lib/rbcurse/rwidget.rb', line 2043

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



2096
2097
2098
# File 'lib/rbcurse/rwidget.rb', line 2096

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.



2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
# File 'lib/rbcurse/rwidget.rb', line 2059

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



2104
2105
2106
# File 'lib/rbcurse/rwidget.rb', line 2104

def source
  @value
end

#to_sObject



2107
2108
2109
# File 'lib/rbcurse/rwidget.rb', line 2107

def to_s
  inspect
end

#update_command(*args, &block) ⇒ Object

install trigger to call whenever a value is updated



2036
2037
2038
2039
2040
# File 'lib/rbcurse/rwidget.rb', line 2036

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

#valueObject



2092
2093
2094
2095
# File 'lib/rbcurse/rwidget.rb', line 2092

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

#value=(val) ⇒ Object



2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
# File 'lib/rbcurse/rwidget.rb', line 2082

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