superseeder 0.9.2 → 0.9.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 48b9be1f436d7ee497deb42090c6f44a37dbec8b
4
- data.tar.gz: fffa66f6b4ff0183a9d0b41aaf3063796d182841
3
+ metadata.gz: 5ac1535106365fed57579024bed2fffd848749ce
4
+ data.tar.gz: ce9deb018e56f473cc7ddfbb1c84194064cb05c6
5
5
  SHA512:
6
- metadata.gz: da5f5ac3f1e05524e921259f063c3d5c74619679263947403da8f4c66affd82f2c34e50ca33cd0450b743b2f23839f299d06750570d69c631f645883638b8a3e
7
- data.tar.gz: 6827ee70a77780a5e9e9364affc9a1753ad7ebea13a5fc3a5a50584c08f850dd38c68c4132de82df2fe7fae2b966baf7a0c345facc6cb0c5d3ce369275636f4d
6
+ metadata.gz: 2474335519980b1a92432b879876e62b34f29b58670758943e12535bbf2e0863c2fbdbc485170fff26e6e5989f11471d07ffbcc2fc2ef627eaee529027de6101
7
+ data.tar.gz: 941980a27de9ee42dd0d6e0c4a36f9a3a6eaab69b447e25334c66314180d6e1fa18e1867593f403b5e61612ba391554714fcfef8104b8397baf34ef2bd7fb396
data/Rakefile CHANGED
@@ -14,19 +14,24 @@ RDoc::Task.new(:rdoc) do |rdoc|
14
14
  rdoc.rdoc_files.include('lib/**/*.rb')
15
15
  end
16
16
 
17
-
18
-
19
-
20
17
  Bundler::GemHelper.install_tasks
21
18
 
22
19
  require 'rake/testtask'
23
20
 
24
21
  Rake::TestTask.new(:test) do |t|
25
- t.libs << 'lib'
26
- t.libs << 'test'
27
- t.pattern = 'test/**/*_test.rb'
28
- t.verbose = false
22
+ if ENV['ADAPTER'].nil?
23
+ require 'superseeder/adapter'
24
+ ::Superseeder::Adapter.adapters.each do |adapter|
25
+ ENV['ADAPTER'] = adapter
26
+ Rake::Task['test'].execute
27
+ end
28
+ else
29
+ puts "Testing with #{ENV['ADAPTER']} adapter..."
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
29
35
  end
30
36
 
31
-
32
37
  task default: :test
@@ -0,0 +1,30 @@
1
+ module Superseeder
2
+ class Adapter
3
+
4
+
5
+ def self.adapters
6
+ %w(active_record mongoid)
7
+ end
8
+
9
+ def initialize(instance)
10
+ @instance = instance
11
+ if defined? ActiveRecord::Base
12
+ if instance.kind_of? ActiveRecord::Base
13
+ require 'superseeder/adapters/active_record'
14
+ self.singleton_class.include ::Superseeder::Adapters::ActiveRecord
15
+ end
16
+ end
17
+ if defined? Mongoid::Document
18
+ if instance.kind_of? Mongoid::Document
19
+ require 'superseeder/adapters/mongoid'
20
+ self.singleton_class.include ::Superseeder::Adapters::Mongoid
21
+ end
22
+ end
23
+ end
24
+
25
+ protected
26
+
27
+ attr_reader :instance
28
+
29
+ end
30
+ end
@@ -0,0 +1,31 @@
1
+ module Superseeder
2
+ module Adapters
3
+ module ActiveRecord
4
+
5
+ def each_relation
6
+ self.instance.singleton_class.reflections.each do |key, val|
7
+ yield key, self.is_array_relation?(val), val.class_name.constantize
8
+ end
9
+ end
10
+
11
+ def each_field(row)
12
+ self.instance.attributes.each do |field, val|
13
+ next unless row.key? field
14
+ yield field, false #self.is_array_field?(options) TODO inquiry
15
+ end
16
+ end
17
+
18
+ protected
19
+
20
+ def is_array_field?(options)
21
+ options.type == Array
22
+ end
23
+
24
+ def is_array_relation?(reflection)
25
+ [::ActiveRecord::Reflection::HasManyReflection,
26
+ ::ActiveRecord::Reflection::HasAndBelongsToManyReflection].any?{ |c| reflection.kind_of? c }
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ module Superseeder
2
+ module Adapters
3
+ module Mongoid
4
+
5
+ def each_relation
6
+ self.instance.relations.each do |key, val|
7
+ yield key, self.is_array_relation?(val), val.class_name.constantize
8
+ end
9
+ end
10
+
11
+ def each_field(row)
12
+ self.instance.fields.select{ |f, o| row.key? o.options[:as] || f }.each do |field, options|
13
+ yield options.options[:as] || field, self.is_array_field?(options)
14
+ end
15
+ end
16
+
17
+ protected
18
+
19
+ def is_array_field?(options)
20
+ options.type == Array
21
+ end
22
+
23
+ def is_array_relation?(options)
24
+ [::Mongoid::Relations::Referenced::Many,
25
+ ::Mongoid::Relations::Embedded::Many,
26
+ ::Mongoid::Relations::Referenced::ManyToMany].include? options[:relation]
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -33,29 +33,29 @@ module Superseeder
33
33
  row['_type'].constantize
