Class: RubyMVC::Models::HashArrayTableModel
Overview
This class provides an adapter to expose an array of hashes or anything else that responds to the #keys, #[] and #[]= methods as a TableModel instance.
Note that the keys of the objects in the array will be requested as symbols and not as any other object type.
Instance Method Summary
collapse
Methods inherited from Model
#[]=, adapt, #each_label, #is_editable?, #label_for, #labels, #link_labels, #size
#signal, #signals, #valid_signal!, #valid_signal?
#signal_connect, #signal_disconnect, #signal_emit
Constructor Details
Returns a new instance of HashArrayTableModel.
37
38
39
40
41
42
43
|
# File 'lib/ruby_mvc/models/array_table_model.rb', line 37
def initialize(data, options = {})
raise ArgumentError, "argument not an Array" if !data.is_a? Array
super(options)
@options = options
@data = data
end
|
Instance Method Details
#[](row) ⇒ Object
101
102
103
|
# File 'lib/ruby_mvc/models/array_table_model.rb', line 101
def [](row)
@data[row]
end
|
#create_rows(count = 1) ⇒ Object
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/ruby_mvc/models/array_table_model.rb', line 45
def create_rows(count = 1)
if f = @options[:row_factory]
f.call(count)
else
r = []
t = @options[:row_template] || @data.last
count.times { r << t.clone }
r
end
end
|
#each(&block) ⇒ Object
Iterates over each of the elements in the array and provides a Model instance to the caller based on the keys contained in the object itself.
109
110
111
112
113
114
115
|
# File 'lib/ruby_mvc/models/array_table_model.rb', line 109
def each(&block)
return if !block
@data.each do |x|
block.call(Model.adapt(x, @options))
end
end
|
#each_with_index(&block) ⇒ Object
117
118
119
120
121
122
123
|
# File 'lib/ruby_mvc/models/array_table_model.rb', line 117
def each_with_index(&block)
return if !block
@data.each_with_index do |x, i|
block.call(Model.adapt(x, @options), i)
end
end
|
#insert_row(index, row) ⇒ Object
56
57
58
59
|
# File 'lib/ruby_mvc/models/array_table_model.rb', line 56
def insert_row(index, row)
@data.insert(index, row)
super(index, Model.adapt(row, @options))
end
|
#insert_rows(index, rows) ⇒ Object
61
62
63
64
|
# File 'lib/ruby_mvc/models/array_table_model.rb', line 61
def insert_rows(index, rows)
@data.insert(index, *rows)
super(index, rows.collect { |r| Model.adapt(r, @options) })
end
|
#keys ⇒ Object
The keys used will come from the first element in the array or be empty if the array is empty.
89
90
91
92
93
94
95
|
# File 'lib/ruby_mvc/models/array_table_model.rb', line 89
def keys
if @data.size > 0
@data.first.keys
else
{}
end
end
|
#remove_row(index) ⇒ Object
66
67
68
69
70
|
# File 'lib/ruby_mvc/models/array_table_model.rb', line 66
def remove_row(index)
row = Model.adapt(@data.delete_at(index), @options)
signal_emit("rows-removed", self, index, [ row ])
row
end
|
#remove_rows(index, count) ⇒ Object
72
73
74
75
76
77
78
79
|
# File 'lib/ruby_mvc/models/array_table_model.rb', line 72
def remove_rows(index, count)
rows = []
count.times do
rows << Model.adapt(@data.delete_at(index), @options)
end
signal_emit("rows-removed", self, index, rows)
rows
end
|
#update_row(index, row) ⇒ Object
81
82
83
84
|
# File 'lib/ruby_mvc/models/array_table_model.rb', line 81
def update_row(index, row)
@data[index] = row
super(index, Model.adapt(row, @options))
end
|
#value_for(row, key) ⇒ Object
97
98
99
|
# File 'lib/ruby_mvc/models/array_table_model.rb', line 97
def value_for(row, key)
@data[row][key.to_sym]
end
|