sprig 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c09e094cca99e32c1042adb111565a9f79f321f2
4
- data.tar.gz: 056a2e3a6ac418608dbbbd99dd905f36b8d0b39b
3
+ metadata.gz: a11f717b26ca547b63e3a10d77e90e5fb0b34f99
4
+ data.tar.gz: 875a60cf93f12dda5e3cf04f17dca18c1201f53c
5
5
  SHA512:
6
- metadata.gz: 694d572f95471fde7c7141d97c5e4cd86826791a61e6cd833fd5c54094cb63273ca7d157756c248de35e5b2b5fb35891a4b6bc9064749867a72cdb6c98115879
7
- data.tar.gz: 3c3bd1e7530e21baec6cf9b616d271a05aa4ce1fbd2263d5404229121d391778f745113cb4293d8da6bcf2c3a67429b8ba44f9b303329d4d386008fb42bf4154
6
+ metadata.gz: 3c5b80e9fbc98aeca33b2f535336a4ac577f4837ecc88b5315351f5cef5582bf67b6fad52a4af4b706c045bdb3120461c1c74b5e118eb177406f66a824e5cf99
7
+ data.tar.gz: 7660d42b5f0ca71849c63de2abaef6cc4ad324ee08598b00676b4196c80050eeb49264589258e601504ad5bac97514edfe42d5ec0624dbdb87193a903d0db875
@@ -9,46 +9,75 @@ module Sprig
9
9
  end
10
10
 
11
11
  def dependencies
12
- @dependencies ||= determine_dependencies.uniq
12
+ @dependencies ||= find_dependencies_within(raw_value).uniq
13
13
  end
14
14
 
15
15
  def value
16
- compute_value if @value.nil?
16
+ @value = compute_value(raw_value) if @value.nil?
17
17
 
18
18
  @value
19
19
  end
20
20
 
21
21
  private
22
22
 
23
- def determine_dependencies
24
- if computed_value?
25
- matches = raw_value.scan(/(sprig_record\(([A-Z][^,]*), ([\d]*)\))+/)
26
- matches.map {|match| Dependency.for(match[1], match[2]) }
23
+ def find_dependencies_within(value)
24
+ if array?(value)
25
+ find_dependencies_within_array(value)
26
+ elsif string?(value) && computed_value?(value)
27
+ find_dependencies_within_string(value)
27
28
  else
28
29
  []
29
30
  end
30
31
  end
31
32
 
32
- def string?
33
- raw_value.is_a?(String)
33
+ def find_dependencies_within_array(array)
34
+ array.flat_map do |value|
35
+ find_dependencies_within(value)
36
+ end
37
+ end
38
+
39
+ def find_dependencies_within_string(string)
40
+ matches = string.scan(/(sprig_record\(([A-Z][^,]*), ([\d]*)\))+/)
41
+ matches.map { |match| Dependency.for(match[1], match[2]) }
42
+ end
43
+
44
+ def string?(value)
45
+ value.is_a?(String)
46
+ end
47
+
48
+ def array?(value)
49
+ value.is_a?(Array)
34
50
  end
35
51
 
36
- def computed_value?
37
- string? && raw_value =~ computed_value_regex
52
+ def computed_value?(value)
53
+ String(value) =~ computed_value_regex
38
54
  end
39
55
 
40
56
  def computed_value_regex
41
57
  /<%[=]?(.*)%>/
42
58
  end
43
59
 
44
- def compute_value
45
- @value = if computed_value?
46
- matches = computed_value_regex.match(raw_value)
47
- eval(matches[1])
60
+ def compute_value(value)
61
+ if array?(value)
62
+ compute_array_value(value)
63
+ elsif string?(value) && computed_value?(value)
64
+ compute_string_value(value)
48
65
  else
49
- raw_value
66
+ value
50
67
  end
51
68
  end
69
+
70
+ def compute_array_value(array)
71
+ array.map do |value|
72
+ compute_value(value)
73
+ end
74
+ end
75
+
76
+ def compute_string_value(string)
77
+ matches = computed_value_regex.match(string)
78
+ eval(matches[1])
79
+ end
80
+
52
81
  end
53
82
  end
54
83
  end
@@ -1,3 +1,3 @@
1
1
  module Sprig
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
Binary file
@@ -1,5 +1,7 @@
1
1
  class Post < ActiveRecord::Base
2
2
 
3
+ has_and_belongs_to_many :tags
4
+
3
5
  def photo=(file)
4
6
  write_attribute(:photo, File.basename(file.path))
5
7
  end