34
34
  end.new
35
35
 
36
+ require 'superseeder/adapter'
37
+ adapter = ::Superseeder::Adapter.new instance
38
+
36
39
  # Set relations
37
- instance.class.reflections.each do |key, val|
38
- attrs = row.select{ |k, _| k =~ /\A#{key}_/ }
40
+ adapter.each_relation do |name, is_array, class_name|
41
+ attrs = row.select{ |k, _| k =~ /\A#{name}_/ }
39
42
  next if attrs.empty?
40
- attrs = attrs.inject({}){ |h, (k, v)| h[k.sub /\A#{key}_/, ''] = v; h }
41
- if [Mongoid::Relations::Referenced::Many,
42
- Mongoid::Relations::Embedded::Many,
43
- Mongoid::Relations::Referenced::ManyToMany].include? val[:relation]
43
+ attrs = attrs.inject({}){ |h, (k, v)| h[k.sub /\A#{name}_/, ''] = v; h }
44
+ if is_array
44
45
  vals = attrs.map do |k, v|
45
- v.split(many_sep).map{ val.class_name.constantize.find_by k => v } unless v.nil?
46
+ v.split(many_sep).map{ |u| class_name.find_by k => u } unless v.nil?
46
47
  end
47
48
  vals.flatten!
48
49
  vals.compact!
49
- instance.send "#{key}=", vals
50
+ instance.send "#{name}=", vals
50
51
  else
51
- instance.send "#{key}=", val.class_name.constantize.find_by(attrs)
52
+ instance.send "#{name}=", class_name.find_by(attrs)
52
53
  end
53
54
  end
54
55
 
55
- # Set attributes
56
- instance.fields.select{ |f, o| row.key? o.options[:as] || f }.each do |f, o|
57
- name = o.options[:as] || f
58
- val = if o.type == Array
56
+ # Set fields
57
+ adapter.each_field(row) do |name, is_array|
58
+ val = if is_array
59
59
  row[name].try(:split, many_sep)
60
60
  else
61
61
  row[name]
@@ -66,7 +66,7 @@ module Superseeder
66
66
  if instance.valid?
67
67
  instance.save
68
68
  else
69
- logger.debug "Skipped #{row} : #{instance.errors.full_messages}"
69
+ Rails.logger.debug "Skipped #{row} : #{instance.errors.full_messages}"
70
70
  end
71
71
  end
72
72
  end
@@ -2,8 +2,12 @@ module Superseeder
2
2
  module Seedable
3
3
 
4
4
  def seed(*args, &block)
5
- opts = args.extract_options!
6
- path = Rails.root.join('db', 'seeds', 'data')
5
+ opts = args.extract_options!
6
+ path = if opts[:path].blank?
7
+ Rails.root.join('db', 'seeds', 'data')
8
+ else
9
+ Pathname(opts.delete :path)
10
+ end
7
11
  filename = opts.delete :filename
8
12
  path = if filename.blank?
9
13
  tmp = path.join "#{self.name.underscore.pluralize}*"
@@ -12,18 +16,20 @@ module Superseeder
12
16
  raise ArgumentError.new "No candidate files for #{self.name}. Either create a #{tmp} file or specify the filename you want to use with the :filename option." if candidates.length == 0
13
17
  candidates.first
14
18
  else
15
- path.join filename
19
+ tmp = path.join filename
20
+ raise ArgumentError.new "#{filename} does not exist." unless File.exist? tmp
21
+ tmp
16
22
  end
17
23
  ext = File.extname path
18
24
  Dir[Pathname(Gem::Specification.find_by_name('superseeder').gem_dir).join('lib','superseeder','formats','*.rb')].each{ |f| require(f) }
19
25
  modules = Superseeder::Formats.constants.select{ |c| Module === Superseeder::Formats.const_get(c) }.map{ |c| Superseeder::Formats.const_get(c) }
20
26
  mod = modules.find{ |m| m.send(:extensions).include? ext }
21
27
  raise ArgumentError.new "No registered module to support #{ext} extension." if mod.nil?
22
- logger.debug "Seeding #{self.name.downcase.pluralize.humanize} from #{path}..."
28
+ Rails.logger.debug "Seeding #{self.name.downcase.pluralize.humanize} from #{path}..."
23
29
  count = self.count
24
30
  self.extend mod
25
31
  self.__process path, opts, &block
26
- logger.debug "Done. (#{self.count - count} entries added)"
32
+ Rails.logger.debug "Done. (#{self.count - count} entries added)"
27
33
  end
28
34
  end
29
35
 
@@ -1,3 +1,3 @@
1
1
  module Superseeder
2
- VERSION = '0.9.2'
2
+ VERSION = '0.9.3'
3
3
  end
@@ -0,0 +1,26 @@
1
+ require 'active_record'
2
+ require 'rake'
3
+
4
+ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
5
+
6
+ class Car < ActiveRecord::Base
7
+
8
+ belongs_to :parking
9
+
10
+ validates :name, :presence => true
11
+ end
12
+
13
+ class Parking < ActiveRecord::Base
14
+
15
+ has_many :cars
16
+
17
+ validates :name, :presence => true, :uniqueness => true
18
+ end
19
+
20
+ load File.dirname(__FILE__) + '/schema.rb'
21
+ #ActiveRecord::Tasks::DatabaseTasks.migrate
22
+ #Rake.load_rakefile 'active_record/railties/databases.rake'
23
+ #require 'active_record/railties/databases.rake'
24
+ #Rake::Task['db:migrate'].invoke
25
+
26
+ require_relative 'all'
@@ -0,0 +1,31 @@
1
+ class Whatever
2
+
3
+ def self.count
4
+ 0
5
+ end
6
+
7
+ end
8
+
9
+ class Multiple
10
+
11
+ def self.count
12
+ 0
13
+ end
14
+
15
+ end
16
+
17
+ class Nofile
18
+
19
+ def self.count
20
+ 0
21
+ end
22
+
23
+ end
24
+
25
+ class Noformat
26
+
27
+ def self.count
28
+ 0
29
+ end
30
+
31
+ end
@@ -0,0 +1,25 @@
1
+ require 'mongoid'
2
+
3
+ Mongoid.load! 'test/adapters/mongoid.yml', :test
4
+ Mongoid.raise_not_found_error = false
5
+
6
+ class Car
7
+ include Mongoid::Document
8
+
9
+ field :name, :type => String
10
+
11
+ belongs_to :parking
12
+
13
+ validates :name, :presence => true
14
+ end
15
+
16
+ class Parking
17
+ include Mongoid::Document
18
+
19
+ field :name, :type => String
20
+ has_many :cars
21
+
22
+ validates :name, :presence => true, :uniqueness => true
23
+ end
24
+
25
+ require_relative 'all'
@@ -0,0 +1,6 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: superseeder_test
5
+ hosts:
6
+ - localhost:27017
@@ -0,0 +1,12 @@
1
+ ActiveRecord::Schema.define do
2
+
3
+ create_table :parkings do |t|
4
+ t.string :name
5
+ end
6
+
7
+ create_table :cars do |t|
8
+ t.string :name
9
+ t.references :parking
10
+ end
11
+
12
+ end
@@ -1,9 +1,23 @@
1
1
  require File.expand_path('../boot', __FILE__)
2
2
 
3
- require 'rails/all'
3
+ require 'rails'
4
+
5
+ %w(
6
+ action_controller
7
+ action_view
8
+ action_mailer
9
+ active_job
10
+ rails/test_unit
11
+ sprockets
12
+ ).each do |framework|
13
+ begin
14
+ require "#{framework}/railtie"
15
+ rescue LoadError
16
+ end
17
+ end
4
18
 
5
19
  Bundler.require(*Rails.groups)
6
- require "superseeder"
20
+ require 'superseeder'
7
21
 
8
22
  module Dummy
9
23
  class Application < Rails::Application
@@ -13,7 +13,7 @@ Rails.application.configure do
13
13
  config.eager_load = false
14
14
 
15
15
  # Configure static asset server for tests with Cache-Control for performance.
16
- config.serve_static_assets = true
16
+ config.serve_static_files = true
17
17
  config.static_cache_control = 'public, max-age=3600'
18
18
 
19
19
  # Show full error reports and disable caching.
@@ -34,6 +34,8 @@ Rails.application.configure do
34
34
  # Print deprecation notices to the stderr.
35
35
  config.active_support.deprecation = :stderr
36
36
 
37
+ config.active_support.test_order = :random
38
+
37
39
  # Raises error for missing translations
38
40
  # config.action_view.raise_on_missing_translations = true
39
41
  end
@@ -0,0 +1,4 @@
1
+ name,parking_name
2
+ Mustang,North
3
+ Corvette,South
4
+ BMW,South
@@ -0,0 +1,4 @@
1
+ name
2
+ Mustang
3
+ Corvette
4
+ BMW
File without changes
File without changes
@@ -0,0 +1,5 @@
1
+ name,cars_name
2
+ North,Mustang
3
+ East,
4
+ South,"Corvette,BMW"
5
+ West,
@@ -0,0 +1,5 @@
1
+ name
2
+ North
3
+ East
4
+ South
5
+ West
File without changes
File without changes