garden 0.0.1 → 0.0.2.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,23 +5,33 @@ module Garden
5
5
  def self.parse_table_name namish
6
6
  namish.parameterize.underscore
7
7
  end
8
- def self.get_instance table_name, id
8
+ def self.get_instance table, id
9
9
 
10
+ model_name = table.is_a?(ActiveRecord::Reflection::AssociationReflection) ? table.class_name : table.to_s.classify
11
+
10
12
  # puts "Find #{id} from #{table_name}"
11
13
  # puts id.class.to_s
12
14
 
13
15
  begin
14
- clazz = table_name.to_s.classify.constantize
16
+ clazz = model_name.constantize
15
17
  raise "Class #{clazz.to_s} is not an ActiveRecord subclass." unless clazz.new.is_a?(ActiveRecord::Base)
16
- instance = clazz.find id.to_s
18
+ instance = clazz.find(id.to_s)
19
+
20
+ # puts "!! Created clazz #{clazz.to_s} from model_name. No problem. #{instance.class.to_s} #{instance.to_param}"
21
+
22
+ return instance
23
+ rescue ActiveRecord::RecordNotFound => e
24
+
25
+ instance = clazz.find_by_name(id.to_s) if instance.nil? && clazz.respond_to?(:find_by_name)
26
+ # puts "++Find #{clazz.to_s} by title: #{id}" if instance.nil? && clazz.respond_to?(:find_by_title)
27
+ instance = clazz.find_by_title(id.to_s) if instance.nil? && clazz.respond_to?(:find_by_title)
28
+ # puts "++#{instance}" if instance
17
29
  return instance
18
30
  rescue Exception => e
19
- puts "Could not find #{id} from table #{table_name}: #{e.message}"
31
+ puts "Could not find #{id} from table #{model_name}: #{e.message}"
20
32
  return nil
21
33
  end
22
34
  end
23
-
24
-
25
35
 
26
36
  def initialize namish
27
37
 
@@ -45,31 +55,34 @@ module Garden
45
55
 
46
56
  def parse_headers array
47
57
  @headers = array.map { |header| header.to_s.parameterize.underscore }
48
- @relationships = []
49
-
50
- @headers.each do |header|
51
- @relationships.push header if @clazz.reflections.keys.include?(header.to_sym)
52
- end
53
- end
54
-
55
-
56
- def relationships
57
- @relationships
58
+ # @relationships = []
59
+ #
60
+ # @headers.each do |header|
61
+ # @relationships.push header if @clazz.reflections.keys.include?(header.to_sym)
62
+ # end
58
63
  end
59
64
 
65
+ # def relationships
66
+ # @relationships
67
+ # end
68
+ #
60
69
  def create_instance attributes
61
- relationships = @relationships
70
+ # relationships = @relationships
62
71
 
63
-
64
- attributes = parse_row_relationships attributes, relationships
72
+ all_keys = attributes.keys
65
73
 
66
74
  instance = @clazz.new
75
+ parse_row_relationships instance, attributes#, relationships
67
76
 
68
- rejected = {}
69
- attributes.each_key do |key|
70
- rejected[key] = attributes.delete(key) if !instance.attributes.include?(key.to_s) && !relationships.include?(key.to_s)
71
- end
77
+ # puts "A: #{attributes.keys}"
72
78
 
79
+ # rejected = {}
80
+ # attributes.each_key do |key|
81
+ # rejected[key] = attributes.delete(key) if !instance.attributes.include?(key.to_s)# && !relationships.include?(key.to_s)
82
+ # end
83
+
84
+
85
+ attributes.delete_if do |key, value| !instance.attributes.include?(key.to_s) end
73
86
  instance.attributes = attributes
74
87
 
75
88
  # instance.id = rejected[:id] if rejected.key?(:id)
@@ -86,21 +99,53 @@ module Garden
86
99
  puts "Invalid instance."
87
100
  puts "#{@name}:"
88
101
  puts " - Valid attributes: #{attributes.keys}"
89
- puts " - Invalid attributes: #{rejected.keys}"
90
- puts " - Association attributes: #{relationships}"
102
+ puts " - Excel sheet keys: #{all_keys}"
103
+ # puts " - Invalid attributes: #{rejected.keys}"
104
+ # puts " - Association attributes: #{relationships}"
91
105
  puts " - #{instance.errors}"
92
106
  end
93
107
  end
94
108
 
95
- def parse_row_relationships hash, relationships
96
- relationships.each do |r|
97
- # puts "Parsing row relationship: #{hash[r.to_sym]}"
98
- instance = get_instance r, hash[r.to_sym]
99
- hash[r.to_sym] = instance
109
+ def parse_row_relationships instance, hash#, relationships
110
+ @clazz.reflections.each_value do |r|
111
+ next unless hash.has_key?(r.name)
112
+ # Remove the value from the hash.
113
+ value = hash.delete(r.name.to_sym)
114
+ case r.macro
115
+ when :has_many
116
+ parse_has_many(instance, r, value)
117
+ when :belongs_to
118
+ parse_belongs_to(instance, r, value)
119
+ end
100
120
  end
101
- hash
121
+
122
+ # relationships.each do |r|
123
+ # # puts "Parsing row relationship: #{hash[r.to_sym]}"
124
+ # instance = get_instance r, hash[r.to_sym]
125
+ # hash[r.to_sym] = instance
126
+ # end
127
+ # hash
128
+ end
129
+
130
+
131
+ def parse_has_many instance, r, value
132
+ # puts " // Parsing #{r.name}, #{value}"
133
+ ids = value.split(/\s*,\s*/)
134
+ ids.each do |id|
135
+ related_instance = Table.get_instance r.class_name, id
136
+ # puts ">> Creating a has-many relationship. #{id}, #{related_instance}"
137
+ unless related_instance.nil?
138
+ instance.send(r.name.to_sym) << related_instance
139
+ end
140
+ end
141
+ end
142
+
143
+ def parse_belongs_to instance, r, value
144
+ ri = Table.get_instance(r, value)
145
+ instance.attributes = { r.name.to_sym => ri }
146
+
147
+ # puts "Parsing Belongs_To. #{instance.class} #{instance.to_param} --- #{r.name.to_sym} --- #{ri.class} #{ri.to_param}"
102
148
  end
103
-
104
149
 
105
150
  end
106
151
  end
@@ -1,3 +1,3 @@
1
1
  module Garden
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2.pre"
3
3
  end
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garden
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.0.2.pre
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - amoslanka
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-26 00:00:00.000000000Z
12
+ date: 2011-08-29 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: spreadsheet
16
- requirement: &70240349441300 !ruby/object:Gem::Requirement
16
+ requirement: &70239804122580 !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: *70240349441300
24
+ version_requirements: *70239804122580
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:
@@ -54,9 +54,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
54
54
  required_rubygems_version: !ruby/object:Gem::Requirement
55
55
  none: false
56
56
  requirements:
57
- - - ! '>='
57
+ - - ! '>'
58
58
  - !ruby/object:Gem::Version
59
- version: '0'
59
+ version: 1.3.1
60
60
  requirements: []
61
61
  rubyforge_project: garden
62
62
  rubygems_version: 1.8.6