@@ -0,0 +1,3 @@
1
+ class Tag < ActiveRecord::Base
2
+ validates :name, :presence => true
3
+ end
@@ -0,0 +1,7 @@
1
+ records:
2
+ - sprig_id: 1
3
+ title: 'Yaml title'
4
+ content: 'Yaml content'
5
+ tag_ids:
6
+ - '<%= sprig_record(Tag, 1).id %>'
7
+ - '<%= sprig_record(Tag, 2).id %>'
@@ -0,0 +1,5 @@
1
+ records:
2
+ - sprig_id: 1
3
+ name: 'Botany'
4
+ - sprig_id: 2
5
+ name: 'Biology'
@@ -58,6 +58,12 @@ Post.connection.execute "CREATE TABLE posts (id INTEGER PRIMARY KEY AUTOINCREMEN
58
58
  Comment.connection.execute "DROP TABLE IF EXISTS comments;"
59
59
  Comment.connection.execute "CREATE TABLE comments (id INTEGER PRIMARY KEY , post_id INTEGER, body VARCHAR(255));"
60
60
 
61
+ Tag.connection.execute "DROP TABLE IF EXISTS tags;"
62
+ Tag.connection.execute "CREATE TABLE tags (id INTEGER PRIMARY KEY , name VARCHAR(255));"
63
+
64
+ Tag.connection.execute "DROP TABLE IF EXISTS posts_tags;"
65
+ Tag.connection.execute "CREATE TABLE posts_tags (id INTEGER PRIMARY KEY , post_id INTEGER, tag_id INTEGER);"
66
+
61
67
  # Helpers
62
68
  #
63
69
  # Setup fake `Rails.root`
@@ -235,6 +235,24 @@ describe "Seeding an application" do
235
235
  end
236
236
  end
237
237
 
238
+ context "with has_and_belongs_to_many relationships" do
239
+ around do |example|
240
+ load_seeds('posts_with_habtm.yml', 'tags.yml', &example)
241
+ end
242
+
243
+ it "saves the habtm relationships" do
244
+ sprig [
245
+ Tag,
246
+ {
247
+ :class => Post,
248
+ :source => open('spec/fixtures/seeds/test/posts_with_habtm.yml')
249
+ }
250
+ ]
251
+
252
+ Post.first.tags.map(&:name).should == ['Botany', 'Biology']
253
+ end
254
+ end
255
+
238
256
  context "with cyclic dependencies" do
239
257
  around do |example|
240
258
  load_seeds('comments.yml', 'posts_with_cyclic_dependencies.yml', &example)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sprig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lawson Kurtz
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-25 00:00:00.000000000 Z
12
+ date: 2014-06-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -201,6 +201,7 @@ files:
201
201
  - spec/fixtures/config/environments/test.rb
202
202
  - spec/fixtures/models/comment.rb
203
203
  - spec/fixtures/models/post.rb
204
+ - spec/fixtures/models/tag.rb
204
205
  - spec/fixtures/models/user.rb
205
206
  - spec/fixtures/seeds/staging/posts.yml
206
207
  - spec/fixtures/seeds/test/comments.yml
@@ -219,6 +220,8 @@ files:
219
220
  - spec/fixtures/seeds/test/posts_missing_record.yml
220
221
  - spec/fixtures/seeds/test/posts_with_cyclic_dependencies.yml
221
222
  - spec/fixtures/seeds/test/posts_with_files.yml
223
+ - spec/fixtures/seeds/test/posts_with_habtm.yml
224
+ - spec/fixtures/seeds/test/tags.yml
222
225
  - spec/lib/generators/sprig/install_generator_spec.rb
223
226
  - spec/lib/sprig/configuration_spec.rb
224
227
  - spec/lib/sprig/directive_list_spec.rb
@@ -268,6 +271,7 @@ test_files:
268
271
  - spec/fixtures/config/environments/test.rb
269
272
  - spec/fixtures/models/comment.rb
270
273
  - spec/fixtures/models/post.rb
274
+ - spec/fixtures/models/tag.rb
271
275
  - spec/fixtures/models/user.rb
272
276
  - spec/fixtures/seeds/staging/posts.yml
273
277
  - spec/fixtures/seeds/test/comments.yml
@@ -286,6 +290,8 @@ test_files:
286
290
  - spec/fixtures/seeds/test/posts_missing_record.yml
287
291
  - spec/fixtures/seeds/test/posts_with_cyclic_dependencies.yml
288
292
  - spec/fixtures/seeds/test/posts_with_files.yml
293
+ - spec/fixtures/seeds/test/posts_with_habtm.yml
294
+ - spec/fixtures/seeds/test/tags.yml
289
295
  - spec/lib/generators/sprig/install_generator_spec.rb
290
296
  - spec/lib/sprig/configuration_spec.rb
291
297
  - spec/lib/sprig/directive_list_spec.rb