ez_import 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .idea
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ezimport.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Joe Korzeniewski
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # EzImport: Easy XML Import and Export From ActiveRecord
2
+
3
+ Have you ever been irritated with keeping ActiveRecord models consistent across different developers or server instances, but didn't want to waste all the time maintaining fixtures? Now importing and exporting is easy.
4
+
5
+ EzImport allows you to export and import data from models while keeping the ID's consistent. Now you can make your adjustments to the database directly and freeze those changes in version control.
6
+
7
+ ## Installation
8
+
9
+ gem 'ez_import'
10
+
11
+ ## Usage
12
+
13
+ To export a single model:
14
+
15
+ $ rake ez_export model_name
16
+
17
+ To export a multiple models:
18
+
19
+ $ rake ez_export model_name another_model third_model
20
+
21
+ To import a single model:
22
+
23
+ $ rake ez_import model_name
24
+
25
+ To export a multiple models:
26
+
27
+ $ rake ez_import model_name another_model third_model
28
+
29
+ ## Batch Imports
30
+
31
+ You may want to sync up the same group of models from time to time without having to specify each on the command line. For that, you'd use batch imports.
32
+
33
+ First, generate an initializer (config/initializers/ez_import.rb):
34
+
35
+ $ rails g ez_import:config user_type subscription_type
36
+
37
+ Now you can import and export automatically from this list:
38
+
39
+ $ rake ez_export:all
40
+
41
+ $ rake ez_import:all
42
+
43
+
44
+ ## Custom callbacks
45
+
46
+ There may be a situation where you want to do something after an import in order to fix it in the database. A good example of this is when you are importing something with a counter cache. The counter cache gets imported and then incremented for each record after it. This is no good. The solution is to define a callback class method in your model. It must be named after_ezimport.
47
+
48
+ def self.after_ezimport
49
+ Post.all.each do |p|
50
+ Post.reset_counters(p.id, :comments)
51
+ end
52
+ end
53
+
54
+
55
+ ## Copyright
56
+
57
+ Copyright (c) 2013 Joe Korzeniewski. See LICENSE.txt for
58
+ further details.
59
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/ez_import.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ez_import/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "ez_import"
8
+ gem.version = EzImport::VERSION
9
+ gem.authors = ["Joe Korzeniewski"]
10
+ gem.email = ["trogdor33@gmail.com"]
11
+ gem.description = %q{Ezimport reads and writes XML representations of data stored in rails models while preserving IDs.}
12
+ gem.summary = %q{Easily import and export static data from rails}
13
+ gem.homepage = "https://github.com/jkorz/ezimport"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "hpricot"
21
+ end
@@ -0,0 +1,63 @@
1
+
2
+ module EzImport
3
+ require 'hpricot'
4
+ class Main
5
+
6
+ @@models = []
7
+ @@xmlpath = "db/ez_import"
8
+
9
+ cattr_accessor :models
10
+ cattr_accessor :xmlpath
11
+
12
+ def self.export(model_name_original)
13
+ model_name, model_obj = self.get_model(model_name_original)
14
+ raise "Model '#{model_name_original}' not found" if model_obj.nil?
15
+ unless File.directory?(@@xmlpath)
16
+ Dir.mkdir(@@xmlpath)
17
+ puts "create #{@@xmlpath}"
18
+ end
19
+ filepath = "#{@@xmlpath}/#{model_name.underscore.pluralize}.xml"
20
+ File.open(filepath, 'w') {|f| f << model_obj.all.to_xml}
21
+ puts "write #{filepath}"
22
+ end
23
+
24
+ def self.get_model(model_name)
25
+ model_name = model_name.downcase.gsub('_','')
26
+ Dir.glob("#{Rails.root}/app/models/**/*rb").each{|m| require_or_load m }
27
+ model_list = {}
28
+ ActiveRecord::Base.subclasses.each {|m| model_list[m.to_s.downcase] = m}
29
+ m1 = model_list[model_name].to_s.underscore
30
+ m2 = model_list[model_name]
31
+ [m1, m2]
32
+ end
33
+
34
+ def self.import(model_name_original)
35
+ model_name, model = self.get_model(model_name_original)
36
+ raise "Model '#{model_name_original}' not found" if model.nil?
37
+ model.delete_all
38
+ puts "\n"
39
+ puts "Load #{model_name.pluralize}.xml"
40
+ file_content = File.read("#{@@xmlpath}/#{model_name.pluralize}.xml")
41
+ file_content.gsub!("&amp;", "&")
42
+ count = 0
43
+ Hpricot(file_content).search(model_name.gsub('_', '-')).each do |record|
44
+ model.new do |new_instance|
45
+ model.columns.each do |col|
46
+ n = col.name.gsub('_', '-')
47
+ eval('new_instance.' + col.name + " = (record/n).innerHTML")
48
+ end
49
+ new_instance.save
50
+ end
51
+ count += 1
52
+ end
53
+ puts "\t#{count} records loaded"
54
+ if model.methods.include?(:after_ezimport)
55
+ puts "Running #{model_name}.after_ezimport..."
56
+ model.after_ezimport
57
+ end
58
+ end
59
+
60
+
61
+ end
62
+
63
+ end
@@ -0,0 +1,11 @@
1
+ require 'ez_import'
2
+ require 'rails'
3
+ module EzImport
4
+ class Railtie < Rails::Railtie
5
+ railtie_name :ezimport
6
+
7
+ rake_tasks do
8
+ load "tasks/ez_import.rake"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module EzImport
2
+ VERSION = "0.1.1"
3
+ end
data/lib/ez_import.rb ADDED
@@ -0,0 +1,23 @@
1
+ require "ez_import/version"
2
+ require "ez_import/main"
3
+ require "ez_import/railtie.rb"
4
+
5
+ module EzImport
6
+
7
+ def self.models
8
+ Main.models
9
+ end
10
+
11
+ def self.models=(m)
12
+ Main.models = m
13
+ end
14
+
15
+ def self.xmlpath
16
+ Main.xmlpath
17
+ end
18
+
19
+ def self.xmlpath=(p)
20
+ Main.xmlpath = p
21
+ end
22
+
23
+ end
@@ -0,0 +1,22 @@
1
+ module EzImport
2
+
3
+ module Generators
4
+ class ConfigGenerator < Rails::Generators::Base
5
+ argument :models, :type => :array, :default => [], :banner => "ModelName1 ModelName2 etc..."
6
+
7
+ desc "Creates an initializer for EzImport at /config/initializers/ez_import.rb"
8
+
9
+ def self.source_root
10
+ File.expand_path("../templates", __FILE__)
11
+ end
12
+
13
+ def create_initializer_file
14
+ unless models.empty?
15
+ @models = models.to_s
16
+ end
17
+ template 'initializer.rb.erb', File.join('config', 'initializers', 'ez_import.rb')
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,15 @@
1
+ #define the models that will be imported/exported with ez_import:all and ez_export:all
2
+
3
+ #Models: this is an array of model names used to import/export all models
4
+ # YOU MUST UNCOMMENT THIS AND CHANGE IT FOR ez_import:all AND ez_export:all TO FUNCTION!
5
+ <% if @models.length > 0 -%>
6
+ <%= "EzImport.models = #{@models}" -%>
7
+ <% else -%>
8
+ #EzImport.models = ['ModelName1', 'ModelName2']
9
+ <% end -%>
10
+
11
+
12
+ #XML Path: this is where the xml files will be stored
13
+ #Uncomment the line below to customize
14
+
15
+ # EzImport.xmlpath = "db/ez_import"
@@ -0,0 +1,46 @@
1
+ desc "Exports a specific model or models to xml"
2
+ task :ez_export => :environment do
3
+ ARGV.shift
4
+ exported = false
5
+ while name = ARGV.shift
6
+ EzImport::Main.export(name)
7
+ task name.to_sym do ; end
8
+ exported = true
9
+ end
10
+ unless exported
11
+ puts "Usage: \n rake ez_export model1 model2 model3 etc..."
12
+ end
13
+ end
14
+
15
+ desc "Imports a specific model or models from saved xml"
16
+ task :ez_import => :environment do
17
+ ARGV.shift
18
+ imported = false
19
+ while name = ARGV.shift
20
+ EzImport::Main.import(name)
21
+ task name.to_sym do ; end
22
+ imported = true
23
+ end
24
+ unless imported
25
+ puts "Usage: \n rake ez_import model1 model2 model3 etc..."
26
+ end
27
+ end
28
+
29
+ namespace :ez_import do
30
+
31
+ desc "Imports all models defined in config/initializers/ez_import.rb"
32
+ task :all => :environment do
33
+ EzImport::Main.models.each do |model|
34
+ EzImport::Main.import(model)
35
+ end
36
+ end
37
+ end
38
+
39
+ namespace :ez_export do
40
+ desc "Exports all models defined in config/initializers/ez_import.rb"
41
+ task :all => :environment do
42
+ EzImport::Main.models.each do |model|
43
+ EzImport::Main.export(model)
44
+ end
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ez_import
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Joe Korzeniewski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hpricot
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Ezimport reads and writes XML representations of data stored in rails
31
+ models while preserving IDs.
32
+ email:
33
+ - trogdor33@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - ez_import.gemspec
44
+ - lib/ez_import.rb
45
+ - lib/ez_import/main.rb
46
+ - lib/ez_import/railtie.rb
47
+ - lib/ez_import/version.rb
48
+ - lib/generators/ez_import/config/config_generator.rb
49
+ - lib/generators/ez_import/config/templates/initializer.rb.erb
50
+ - lib/tasks/ez_import.rake
51
+ homepage: https://github.com/jkorz/ezimport
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.24
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Easily import and export static data from rails
75
+ test_files: []