dataview 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/data_view.rb +98 -85
  2. data/test/tc_data_view.rb +9 -0
  3. metadata +2 -2
@@ -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
- def create_view_row_struct
99
- fields = @columns.collect{|c| c.field}
100
- Struct.new(*fields)
101
- end
102
-
103
- def get_view_row(row_index, view_row_struct)
104
- model_row = @model[row_index]
105
-
106
- view_row_values = view_row_struct.new
107
- @columns.each_with_index do |column, column_index|
108
- value = get_raw_value(model_row, column, row_index, column_index)
109
- value = get_transformed_value(value, column, row_index, column_index)
110
- view_row_values[column.field] = value
111
- end
112
- view_row_values
113
- end
114
-
115
- def get_raw_value(model_row, col, row_index, column_index)
116
- return model_row[col.field] if col.value.nil?
117
-
118
- return col.value.call(
119
- create_value_proc_struct(row_index, column_index, col)
120
- ) if col.value.respond_to?(:call)
121
-
122
- return col.value
123
- end
124
-
125
- def create_value_proc_struct(row_index, column_index, column)
126
- @@Value_Proc_Struct_Class = Struct.new(
127
- :model,
128
- :row_index,
129
- :column_index,
130
- :column
131
- ) unless defined?(@@Value_Proc_Struct_Class)
132
-
133
- @@Value_Proc_Struct_Class.new(
134
- @model,
135
- row_index,
136
- column_index,
137
- column
138
- )
139
- end
140
-
141
- def get_transformed_value(value, column, row_index, column_index)
142
- transforms = column.value_transforms
143
- return value if transforms.nil?
144
-
145
- DuckType.check(transforms, :each, 'column.value_transforms')
146
-
147
- transforms.each do |t|
148
- next unless t
149
-
150
- params = create_value_transforms_struct(
151
- value,
152
- row_index,
153
- column_index,
154
- column
155
- )
156
-
157
- value = t.call(params)
158
- end
159
- value
160
- end
161
-
162
- def create_value_transforms_struct(value, row_index, column_index, column)
163
- @@Value_Transforms_Struct_Class = Struct.new(
164
- :value,
165
- :model,
166
- :row_index,
167
- :column_index,
168
- :column
169
- ) unless defined?(@@Value_Transforms_Struct_Class)
170
-
171
- @@Value_Transforms_Struct_Class.new(
172
- value,
173
- @model,
174
- row_index,
175
- column_index,
176
- column
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
@@ -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.0
7
- date: 2005-09-02 00:00:00 -07:00
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