garden 0.0.11.pre → 0.0.12.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -44,29 +44,40 @@ module Garden
44
44
  include Reflection
45
45
  include Columns
46
46
 
47
- def initialize(clazz, attributes=nil)
48
- @object = clazz.new
47
+ def initialize(clazz, attributes=nil, options={})
48
+ @options = options.dup
49
+
50
+ if @options.has_key? :reference
51
+ @reference_column = @options.delete(:reference).to_sym
52
+ @reference = attributes.delete @reference_column
53
+ @object = clazz.send "find_by_#{@reference_column}".to_sym, @reference
54
+
55
+ unless @object
56
+ puts "Unable to find referenced instance using column '#{@reference_column}' and value '#{@reference}'"
57
+ return
58
+ end
59
+ else
60
+ @object = clazz.new
61
+ end
62
+
49
63
  assign_attributes attributes if attributes
50
64
  end
51
65
 
52
66
  def assign_attributes(attributes)
53
67
 
54
68
  attributes.each do |key, value|
69
+ # puts "assign attribute: #{key}, #{value}"
55
70
  map_attribute key, value
56
71
  end
57
72
 
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
73
  if @object.valid?
67
74
  @object.save!
68
75
 
69
- puts ". Saved instance: #{@clazz} #{@object.to_param}"
76
+ if @reference.nil?
77
+ puts ". Saved new instance: #{@clazz} #{@object.to_param}"
78
+ else
79
+ puts ". Saved existing instance: #{@clazz} #{@object.to_param}"
80
+ end
70
81
  else
71
82
  puts "! Invalid instance: #{@object.to_param}"
72
83
  # puts "#{@name}:"
@@ -86,6 +97,8 @@ module Garden
86
97
 
87
98
  def map_attribute(key, value)
88
99
  return if value.nil?
100
+
101
+ # puts "map_attribute: #{key} >> #{value}"
89
102
 
90
103
  if reflection = reflection_for(key)
91
104
  # There is an assocation for this column.
@@ -124,8 +137,22 @@ module Garden
124
137
  return
125
138
  end
126
139
 
140
+ if key.to_s =~ /\./
141
+ # Dealing with something like an interpolatable value. A property of an associated model.
142
+ # puts "Interpolated association value!!" + " >> " + "@object.#{key} = #{value}"
143
+ # puts "Assignment via eval"
144
+ # puts "@object.#{key} = '#{value.gsub(/[']/, '\\\\\'')}'"
145
+ assoc = key.to_s.split('.').first
146
+ if @object.send(assoc.to_sym).nil?
147
+ # build the association if it hasn't already been built.
148
+ @object.send("build_#{assoc}".to_sym)
149
+ end
150
+ eval "@object.#{key} = '#{value.gsub(/[']/, '\\\\\'')}'"
151
+ return
152
+ end
127
153
 
128
154
  if @object.respond_to?(key.to_sym)
155
+ # puts "Assignment via respond_to?"
129
156
  # puts "Directly setting. #{key}"
130
157
  @object.send "#{key}=", value
131
158
  return
@@ -31,6 +31,7 @@ module Garden
31
31
  end
32
32
 
33
33
  def initialize namish
34
+ @instance_options ||= {}
34
35
 
35
36
  @clazz = Table.find_model_class namish
36
37
 
@@ -55,12 +56,12 @@ module Garden
55
56
  end
56
57
 
57
58
  def parse_headers array
58
- @headers = array.map { |header| header.to_s.parameterize.underscore }
59
- # @relationships = []
60
- #
61
- # @headers.each do |header|
62
- # @relationships.push header if @clazz.reflections.keys.include?(header.to_sym)
63
- # end
59
+ # @headers = array.map { |header| header.to_s.parameterize.underscore }
60
+ @headers = array.map { |header| header.to_s.underscore }
61
+ end
62
+
63
+ def reference_by col_name
64
+ @instance_options[:reference] = col_name
64
65
  end
65
66
 
66
67
  # def relationships
@@ -68,7 +69,7 @@ module Garden
68
69
  # end
69
70
  #
70
71
  def create_instance attributes
71
- Instance.new @clazz, attributes
72
+ Instance.new @clazz, attributes, @instance_options.dup
72
73
  end
73
74
 
74
75
 
@@ -13,7 +13,10 @@ module Garden
13
13
  end
14
14
 
15
15
  def open filepath
16
- puts "Planting spreadsheet: #{filepath}"
16
+ message = "Planting spreadsheet: #{filepath}"
17
+ message += " (only these worksheets: #{@options[:only].to_s})" if @options[:only]
18
+ message += " (worksheet #{@options[:worksheet].to_s})" if @options[:worksheet]
19
+ puts message
17
20
 
18
21
  @ss = Spreadsheet.open filepath
19
22
  worksheet_names = @options[:only] || @options[:worksheet] || @ss.worksheets.collect { |table| table.name }
@@ -37,6 +40,11 @@ module Garden
37
40
 
38
41
  # Get the headers. These values will be the attribute names
39
42
  headers = table_mediator.parse_headers table.first.to_a
43
+
44
+ # Set reference column if defined.
45
+ if @options.has_key?(:reference)
46
+ table_mediator.reference_by(@options[:reference])
47
+ end
40
48
 
41
49
  # Now loop the table rows, inserting records.
42
50
  table.each do |row|
@@ -1,3 +1,3 @@
1
1
  module Garden
2
- VERSION = "0.0.11.pre"
2
+ VERSION = "0.0.12.pre"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garden
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11.pre
4
+ version: 0.0.12.pre
5
5
  prerelease: 7
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-12 00:00:00.000000000Z
12
+ date: 2011-10-19 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: spreadsheet
16
- requirement: &70222477948380 !ruby/object:Gem::Requirement
16
+ requirement: &70284367764260 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70222477948380
24
+ version_requirements: *70284367764260
25
25
  description: ! 'Allows you to organize your seeds in different formats. Typical seeds.rb,
26
26
  yaml fixtures, and a variety of spreadsheet formats. '
27
27
  email: