dataview 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/data_view.rb +98 -85
- data/test/tc_data_view.rb +9 -0
- metadata +2 -2
data/lib/data_view.rb
CHANGED
@@ -17,11 +17,8 @@ class << self
|
|
17
17
|
@@Column_Struct_Class.new(field, title, value, value_transforms.flatten)
|
18
18
|
end
|
19
19
|
end
|
20
|
-
|
21
|
-
public
|
22
20
|
|
23
21
|
include Enumerable
|
24
|
-
attr_reader :columns, :rows
|
25
22
|
# Initializes the DataView.
|
26
23
|
# ===Model
|
27
24
|
# The DataView supports a model that contains an array of structs or hashes.
|
@@ -29,6 +26,7 @@ end
|
|
29
26
|
# ===Columns
|
30
27
|
# An Array of Structs (see :create_column) that contain data
|
31
28
|
# about the columns and how the columns will be handled.
|
29
|
+
# The Columns array also has the method find_by_field(field) which will return the column with the passed field.
|
32
30
|
# The column order in the each method is the same as the array order.
|
33
31
|
# Each Struct contains the following keys:
|
34
32
|
# ====:field
|
@@ -62,12 +60,28 @@ end
|
|
62
60
|
# * :column_index - The Index of the current column.
|
63
61
|
# * :column - The column of the current value.
|
64
62
|
def initialize(model, *columns)
|
63
|
+
# A new array object is created here.
|
65
64
|
columns = columns.flatten
|
66
65
|
@model = if model.respond_to?(:each_index) then model else [model] end
|
67
66
|
|
68
67
|
# @columns is an ordered array of the columns that will be shown
|
69
68
|
@columns = columns
|
69
|
+
class << @columns
|
70
|
+
def find_by_field(field)
|
71
|
+
self.find {|c| c.field == field}
|
72
|
+
end
|
73
|
+
end
|
70
74
|
end
|
75
|
+
|
76
|
+
# An array of the columns in the DataView. columns also has the find_by_field(field) method to quickly find a column by its field.
|
77
|
+
def columns
|
78
|
+
@columns
|
79
|
+
end
|
80
|
+
|
81
|
+
# An array of the rows in the DataView.
|
82
|
+
def rows
|
83
|
+
@rows
|
84
|
+
end
|
71
85
|
|
72
86
|
# Creates a column definition struct.
|
73
87
|
def create_column(field=nil, title=nil, value=nil, *value_transforms)
|
@@ -94,88 +108,87 @@ end
|
|
94
108
|
end
|
95
109
|
|
96
110
|
private
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
end
|
111
|
+
def create_view_row_struct
|
112
|
+
fields = @columns.collect{|c| c.field}
|
113
|
+
Struct.new(*fields)
|
114
|
+
end
|
115
|
+
|
116
|
+
def get_view_row(row_index, view_row_struct)
|
117
|
+
model_row = @model[row_index]
|
118
|
+
|
119
|
+
view_row_values = view_row_struct.new
|
120
|
+
@columns.each_with_index do |column, column_index|
|
121
|
+
value = get_raw_value(model_row, column, row_index, column_index)
|
122
|
+
value = get_transformed_value(value, column, row_index, column_index)
|
123
|
+
view_row_values[column.field] = value
|
124
|
+
end
|
125
|
+
view_row_values
|
126
|
+
end
|
127
|
+
|
128
|
+
def get_raw_value(model_row, col, row_index, column_index)
|
129
|
+
return model_row[col.field] if col.value.nil?
|
130
|
+
|
131
|
+
return col.value.call(
|
132
|
+
create_value_proc_struct(row_index, column_index, col)
|
133
|
+
) if col.value.respond_to?(:call)
|
134
|
+
|
135
|
+
return col.value
|
136
|
+
end
|
137
|
+
|
138
|
+
def create_value_proc_struct(row_index, column_index, column)
|
139
|
+
@@Value_Proc_Struct_Class = Struct.new(
|
140
|
+
:model,
|
141
|
+
:row_index,
|
142
|
+
:column_index,
|
143
|
+
:column
|
144
|
+
) unless defined?(@@Value_Proc_Struct_Class)
|
145
|
+
|
146
|
+
@@Value_Proc_Struct_Class.new(
|
147
|
+
@model,
|
148
|
+
row_index,
|
149
|
+
column_index,
|
150
|
+
column
|
151
|
+
)
|
152
|
+
end
|
153
|
+
|
154
|
+
def get_transformed_value(value, column, row_index, column_index)
|
155
|
+
transforms = column.value_transforms
|
156
|
+
return value if transforms.nil?
|
157
|
+
|
158
|
+
DuckType.check(transforms, :each, 'column.value_transforms')
|
159
|
+
|
160
|
+
transforms.each do |t|
|
161
|
+
next unless t
|
162
|
+
|
163
|
+
params = create_value_transforms_struct(
|
164
|
+
value,
|
165
|
+
row_index,
|
166
|
+
column_index,
|
167
|
+
column
|
168
|
+
)
|
169
|
+
|
170
|
+
value = t.call(params)
|
171
|
+
end
|
172
|
+
value
|
173
|
+
end
|
174
|
+
|
175
|
+
def create_value_transforms_struct(value, row_index, column_index, column)
|
176
|
+
@@Value_Transforms_Struct_Class = Struct.new(
|
177
|
+
:value,
|
178
|
+
:model,
|
179
|
+
:row_index,
|
180
|
+
:column_index,
|
181
|
+
:column
|
182
|
+
) unless defined?(@@Value_Transforms_Struct_Class)
|
183
|
+
|
184
|
+
@@Value_Transforms_Struct_Class.new(
|
185
|
+
value,
|
186
|
+
@model,
|
187
|
+
row_index,
|
188
|
+
column_index,
|
189
|
+
column
|
190
|
+
)
|
191
|
+
end
|
179
192
|
end
|
180
193
|
|
181
194
|
end
|
data/test/tc_data_view.rb
CHANGED
@@ -37,6 +37,15 @@ class DataViewTest < Test::Unit::TestCase
|
|
37
37
|
expected_transform_value = test_transform.call(arg)
|
38
38
|
assert_equal(expected_transform_value, data_view[0].field1)
|
39
39
|
end
|
40
|
+
|
41
|
+
def test_find_by_field
|
42
|
+
data_view = get_data_view(mock_model)
|
43
|
+
col = data_view.columns.find_by_field(:field1)
|
44
|
+
assert_not_nil(col)
|
45
|
+
|
46
|
+
expected_col = data_view.columns.find {|col| col.field == :field1}
|
47
|
+
assert_equal(expected_col, col)
|
48
|
+
end
|
40
49
|
|
41
50
|
private
|
42
51
|
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: dataview
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.3.
|
7
|
-
date: 2005-
|
6
|
+
version: 0.3.1
|
7
|
+
date: 2005-10-18 00:00:00 -07:00
|
8
8
|
summary: Data View is a library that creates a view of a data model. The view can transform the data of the data model without changing the data. Supports ActiveRecord models and other data models that have a similar interface.
|
9
9
|
require_paths:
|
10
10
|
- lib
|