seed_gimmick 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6be9be948b6f884683db4b575af6fc91fb4488c2
4
+ data.tar.gz: 0edcd2d9b7c3ce9bedde7c32f063a496654bd4f0
5
+ SHA512:
6
+ metadata.gz: 8349d720b3c83b1500c4807db2224084d601e70b50f5dc13249ecdfc6a3b6c5b3b7653eb74bb8aa44053e0d829fec43bc9255014ea635895415850c075ea9e7b
7
+ data.tar.gz: bbaf8659eae13a13b8158bf51e22fab3d698b6a5e0eac8e358c80bafc23ed8dcd098ee7a59cb81eacf3dab077c3b33a330501417629f0a86673dbafe7c9c4352
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .DS_Store
16
+
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in seed_gimmick.gemspec
4
+ gemspec
5
+
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2015 i2bskn
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.
23
+
@@ -0,0 +1,28 @@
1
+ # SeedGimmick
2
+
3
+ Database bootstrapping utilities for Rails.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'seed_gimmick'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ $ bundle exec rails generate seed_gimmick:install
20
+ $ bundle exec rake db:seed
21
+
22
+ ## Contributing
23
+
24
+ 1. Fork it ( https://github.com/i2bskn/seed_gimmick/fork )
25
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
26
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
27
+ 4. Push to the branch (`git push origin my-new-feature`)
28
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,13 @@
1
+ module SeedGimmick
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path(File.join(File.dirname(__FILE__), "templates"))
5
+
6
+ desc "Install SeedGimmick files"
7
+ def install_seed_gimmick_files
8
+ template "seeds.rb", "db/seeds.rb"
9
+ end
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,3 @@
1
+ # Bootstrap of SeedGimmick
2
+ SeedGimmick.bootstrap!
3
+
@@ -0,0 +1,31 @@
1
+ require "yaml"
2
+ require "csv"
3
+ require "pathname"
4
+
5
+ require "active_support"
6
+ require "active_record"
7
+ require "activerecord-import"
8
+
9
+ require "seed_gimmick/version"
10
+ require "seed_gimmick/errors"
11
+ require "seed_gimmick/options"
12
+ require "seed_gimmick/inflector"
13
+ require "seed_gimmick/finder"
14
+ require "seed_gimmick/seed_io"
15
+ require "seed_gimmick/seed_file"
16
+ require "seed_gimmick/railtie" if defined?(Rails)
17
+
18
+ module SeedGimmick
19
+ def self.bootstrap!(options = nil)
20
+ SeedFile.find(options).each do |seed|
21
+ seed.bootstrap!
22
+ end
23
+ end
24
+
25
+ def self.seed_from(path)
26
+ path = Pathname.new(path.to_s) unless path.is_a?(Pathname)
27
+ options = Options.new(seed_dir: path)
28
+ bootstrap!(options)
29
+ end
30
+ end
31
+
@@ -0,0 +1,10 @@
1
+ module SeedGimmick
2
+ class SeedGimmickError < StandardError; end
3
+
4
+ class LoadFailed < SeedGimmickError
5
+ def initialize(seed_file)
6
+ super("Can not load: #{seed_file.to_s}")
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,18 @@
1
+ module SeedGimmick
2
+ module Finder
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def find(options = nil)
7
+ options ||= Options.new
8
+ seed_files(options).map {|file| SeedFile.new(options.seed_dir, file) }
9
+ end
10
+
11
+ private
12
+ def seed_files(options)
13
+ Pathname.glob(options.seed_dir.join("**", "*")).select(&:file?)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,35 @@
1
+ module SeedGimmick
2
+ class Inflector
3
+ attr_reader :seed_dir
4
+
5
+ def initialize(seed_dir)
6
+ @seed_dir = seed_dir
7
+ end
8
+
9
+ def model_for(seed_file)
10
+ relative_path(without_ext(seed_file)).to_s.classify.constantize
11
+ end
12
+
13
+ def seed_for(model, format = :yml)
14
+ seed_dir + _pathname(model.model_name.collection).sub_ext(".#{format}")
15
+ end
16
+
17
+ private
18
+ def without_ext(pathname)
19
+ pathname.dirname + pathname.basename(".*")
20
+ end
21
+
22
+ def relative_path(pathname)
23
+ pathname.relative? ? pathname : pathname.relative_path_from(seed_dir)
24
+ end
25
+
26
+ def _pathname(path)
27
+ case path
28
+ when Pathname then path
29
+ when String then Pathname.new(path)
30
+ else nil
31
+ end
32
+ end
33
+ end
34
+ end
35
+
@@ -0,0 +1,23 @@
1
+ module SeedGimmick
2
+ class Options
3
+ %i(seed_dir).each do |key|
4
+ define_method "#{key}=" do |value|
5
+ @options[key] = value
6
+ end
7
+ end
8
+
9
+ def initialize(options = {})
10
+ @options = options.symbolize_keys
11
+ end
12
+
13
+ def seed_dir
14
+ @options[:seed_dir] || default_seed_dir
15
+ end
16
+
17
+ private
18
+ def default_seed_dir
19
+ (defined?(Rails) ? Rails.root : Pathname.pwd).join("db", "seeds")
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,8 @@
1
+ module SeedGimmick
2
+ class Railtie < ::Rails::Railtie
3
+ rake_tasks do
4
+ load "tasks/seed_gimmick.rake"
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,39 @@
1
+ module SeedGimmick
2
+ class SeedFile
3
+ include Finder
4
+
5
+ attr_reader :inflector, :seed_file
6
+
7
+ def initialize(seed_dir, seed_file)
8
+ @inflector = Inflector.new(seed_dir)
9
+ @seed_file = seed_file
10
+ end
11
+
12
+ def load_file
13
+ SeedIO.get(seed_file).load_data
14
+ end
15
+
16
+ def bootstrap!
17
+ custom_action do |model, data|
18
+ ActiveRecord::Migration.say_with_time(model.model_name.plural) do
19
+ model.transaction do
20
+ model.delete_all
21
+ model.import(data.map {|rec| model.new(rec) })
22
+ end
23
+ end
24
+ end
25
+ rescue LoadFailed => e
26
+ puts e.message
27
+ end
28
+
29
+ def custom_action
30
+ yield(_model, load_file) if block_given?
31
+ end
32
+
33
+ private
34
+ def _model
35
+ @_model ||= inflector.model_for(seed_file)
36
+ end
37
+ end
38
+ end
39
+
@@ -0,0 +1,14 @@
1
+ require "seed_gimmick/seed_io/base"
2
+ require "seed_gimmick/seed_io/yaml_file"
3
+
4
+ module SeedGimmick
5
+ module SeedIO
6
+ def self.get(seed_file)
7
+ ext = File.extname(seed_file.to_s).presence || (raise SeedGimmickError)
8
+ ext.sub!(/\A\./, "")
9
+ ext = "yaml" if ext == "yml"
10
+ const_get("#{ext.capitalize}File", false).new(seed_file)
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,16 @@
1
+ module SeedGimmick
2
+ module SeedIO
3
+ class Base
4
+ attr_reader :seed_file
5
+
6
+ def initialize(seed_file)
7
+ @seed_file = seed_file
8
+ end
9
+
10
+ def load_data
11
+ raise NotImplementedError
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,11 @@
1
+ module SeedGimmick
2
+ module SeedIO
3
+ class YamlFile < Base
4
+ def load_data
5
+ data = YAML.load_file(seed_file) || (raise LoadFailed.new(seed_file))
6
+ data.values
7
+ end
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,3 @@
1
+ module SeedGimmick
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ namespace :seed_gimmick do
2
+ task :dump => :environment do
3
+ raise NotImplementedError
4
+ end
5
+ end
6
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'seed_gimmick/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "seed_gimmick"
8
+ spec.version = SeedGimmick::VERSION
9
+ spec.authors = ["i2bskn"]
10
+ spec.email = ["i2bskn@gmail.com"]
11
+ spec.summary = %q{Database bootstrapping utilities.}
12
+ spec.description = %q{Database bootstrapping utilities.}
13
+ spec.homepage = "https://github.com/i2bskn/seed_gimmick"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+ spec.add_dependency "activerecord"
23
+ spec.add_dependency "activerecord-import"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
28
+
File without changes
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seed_gimmick
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - i2bskn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord-import
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ description: Database bootstrapping utilities.
84
+ email:
85
+ - i2bskn@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/generators/seed_gimmick/install_generator.rb
96
+ - lib/generators/seed_gimmick/templates/seeds.rb
97
+ - lib/seed_gimmick.rb
98
+ - lib/seed_gimmick/errors.rb
99
+ - lib/seed_gimmick/finder.rb
100
+ - lib/seed_gimmick/inflector.rb
101
+ - lib/seed_gimmick/options.rb
102
+ - lib/seed_gimmick/railtie.rb
103
+ - lib/seed_gimmick/seed_file.rb
104
+ - lib/seed_gimmick/seed_io.rb
105
+ - lib/seed_gimmick/seed_io/base.rb
106
+ - lib/seed_gimmick/seed_io/yaml_file.rb
107
+ - lib/seed_gimmick/version.rb
108
+ - lib/tasks/seed_gimmick.rake
109
+ - seed_gimmick.gemspec
110
+ - spec/.keep
111
+ homepage: https://github.com/i2bskn/seed_gimmick
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.2.2
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Database bootstrapping utilities.
135
+ test_files:
136
+ - spec/.keep