sowing 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7c8dbbbc138c2729840bf2b8785d4b690bee5744
4
+ data.tar.gz: 61d35ff47329fe1496b5ffdd4e4ba6c1d74aa84f
5
+ SHA512:
6
+ metadata.gz: f0a66a0a6fa5ebba5517136fd6341578dfe3b14b803da4c6b492bc5299b17c2f57cbae8a51243ff8e76ef2a3f369a7e81935d020b60a16e052c845cf1bb8cd45
7
+ data.tar.gz: 7b62d543cc02e0a880ce32ad30cfa1caef6bfbed3e2af46646e48b3db2c89d186a6f33f23e8a61ab13569bc506318061ace081a20db79320725e30f9b55bba87
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .byebug_history
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sowing.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Sowing
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sowing`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'sowing'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install sowing
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ ### Run tests
30
+
31
+ $ bundle exec rake
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sowing.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task default: :test
4
+
5
+ task :test do
6
+ ruby('test/run-test.rb')
7
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sowing"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,13 @@
1
+ require 'dry-configurable'
2
+
3
+ class Sowing::Configuration
4
+ extend Dry::Configurable
5
+
6
+ setting :default_data_directory, 'db/seeds/'
7
+ setting :extensions, %w(csv yaml yml)
8
+ setting :strategies, {
9
+ 'csv' => Sowing::Strategies::ActiveRecordCsv,
10
+ 'yaml' => Sowing::Strategies::ActiveRecordYaml,
11
+ 'yml' => Sowing::Strategies::ActiveRecordYaml
12
+ }
13
+ end
@@ -0,0 +1,59 @@
1
+ class Sowing::Runner
2
+ attr_reader :data_directory
3
+
4
+ def initialize(data_directory: nil)
5
+ @data_directory = Pathname(data_directory || Sowing::Configuration.config.default_data_directory)
6
+ end
7
+
8
+ def create(klass, filename: nil)
9
+ find_file(klass, filename: filename) do |file, strategy|
10
+ strategy.create(klass, file)
11
+ end
12
+ end
13
+
14
+ def create_or_do_nothing(klass, finding_key, filename: nil)
15
+ find_file(klass, filename: filename) do |file, strategy|
16
+ strategy.create_or_do_nothing(klass, file, finding_key)
17
+ end
18
+ end
19
+
20
+ def create_or_update(klass, finding_key, filename: nil)
21
+ find_file(klass, filename: filename) do |file, strategy|
22
+ strategy.create_or_update(klass, file, finding_key)
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def find_file(klass, filename: nil)
29
+ if filename
30
+ file = data_directory.join(filename)
31
+ if file.exist?
32
+ yield(file, select_strategy(file.extname[1..-1]))
33
+ else
34
+ raise "not found: #{file}"
35
+ end
36
+
37
+ return
38
+ end
39
+
40
+ pathname = data_directory.join(klass.to_s.underscore.pluralize)
41
+ ext = Sowing::Configuration.config.extensions.find {|ext| Pathname("#{pathname}.#{ext}").exist? }
42
+
43
+ if ext
44
+ yield(Pathname("#{pathname}.#{ext}"), select_strategy(ext))
45
+ else
46
+ raise "not found: #{pathanme}.(#{Sowing::Configuration.config.extensions.join('|')})"
47
+ end
48
+ end
49
+
50
+ def select_strategy(ext)
51
+ strategy_klass = Sowing::Configuration.config.strategies[ext]
52
+
53
+ if strategy_klass
54
+ strategy_klass.new
55
+ else
56
+ raise "strategy not found: extension #{ext}"
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,24 @@
1
+ module Sowing
2
+ module Strategies
3
+ class AbstractStrategy
4
+ def create(klass, csv_filename)
5
+ raise NotImplementedError
6
+ end
7
+
8
+ def create_or_do_nothing(klass, csv_filename, finding_key)
9
+ raise NotImplementedError
10
+ end
11
+
12
+ def create_or_update(klass, csv_filename, finding_key)
13
+ raise NotImplementedError
14
+ end
15
+
16
+ private
17
+
18
+ def print_object_info(object)
19
+ print 'create: '
20
+ p object
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,38 @@
1
+ module Sowing
2
+ module Strategies
3
+ class ActiveRecordAbstract < AbstractStrategy
4
+ def create(klass, filename)
5
+ read_data(filename).each do |row|
6
+ object = klass.create!(row.to_hash)
7
+
8
+ print_object_info(object)
9
+ end
10
+ end
11
+
12
+ def create_or_do_nothing(klass, filename, finding_key)
13
+ read_data(filename).each do |row|
14
+ klass.find_or_initialize_by(finding_key => row[finding_key.to_s]) do |object|
15
+ object.update!(row.to_hash)
16
+
17
+ print_object_info(object)
18
+ end
19
+ end
20
+ end
21
+
22
+ def create_or_update(klass, filename, finding_key)
23
+ read_data(filename).each do |row|
24
+ object = klass.find_or_initialize_by(finding_key => row[finding_key.to_s])
25
+ object.update!(row.to_hash)
26
+
27
+ print_object_info(object)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def read_data(filename)
34
+ raise NotImplementedError
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,13 @@
1
+ require 'csv'
2
+
3
+ module Sowing
4
+ module Strategies
5
+ class ActiveRecordCsv < ActiveRecordAbstract
6
+ private
7
+
8
+ def read_data(csv_filename)
9
+ CSV.read(csv_filename, headers: true)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'yaml'
2
+
3
+ module Sowing
4
+ module Strategies
5
+ class ActiveRecordYaml < ActiveRecordAbstract
6
+ private
7
+
8
+ def read_data(yaml_filename)
9
+ YAML.load_file(yaml_filename).values
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ require_relative 'strategies/abstract_strategy'
2
+ require_relative 'strategies/active_record_abstract'
3
+ require_relative 'strategies/active_record_csv'
4
+ require_relative 'strategies/active_record_yaml'
5
+
6
+ module Strategies
7
+ end
@@ -0,0 +1,3 @@
1
+ module Sowing
2
+ VERSION = "1.0.0"
3
+ end
data/lib/sowing.rb ADDED
@@ -0,0 +1,11 @@
1
+ module Sowing
2
+ end
3
+
4
+ require 'pathname'
5
+ require 'active_support'
6
+
7
+ require_relative 'sowing/strategies'
8
+ require_relative 'sowing/version'
9
+
10
+ require_relative 'sowing/configuration'
11
+ require_relative 'sowing/runner'
data/sowing.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sowing/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sowing'
8
+ spec.version = Sowing::VERSION
9
+ spec.authors = ['kbaba1001']
10
+ spec.email = ['kbaba1001@gmail.com']
11
+
12
+ spec.summary = %q{seed data handling for Rails apps}
13
+ spec.description = %q{seed data handling for Rails apps}
14
+ spec.homepage = 'https://github.com/kbaba1001/sowing'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'activerecord'
22
+ spec.add_dependency 'dry-configurable'
23
+
24
+ spec.add_development_dependency 'bundler'
25
+ spec.add_development_dependency 'byebug'
26
+ spec.add_development_dependency 'database_cleaner'
27
+ spec.add_development_dependency 'ffaker'
28
+ spec.add_development_dependency 'rake'
29
+ spec.add_development_dependency 'sqlite3'
30
+ spec.add_development_dependency 'test-unit'
31
+ end
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sowing
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - kbaba1001
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
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: dry-configurable
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: database_cleaner
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: ffaker
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: test-unit
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: seed data handling for Rails apps
140
+ email:
141
+ - kbaba1001@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - Gemfile
148
+ - README.md
149
+ - Rakefile
150
+ - bin/console
151
+ - bin/setup
152
+ - lib/sowing.rb
153
+ - lib/sowing/configuration.rb
154
+ - lib/sowing/runner.rb
155
+ - lib/sowing/strategies.rb
156
+ - lib/sowing/strategies/abstract_strategy.rb
157
+ - lib/sowing/strategies/active_record_abstract.rb
158
+ - lib/sowing/strategies/active_record_csv.rb
159
+ - lib/sowing/strategies/active_record_yaml.rb
160
+ - lib/sowing/version.rb
161
+ - sowing.gemspec
162
+ homepage: https://github.com/kbaba1001/sowing
163
+ licenses: []
164
+ metadata: {}
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubyforge_project:
181
+ rubygems_version: 2.6.11
182
+ signing_key:
183
+ specification_version: 4
184
+ summary: seed data handling for Rails apps
185
+ test_files: []