dataview 0.1.0 → 0.1.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 +27 -14
  2. data/test/tc_data_view.rb +39 -4
  3. metadata +1 -1
data/lib/data_view.rb CHANGED
@@ -33,7 +33,7 @@ class DataView
33
33
  end
34
34
  public
35
35
  include Enumerable
36
- attr_reader :fields, :titles, :delegates
36
+ attr_reader :fields, :titles, :values, :value_transforms
37
37
  # Initializes the DataView.
38
38
  # ===Model
39
39
  # The DataView supports a model that contains an array of hashes. The keys in these hashes much be symbols of the field names. This is based off of the ActiveRecord interface.
@@ -46,11 +46,12 @@ class DataView
46
46
  # ====:values
47
47
  # A hash of Procs that get nondefault values of a field in a particular row. By default, the value is taken by looking at the field in the model. The Proc must accept a Hash of parameters and return the transformed value. The Hash contains the following keys:
48
48
  # * :sender - The DataView sending the call
49
+ # * :model - The Model of the DataView
49
50
  # * :row_index - The Index of the current row.
50
51
  # * :column_index - The Index of the current column.
51
52
  # * :field - The field of the current value.
52
53
  # ====:value_transforms
53
- # A Hash of the delegates that will process the values. The field symbol is the key, and the Proc or array of Procs to transform the value. The Proc must accept a Hash of parameters and return the transformed value. The Hash contains the following keys:
54
+ # A Hash of the value_transforms that will process the values. The field symbol is the key, and the Proc or array of Procs to transform the value. The Proc must accept a Hash of parameters and return the transformed value. The Hash contains the following keys:
54
55
  # * :sender - The DataView sending the call
55
56
  # * :row_index - The Index of the current row.
56
57
  # * :column_index - The Index of the current column.
@@ -116,25 +117,37 @@ class DataView
116
117
  view_row_values = []
117
118
  @fields.each_with_index do |field, field_index|
118
119
  if @values.include?(field)
119
- actual_value = @values[field].call(@model, row_index, field)
120
+ actual_value = @values[field].call(
121
+ {
122
+ :sender => self,
123
+ :model => @model,
124
+ :row_index => row_index,
125
+ :column_index => field_index,
126
+ :field => field
127
+ }
128
+ )
120
129
  else
121
130
  actual_value = model_row[field]
122
131
  end
123
132
  view_value = actual_value
124
133
 
125
134
  if @value_transforms.include?(field)
126
- delegate_value = actual_value
127
- @value_transforms[field].each do |delegate|
128
- delegate_params = {
135
+ transform_value = actual_value
136
+
137
+ transforms = @value_transforms[field]
138
+ transforms = [transforms] unless transforms.respond_to?(:each)
139
+ transforms.each do |transform|
140
+ next unless transform
141
+ transform_params = {
129
142
  :sender => self,
130
143
  :row_index => row_index,
131
144
  :column_index => field_index,
132
- :value => delegate_value,
145
+ :value => transform_value,
133
146
  :field => field
134
147
  }
135
- delegate_value = delegate.call(delegate_params)
148
+ transform_value = transform.call(transform_params)
136
149
  end
137
- view_value = delegate_value
150
+ view_value = transform_value
138
151
  end
139
152
  view_row_values << view_value
140
153
  end
@@ -144,14 +157,14 @@ class DataView
144
157
  return nil if param == nil
145
158
  return param[key]
146
159
  end
147
- def format_value_transforms(delegates)
148
- delegates.each do |k, *items|
149
- delegates[k] = items.flatten
150
- delegates[k].each do |d|
160
+ def format_value_transforms(value_transforms)
161
+ value_transforms.each do |k, *items|
162
+ value_transforms[k] = items.flatten
163
+ value_transforms[k].each do |d|
151
164
  raise ":delegate must respond to call" unless d.respond_to?(:call)
152
165
  end
153
166
  end
154
- delegates
167
+ value_transforms
155
168
  end
156
169
  end
157
170
 
data/test/tc_data_view.rb CHANGED
@@ -25,6 +25,19 @@ class DataViewTest < Test::Unit::TestCase
25
25
  assert_equal(mock_model.length, get_data_view(mock_model).length)
26
26
  assert_equal(1, get_data_view(singular_mock_model).length)
27
27
  end
28
+ def test_value_transforms_accessor
29
+ data_view = get_data_view(mock_model)
30
+ data_view.value_transforms[:field1] = nil
31
+ assert_equal(mock_model[0][:field1], data_view[0][:field1])
32
+
33
+ test_transform = lambda { |h|
34
+ 'test ' << h[:value].to_s
35
+ }
36
+ data_view.value_transforms[:field1] = test_transform
37
+
38
+ expected_transform_value = test_transform.call({:value => mock_model[0][:field1]})
39
+ assert_equal(expected_transform_value, data_view[0][:field1])
40
+ end
28
41
  private
29
42
  def do_test_brackets(model)
30
43
  data_view = get_data_view(model)
@@ -32,7 +45,11 @@ class DataViewTest < Test::Unit::TestCase
32
45
  test_model = model.respond_to?(:each_index) ? model : [model]
33
46
  test_model.each_with_index do |r, i|
34
47
  fields.each_with_index do |f, j|
35
- v = (f == :model_value_field) ? get_model_value(model, i, f) : r[f]
48
+ if f != :model_value_field
49
+ v = r[f]
50
+ else
51
+ v = get_model_value({:sender => self, :model => model, :row_index => i, :field => f})
52
+ end
36
53
  assert_equal(expected_altered_value(v), data_view[i][f])
37
54
  end
38
55
  end
@@ -44,7 +61,9 @@ class DataViewTest < Test::Unit::TestCase
44
61
  assert_equal(expected_row.length, row.to_a.length)
45
62
 
46
63
  expected_row.each do |k, expected_value|
47
- expected_value = get_model_value(model, i, k) if k == :model_value_field
64
+ if k == :model_value_field
65
+ expected_value = get_model_value({:sender => self, :model => model, :row_index => i, :field => k})
66
+ end
48
67
  assert_equal(expected_altered_value(expected_value), row[k])
49
68
  end
50
69
  end
@@ -82,8 +101,24 @@ class DataViewTest < Test::Unit::TestCase
82
101
  return @get_model_value_method if defined?(@get_model_value_method)
83
102
  @get_model_value_method = method(:get_model_value)
84
103
  end
85
- def get_model_value(model, row_index, field)
86
- row_index.to_s << ' : Custom ' << field.to_s
104
+ def get_model_value(params)
105
+ test_sender = params[:sender]
106
+ test_model = params[:model]
107
+ test_row_index = params[:row_index]
108
+ test_column_index = params[:column_index]
109
+ test_field = params[:field]
110
+
111
+ unless test_sender == self
112
+ assert_equal(DataView, test_sender.class)
113
+ unless mock_model == test_model
114
+ assert_equal(singular_mock_model, test_model[0])
115
+ end
116
+ assert((0..2).include?(test_row_index))
117
+ assert(fields.include?(test_field))
118
+ fi = fields.index(test_field)
119
+ assert_equal(fi, test_column_index)
120
+ end
121
+ test_row_index.to_s << ' : Custom ' << test_field.to_s
87
122
  end
88
123
  def fields
89
124
  return @fields if defined?(@fields)
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: dataview
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
6
+ version: 0.1.1
7
7
  date: 2005-07-25 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: