dump_to_seeds 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in dump_to_seeds.gemspec
4
+ gemspec
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Kristian Meier, (c) 2011 David Simon
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # DumpToSeeds
2
+
3
+ ## Installation
4
+
5
+ Add `dump_to_seeds` to your project's Gemfile and do `bundle install`
6
+
7
+ **NOTE:** Only tested with Rails 2.x
8
+
9
+
10
+ ## Usage
11
+
12
+ ```ruby
13
+ Author.first.to_seed([:articles => [:comments]])
14
+
15
+ ```
16
+
17
+ ## Licence
18
+
19
+ This gem is available under the MIT Licence.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "dump_to_seeds/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "dump_to_seeds"
7
+ s.version = DumpToSeeds::VERSION
8
+ s.authors = ["Dmitri Chromov"]
9
+ s.email = ["chromov@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Dump model to seeds-like ruby code}
12
+ s.description = %q{Dump model to seeds-like ruby code}
13
+
14
+ s.rubyforge_project = "dump_to_seeds"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # s.add_development_dependency "rspec"
22
+ s.add_runtime_dependency "activerecord"
23
+ end
@@ -0,0 +1,48 @@
1
+ module DumpToSeeds::BaseExt
2
+
3
+ def to_seed(includes = [])
4
+ if includes.blank?
5
+ build_simple_rb(self)
6
+ else
7
+ dump_object(self, includes)
8
+ end
9
+ end
10
+
11
+ private
12
+
13
+ def dump_object(obj, includes = [], parent_var = nil, rel_key = nil)
14
+
15
+ includes = [includes].flatten.compact
16
+
17
+ dump_arr = []
18
+ var_name = obj.class.name.underscore
19
+ dump_arr << "#{var_name} = #{build_simple_rb(obj, !(parent_var && rel_key))}"
20
+
21
+ if(parent_var && rel_key)
22
+ dump_arr << "#{var_name}.#{rel_key} = #{parent_var}.id"
23
+ dump_arr << "#{var_name}.save"
24
+ end
25
+
26
+ includes.each do |i|
27
+ rel, next_ass = i.is_a?(Hash) ? i.to_a.first : [i, []]
28
+
29
+ refl = obj.class.reflect_on_association(rel)
30
+ if refl
31
+ rel_objs = [obj.send(rel)].flatten.compact
32
+ rel_objs.each do |ro|
33
+ dump_arr << dump_object(ro, next_ass, var_name, refl.primary_key_name)
34
+ end
35
+ end
36
+ end
37
+
38
+ dump_arr.join("\n")+"\n"
39
+ end
40
+
41
+ def build_simple_rb(obj, do_create = true)
42
+ attrs = obj.attributes
43
+ attrs.delete(:id)
44
+ attrs = Hash[attrs.to_a.map{|a1,a2| [a1, a2.to_s] }]
45
+ "#{obj.class.name}.#{do_create ? 'create' : 'new'}(#{attrs.inspect})"
46
+ end
47
+
48
+ end
@@ -0,0 +1,3 @@
1
+ module DumpToSeeds
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "dump_to_seeds/version"
2
+ require "dump_to_seeds/base_ext"
3
+
4
+ module DumpToSeeds
5
+
6
+ ActiveRecord::Base.send :include, DumpToSeeds::BaseExt
7
+
8
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dump_to_seeds
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Dmitri Chromov
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-07-12 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activerecord
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: Dump model to seeds-like ruby code
36
+ email:
37
+ - chromov@gmail.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - MIT-LICENSE.txt
48
+ - README.md
49
+ - Rakefile
50
+ - dump_to_seeds.gemspec
51
+ - lib/dump_to_seeds.rb
52
+ - lib/dump_to_seeds/base_ext.rb
53
+ - lib/dump_to_seeds/version.rb
54
+ has_rdoc: true
55
+ homepage: ""
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project: dump_to_seeds
84
+ rubygems_version: 1.4.2
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Dump model to seeds-like ruby code
88
+ test_files: []
89
+