cuttings 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2691d8ba7417ac94552395c47e1a6d0ea792f6a0
4
+ data.tar.gz: 4c414ee4662e7e642be45e51c980f2c992ee1baf
5
+ SHA512:
6
+ metadata.gz: 6b34dda19039bfdaca352ae125dede3391f3dc60e56f13cb58adbc5184b6a004f751591568f4a8307596e3426238e854e4b80ae62caf3d1d3d49b5a72ae2d2e2
7
+ data.tar.gz: df1ed76b541300f76f630081f63143c5446f1fea4f6b8dbe2b1b1536ffac26b48c1f7be309abc225992685d0384e16cba67f19ebfa9e3f68e607ff6cfe8c4e38
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 vala
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,97 @@
1
+ # Cuttings
2
+
3
+ Cuttings allows you to create seed data with a simple DSL that aims to shorten
4
+ your seeding code and keeping it readable.
5
+
6
+ Attachment seeding is made to work with Paperclip's way to assing files.
7
+
8
+ ## Installation
9
+
10
+ Add to your Gemfile and `bundle install` :
11
+
12
+ ```ruby
13
+ gem 'cuttings'
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ In your `db/seeds.rb` or in a `rake` task :
19
+
20
+ ```ruby
21
+ require 'cuttings'
22
+
23
+ Cuttings.plant do
24
+ truncate %w(Visit)
25
+
26
+ seed 'User' do
27
+ create(
28
+ name: 'Jean Jean',
29
+ email: 'jean@jean.com',
30
+ avatar: attachment('user.png')
31
+ )
32
+ end
33
+ end
34
+ ```
35
+
36
+ ## DSL
37
+
38
+ The DSL is quite simple, and gives you just the tools you need to get a
39
+ clean and readable seed file.
40
+
41
+ ### Emptying models
42
+
43
+ To empty model tables, you can use the `#truncate` method followed by a list
44
+ of model names.
45
+
46
+ This is used to empty tables from models you won't seed, and is equivalent to
47
+ calling `.destroy_all` on all provided models.
48
+
49
+ ```ruby
50
+ Cuttings.plant do
51
+ truncate %w(Visit)
52
+ end
53
+ ```
54
+
55
+ ### Seeding models
56
+
57
+ To seed a model, you'll use the `#seed` method.
58
+ The `#seed` method automatically truncates the table of the model.
59
+ If you don't want it to do so, you can pass `truncate: false` as the second
60
+ argument.
61
+
62
+ ```ruby
63
+ seed 'User', truncate: false do
64
+ ...
65
+ end
66
+ ```
67
+
68
+ To create your model's instances, you'll have to call `#create` inside the
69
+ `#seed` method block, and give it a hash of attributes to assign.
70
+
71
+ ```ruby
72
+ seed 'Product' do
73
+ create(name: 'Rope', price: 12.90)
74
+ end
75
+ ```
76
+
77
+ ### Seeding attachments
78
+
79
+ To seed attachments, start by adding your attachment to your
80
+ `lib/seeds/attachments` folder.
81
+ You'll then be able to use the `#attachment` helper method from inside
82
+ the `#seed` method block to pass it to your model's creation.
83
+
84
+ ```ruby
85
+ seed 'User' do
86
+ create(
87
+ name: 'Jean Jean',
88
+ email: 'jean@jean.com',
89
+ avatar: attachment('user.png')
90
+ )
91
+ end
92
+ ```
93
+
94
+
95
+ ## Licence
96
+
97
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Cuttings'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
data/lib/cuttings.rb ADDED
@@ -0,0 +1,12 @@
1
+ module Cuttings
2
+ extend ActiveSupport::Autoload
3
+
4
+ autoload :Greenhouse
5
+ autoload :Cutting
6
+
7
+ def self.plant(&block)
8
+ greenhouse = Cuttings::Greenhouse.new
9
+ greenhouse.instance_eval(&block)
10
+ greenhouse.process
11
+ end
12
+ end
@@ -0,0 +1,40 @@
1
+ module Cuttings
2
+ class Cutting
3
+ attr_accessor :model_name, :processor
4
+
5
+ def initialize(model_name, options = {}, &block)
6
+ self.model_name = model_name
7
+ self.processor = block
8
+ end
9
+
10
+ def model
11
+ @model ||= model_name.constantize
12
+ end
13
+
14
+ def run
15
+ processor && instance_eval(&processor)
16
+ close_all_open_files
17
+ end
18
+
19
+ def create(attrs)
20
+ model.create!(attrs)
21
+ end
22
+
23
+ def attachment(path)
24
+ file = File.open(Rails.root.join('lib', 'seeds', 'attachments', path))
25
+ attachment_files << file
26
+ file
27
+ end
28
+
29
+ private
30
+
31
+ def attachment_files
32
+ @attachment_files ||= []
33
+ end
34
+
35
+ def close_all_open_files
36
+ attachment_files.each(&:close)
37
+ attachment_files.clear
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,31 @@
1
+ module Cuttings
2
+ class Greenhouse
3
+ def seed(model_name, options = {}, &block)
4
+ truncatable_models << model_name unless options.delete(:truncate) == false
5
+ creatable_models << Cutting.new(model_name, options, &block)
6
+ end
7
+
8
+ def truncate(*model_names)
9
+ truncatable_models.concat(model_names.flatten)
10
+ end
11
+
12
+ def truncatable_models
13
+ @truncatable_models ||= []
14
+ end
15
+
16
+ def creatable_models
17
+ @creatable_models ||= []
18
+ end
19
+
20
+ def process
21
+ truncate_models
22
+ creatable_models.each(&:run)
23
+ end
24
+
25
+ def truncate_models
26
+ truncatable_models.each do |model_name|
27
+ model_name.constantize.destroy_all
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Cuttings
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :cuttings do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cuttings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Valentin Ballestrino
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Fast & easy seeding for Rails
42
+ email:
43
+ - vala@glyph.fr
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/cuttings.rb
52
+ - lib/cuttings/cutting.rb
53
+ - lib/cuttings/greenhouse.rb
54
+ - lib/cuttings/version.rb
55
+ - lib/tasks/cuttings_tasks.rake
56
+ homepage: http://github.com/glyph-fr/cuttings
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.4.4
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Fast & easy seeding for Rails
80
+ test_files: []