drive_time 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # DriveTime
2
+ [![Gem Version](https://badge.fury.io/rb/drive_time.png)](http://badge.fury.io/rb/drive_time)
2
3
 
3
- Drive Time allows you to transform one or more Google Spreadsheet's into a Rails model graph.
4
+ Drive Time allows you transfer data from one or more Google Spreadsheets to your Rails Models. It supports associations and has a number of useful features to give you choices on how to mapthe data.
4
5
 
5
6
  ## Installation
6
7
 
@@ -0,0 +1,15 @@
1
+ module DriveTime
2
+
3
+ # Split a string on ',' into an array of values
4
+ class MultiBuilder
5
+
6
+ def build(value)
7
+ if value.present?
8
+ value = value.gsub("\n", "").strip.split(',')
9
+ else
10
+ []
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -6,7 +6,6 @@ module DriveTime
6
6
  protected
7
7
 
8
8
  def process_value(value)
9
-
10
9
  # Add a period to initials
11
10
  if value.length == 1
12
11
  return "#{value}."
@@ -104,11 +104,7 @@ module DriveTime
104
104
  def worksheet_for_association(name, worksheets)
105
105
  # And find the worksheet that satisfies it
106
106
  worksheets.each do |worksheet|
107
- puts "#{name} --->"
108
107
  class_name = DriveTime.class_name_from_title(worksheet.title)
109
- puts " -- #{DriveTime.underscore_from_text(worksheet.title)}"
110
- #resolved = @class_name_map.resolve_mapped_from_original class_name
111
- #puts "Resolved -- #{resolved}"
112
108
  # If the name matches we have a dependent relationship
113
109
  if name == DriveTime.underscore_from_text(worksheet.title)
114
110
  return worksheet
@@ -35,7 +35,6 @@ module DriveTime
35
35
  fields = rows.shift.map{ |row| row }
36
36
  # Reject rows of only empty strings (empty cells).
37
37
  rows.reject! {|row| row.all?(&:empty?)}
38
-
39
38
  rows.each do |row|
40
39
  generate_model_from_row clazz, worksheet.mapping, fields, row
41
40
  end
@@ -70,18 +69,7 @@ module DriveTime
70
69
  # Create new model
71
70
  model = clazz.new(model_fields, without_protection: true)
72
71
 
73
- # Invoke any method calls
74
- method_calls.each do |method_call|
75
- call_step = model
76
- methods = method_call["methods"]
77
- methods.each_with_index do |method, index|
78
- if index == method_call.length - 1
79
- call_step.send(method, method_call["value"])
80
- else
81
- call_step = call_step.send(method)
82
- end
83
- end
84
- end
72
+ invoke_methods_on_model(model, method_calls)
85
73
 
86
74
  # Add its associations
87
75
  add_associations(model, mapping)
@@ -101,6 +89,21 @@ module DriveTime
101
89
  end
102
90
  end
103
91
 
92
+ def invoke_methods_on_model(model, method_calls)
93
+ # Invoke any method calls
94
+ method_calls.each do |method_call|
95
+ call_step = model
96
+ methods = method_call["methods"]
97
+ methods.each_with_index do |method, index|
98
+ if index == method_call.length - 1
99
+ call_step.send(method, method_call["value"])
100
+ else
101
+ call_step = call_step.send(method)
102
+ end
103
+ end
104
+ end
105
+ end
106
+
104
107
  def row_fields_to_method_calls(mapping, model_key)
105
108
  method_calls = []
106
109
  # Run through the mapping, pulling values from row_map as required
@@ -113,7 +116,7 @@ module DriveTime
113
116
  raise NoMethodError "Missing Method: Name for call field: #{field_name} in mapping: #{mapping}"
114
117
  end
115
118
 
116
- field_value = parse_field_value(@row_map[field_name])
119
+ field_value = parse_field_value(@row_map[field_name], model_key)
117
120
 
118
121
  # handle multi-values
119
122
  if call_mapping[:builder] == "multi"
@@ -139,14 +142,13 @@ module DriveTime
139
142
  raise NoFieldNameError "Missing Field: Name for field: #{value} in mapping: #{mapping}"
140
143
  end
141
144
 
142
- field_value = parse_field_value(@row_map[field_name])
145
+ field_value = parse_field_value(@row_map[field_name], model_key)
143
146
  model_fields[mapped_to_field_name || field_name] = field_value
144
147
  end
145
148
  end
146
149
  return model_fields
147
150
  end
148
151
 
149
- # TODO: Refactor this big ugly beast
150
152
  def add_associations(model, mapping)
151
153
  associations_mapping = mapping[:associations]
152
154
  if associations_mapping
@@ -282,9 +284,9 @@ module DriveTime
282
284
  class_name.constantize
283
285
  end
284
286
 
285
- def parse_field_value(field_value)
287
+ def parse_field_value(field_value, model_key)
286
288
  if field_value
287
- field_value = check_for_token(field_value)
289
+ field_value = check_for_token(field_value, model_key)
288
290
  field_value = check_for_boolean(field_value)
289
291
  end
290
292
  # Make sure any empty cells give us nil (rather than an empty string)
@@ -292,7 +294,7 @@ module DriveTime
292
294
  field_value
293
295
  end
294
296
 
295
- def check_for_token(field_value)
297
+ def check_for_token(field_value, model_key)
296
298
  # Check for token pattern: {{some_value}}
297
299
  match = /\{\{(.*?)\}\}/.match(field_value)
298
300
  if match
@@ -52,7 +52,7 @@ module DriveTime
52
52
 
53
53
  # Is there an instance
54
54
  model = models_for_class[key]
55
- unless model.preset?
55
+ unless model.present?
56
56
  raise NoModelOfClassWithKeyInStoreError, "No model of class #{clazz} with a key of #{key} in model store"
57
57
  end
58
58
 
@@ -1,3 +1,3 @@
1
1
  module DriveTime
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  module DriveTime
4
4
 
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ module DriveTime
4
+
5
+ describe MultiBuilder do
6
+
7
+ before :each do
8
+ @builder = MultiBuilder.new
9
+ @valid_string = "one,two,three"
10
+ @valid_string_with_newlines = "\none,\ntwo,\nthree\n"
11
+ end
12
+
13
+ context "for a valid string" do
14
+
15
+ it "should split a string on commas" do
16
+ @builder.build(@valid_string).should == ["one", "two", "three"]
17
+ end
18
+
19
+ it "should remove all newlines" do
20
+ @builder.build(@valid_string_with_newlines).should == ["one", "two", "three"]
21
+ end
22
+
23
+ end
24
+
25
+ it "should return an empty array for an empty string" do
26
+ @builder.build("").should == []
27
+ end
28
+
29
+ end
30
+
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: drive_time
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -171,6 +171,7 @@ files:
171
171
  - lib/drive_time.rb
172
172
  - lib/drive_time/bi_directional_hash.rb
173
173
  - lib/drive_time/builders/join_builder.rb
174
+ - lib/drive_time/builders/multi_builder.rb
174
175
  - lib/drive_time/builders/name_builder.rb
175
176
  - lib/drive_time/class_name_map.rb
176
177
  - lib/drive_time/converters/spreadsheets_converter.rb
@@ -182,15 +183,16 @@ files:
182
183
  - lib/drive_time/version.rb
183
184
  - spec/drive_time/bi_directional_hash_spec.rb
184
185
  - spec/drive_time/builder/join_builder_spec.rb
186
+ - spec/drive_time/builder/multi_buider_spec.rb
185
187
  - spec/drive_time/builder/name_builder_spec.rb
186
188
  - spec/drive_time/class_name_map_spec.rb
187
189
  - spec/drive_time/converters/worksheet_converter_spec.rb
188
190
  - spec/drive_time/field_expander_spec.rb
191
+ - spec/drive_time/integration/full.rb
189
192
  - spec/drive_time/loader_spec.rb
190
193
  - spec/drive_time/model_store_spec.rb
191
194
  - spec/drive_time_spec.rb
192
195
  - spec/fixtures/mapping.yml
193
- - spec/full_tests.rb
194
196
  - spec/spec_helper.rb
195
197
  homepage: https://github.com/stationkeeping/drive_time
196
198
  licenses: []
@@ -220,14 +222,15 @@ summary: Map Worksheets to Models and their columns to model attributes. Seed yo
220
222
  test_files:
221
223
  - spec/drive_time/bi_directional_hash_spec.rb
222
224
  - spec/drive_time/builder/join_builder_spec.rb
225
+ - spec/drive_time/builder/multi_buider_spec.rb
223
226
  - spec/drive_time/builder/name_builder_spec.rb
224
227
  - spec/drive_time/class_name_map_spec.rb
225
228
  - spec/drive_time/converters/worksheet_converter_spec.rb
226
229
  - spec/drive_time/field_expander_spec.rb
230
+ - spec/drive_time/integration/full.rb
227
231
  - spec/drive_time/loader_spec.rb
228
232
  - spec/drive_time/model_store_spec.rb
229
233
  - spec/drive_time_spec.rb
230
234
  - spec/fixtures/mapping.yml
231
- - spec/full_tests.rb
232
235
  - spec/spec_helper.rb
233
236
  has_rdoc: