Module: VR::ViewCommon::IterMethods
- Defined in:
- lib/treeview/IterMethods.rb
Overview
The IterMethods module extends the GtkTreeIter class so it can work with
visualruby's colum ID symbols as well as Gtk's column numbers.
Instance Attribute Summary collapse
-
#column_keys ⇒ Object
Returns the value of attribute column_keys.
Instance Method Summary collapse
-
#[](col_id) ⇒ Object
The [] method returns the value from the specified column in the iter: val = iter - col_id – Either a visualruby column ID symbol, as above, or an integer.
- #[]=(col_id, val) ⇒ Object
-
#id(col_id) ⇒ Object
The id() method translates visualruby’s colum ID symbols into integers that GtkTreeIter can use: iter = model.append iter = “Henry” Normally, its best to use VR’s “rows” instead.
-
#load_object(obj) ⇒ Object
This will load the values of any object into the iter.
Instance Attribute Details
#column_keys ⇒ Object
Returns the value of attribute column_keys.
10 11 12 |
# File 'lib/treeview/IterMethods.rb', line 10 def column_keys @column_keys end |
Instance Method Details
#[](col_id) ⇒ Object
The [] method returns the value from the specified column in the iter:
val = iter[:name]
- col_id -- Either a visualruby column ID symbol, as above, or an integer.
20 21 22 23 24 25 26 |
# File 'lib/treeview/IterMethods.rb', line 20 def [](col_id) if col_id.is_a? Integer super else get_value(id(col_id)) end end |
#[]=(col_id, val) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/treeview/IterMethods.rb', line 28 def []=(col_id,val) if col_id.is_a? Integer super(col_id, val) else super(id(col_id), val) end end |
#id(col_id) ⇒ Object
The id() method translates visualruby’s colum ID symbols into integers that
GtkTreeIter can use:
iter = model.append
iter[id(:name)] = "Henry"
Normally, its best to use VR's "rows" instead. They will already use the column ID symbols.
44 45 46 |
# File 'lib/treeview/IterMethods.rb', line 44 def id(col_id) # :nodoc: return (col_id.is_a? Integer) ? col_id : @column_keys.index(col_id) end |
#load_object(obj) ⇒ Object
This will load the values of any object into the iter. It will look at all the
instance variables, methods, and ActiveRecord fields and match them to
columns in a VR::ListView or VR::TreeView. For example, if your object
has an instance variable, @name, this will fill in a column with the symbol :name.
class MyClass
@name = "Henry"
end
@view = VR::ListView.new(:name => String)
my = MyClass.new
row = @view.add_row()
row.load_object(my) # assigns "Henry" to first cell
-obj--any ruby object or object subclassed from ActiveRecord::Base.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/treeview/IterMethods.rb', line 65 def load_object(obj) class_sym = obj.class.name.to_sym self[class_sym] = obj if @column_keys.include?(class_sym) @column_keys.each do |k| begin self[k] = obj.send(k) if obj.respond_to?(k.to_s) rescue end end keys = @column_keys.inject([]) { |ar, e| ar << e.to_s } matches = obj.attributes.keys & keys if obj.attributes.is_a? Hash matches.each do |field| begin self[field.to_sym] = obj.attributes[field] rescue end end obj.instance_variables.each do |name| self[name] = instance_variable_get(name) if @column_keys.include?(name) end end |