pop 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +41 -0
- data/Rakefile +2 -0
- data/lib/pop.rb +84 -0
- data/lib/pop/version.rb +3 -0
- data/pop.gemspec +17 -0
- metadata +53 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Francesco Rodriguez
|
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,41 @@
|
|
1
|
+
# Pop
|
2
|
+
|
3
|
+
Seeding helpers for Rails.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'pop'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install pop
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
on seeds.rb:
|
22
|
+
|
23
|
+
# Delete the records of a given table
|
24
|
+
Pop.clean :cities, :countries
|
25
|
+
|
26
|
+
# Populate a table/collection
|
27
|
+
Pop.populate :cities # Rails.root/db/cities.yml by default
|
28
|
+
Pop.populate :countries, "/db/data/countries.yaml"
|
29
|
+
|
30
|
+
# Clean and Populate a table
|
31
|
+
Pop.setup :cities
|
32
|
+
Pop.setup :countries, "/db/data/countries.yml"
|
33
|
+
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/pop.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require "pop/version"
|
2
|
+
|
3
|
+
require "yaml"
|
4
|
+
|
5
|
+
module Pop
|
6
|
+
extend self
|
7
|
+
|
8
|
+
# Populates a table with data in a filename.
|
9
|
+
#
|
10
|
+
# table - The Symbol that references a table name.
|
11
|
+
# filename - The String that references the filename path.
|
12
|
+
#
|
13
|
+
# Examples:
|
14
|
+
#
|
15
|
+
# City # => City(id: integer, region_id: integer, name: string)
|
16
|
+
# populate(:cities)
|
17
|
+
# populate(:countries, "db/data/countries.yaml")
|
18
|
+
#
|
19
|
+
# Returns the populated data.
|
20
|
+
def populate(table, filename = nil)
|
21
|
+
model = modelize(table)
|
22
|
+
filename ||= "#{Rails.root}/db/#{table}.yml"
|
23
|
+
|
24
|
+
result = []
|
25
|
+
YAML.load_file(filename).each do |record|
|
26
|
+
puts "Inserting #{record.inspect} into #{table.to_s.inspect}"
|
27
|
+
result << model.create(record)
|
28
|
+
end
|
29
|
+
|
30
|
+
result
|
31
|
+
end
|
32
|
+
|
33
|
+
# Delete the records of the given tables.
|
34
|
+
#
|
35
|
+
# tables - The Array of tables represented by symbols.
|
36
|
+
#
|
37
|
+
# Examples:
|
38
|
+
#
|
39
|
+
# City.count #=> 1
|
40
|
+
# clean(:cities)
|
41
|
+
# City.count #=> 0
|
42
|
+
#
|
43
|
+
# Returns the given tables name.
|
44
|
+
def clean(*tables)
|
45
|
+
tables.each do |table|
|
46
|
+
puts "Cleaning #{table.to_s.inspect}"
|
47
|
+
modelize(table).delete_all
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Clean and Populate a table.
|
52
|
+
#
|
53
|
+
# tables - The Array of tables represented by symbols
|
54
|
+
#
|
55
|
+
# Examples:
|
56
|
+
#
|
57
|
+
# setup :cities, :regions
|
58
|
+
#
|
59
|
+
# Returns the last populated data
|
60
|
+
def setup(table, filename = nil)
|
61
|
+
clean(table)
|
62
|
+
populate(table, filename)
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
# Tries to find a constant with the table name specified
|
68
|
+
# in the argument symbol.
|
69
|
+
#
|
70
|
+
# table - The Symbol that references a table name.
|
71
|
+
#
|
72
|
+
# Examples:
|
73
|
+
#
|
74
|
+
# modelize(:cities)
|
75
|
+
# # => City
|
76
|
+
#
|
77
|
+
# modelize(:people)
|
78
|
+
# # => Person
|
79
|
+
#
|
80
|
+
# Returns the finded constant.
|
81
|
+
def modelize(table)
|
82
|
+
table.to_s.classify.constantize
|
83
|
+
end
|
84
|
+
end
|
data/lib/pop/version.rb
ADDED
data/pop.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/pop/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Francesco Rodriguez"]
|
6
|
+
gem.email = ["lrodriguezsanc@gmail.com"]
|
7
|
+
gem.description = %q{Seeding helpers}
|
8
|
+
gem.summary = %q{Helpers for Rails db/seeds.rb}
|
9
|
+
gem.homepage = "https://github.com/frodsan/pop"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "pop"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Pop::VERSION
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pop
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Francesco Rodriguez
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-17 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Seeding helpers
|
15
|
+
email:
|
16
|
+
- lrodriguezsanc@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/pop.rb
|
27
|
+
- lib/pop/version.rb
|
28
|
+
- pop.gemspec
|
29
|
+
homepage: https://github.com/frodsan/pop
|
30
|
+
licenses: []
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 1.8.11
|
50
|
+
signing_key:
|
51
|
+
specification_version: 3
|
52
|
+
summary: Helpers for Rails db/seeds.rb
|
53
|
+
test_files: []
|