trowel 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.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in trowel.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Trowel
2
+
3
+ Loads ActiveRecord seed data from YAML.
4
+
5
+ ![Robo-gardener](http://www.technovelgy.com/graphics/content08/farmer-robot-suit.jpg "Robo-gardener")
6
+
7
+ ## Installation
8
+
9
+ Add to your Gemfile and run the `bundle` command to install it.
10
+
11
+ ```ruby
12
+ gem 'trowel'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ***Simple example***
18
+
19
+ Trowel::Sow.new(Rails.root.join('db/seeds/users.yml').to_s)
20
+
21
+ This loads data into the User model. The YAML is expected to look something like this:
22
+
23
+ # users.yml
24
+ -
25
+ email: belvedere@example.com
26
+ password: secret
27
+ -
28
+ email: humphrey@example.com
29
+ password: secret
30
+
31
+ ***Complex example***
32
+
33
+ Callbacks can be triggered in the form of a symbol or lambda, and the class can be passed in as an option.
34
+
35
+ For example:
36
+
37
+ Trowel::Sow.new(Rails.root.join('db/seeds/users.yml'),
38
+ :class => Contact,
39
+ :before_save => lambda { |u| u.email.upcase! },
40
+ :after_save => :confirm!)
41
+
42
+ ## Development
43
+
44
+ Questions or problems? Please post them on the [issue tracker](https://github.com/zubin/trowel/issues). You can contribute changes by forking the project and submitting a pull request. You can ensure the tests passing by running `bundle` and `rake`.
45
+
46
+ Copyright (c) 2012 Zubin Henner, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new('spec')
4
+
5
+ task :default => :spec
data/lib/trowel/sow.rb ADDED
@@ -0,0 +1,28 @@
1
+ module Trowel
2
+ class Sow
3
+ def initialize(path, options = {})
4
+ @path, @options = path, options
5
+ $stdout << "[Trowel::Sow] Seeding #{klass} from #{path}\n" if options[:verbose]
6
+ YAML::load_file(path).each do |attributes|
7
+ object = klass.new
8
+ attributes.each { |k,v| object.send("#{k}=", v) }
9
+ process_callback(:before_save, object)
10
+ process_callback(:after_save, object)
11
+ object.save!
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def klass
18
+ @options[:class] || File.basename(@path)[/^([^\.]+)(?:|\.ya?ml)$/, 1].singularize.classify.constantize
19
+ end
20
+
21
+ def process_callback(name, object)
22
+ case @options[name]
23
+ when Symbol then object.send(*@options[name])
24
+ when Proc then @options[name].call(object)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module Trowel
2
+ VERSION = "0.0.1"
3
+ end
data/lib/trowel.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "trowel/version"
2
+ require "trowel/sow"
@@ -0,0 +1,6 @@
1
+ -
2
+ email: belvedere@example.com
3
+ password: secret
4
+ -
5
+ email: humphrey@example.com
6
+ password: secret
@@ -0,0 +1,15 @@
1
+ require 'trowel'
2
+ require 'supermodel'
3
+
4
+ RSpec.configure do |config|
5
+ config.before(:each) { User.delete_all; Contact.delete_all }
6
+ end
7
+
8
+ class User < SuperModel::Base
9
+ def confirm!
10
+ update_attribute(:confirmed, true)
11
+ end
12
+ end
13
+
14
+ class Contact < SuperModel::Base
15
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Trowel::Sow do
4
+ let(:users_yaml_path) { File.join(File.dirname(__FILE__), 'fixtures/users.yml') }
5
+ describe '.new' do
6
+ context "path only" do
7
+ it "creates 2 users" do
8
+ Trowel::Sow.new(users_yaml_path)
9
+ User.all.collect(&:email).should eql(%w(belvedere@example.com humphrey@example.com))
10
+ end
11
+ end
12
+
13
+ context "with :class option" do
14
+ it "creates 2 contacts" do
15
+ Trowel::Sow.new(users_yaml_path, :class => Contact)
16
+ Contact.count.should eql(2)
17
+ end
18
+ end
19
+
20
+ context "with :before_save lambda" do
21
+ it "applies lambda" do
22
+ Trowel::Sow.new(users_yaml_path, :before_save => lambda { |u| u.email.upcase! })
23
+ User.all.collect(&:email).should eql(%w(BELVEDERE@EXAMPLE.COM HUMPHREY@EXAMPLE.COM))
24
+ end
25
+ end
26
+
27
+ context "with :after_save symbol" do
28
+ it "creates 2 contacts" do
29
+ Trowel::Sow.new(users_yaml_path, :after_save => :confirm!)
30
+ User.all.should be_all(&:confirmed?)
31
+ end
32
+ end
33
+ end
34
+ end
data/trowel.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "trowel/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "trowel"
7
+ s.version = Trowel::VERSION
8
+ s.authors = ["Zubin Henner"]
9
+ s.email = ["zubin.henner@gmail.com"]
10
+ s.homepage = "https://github.com/zubin/trowel"
11
+ s.summary = %q{Loads ActiveRecord seed data from YAML}
12
+ s.description = %q{Loads ActiveRecord seed data from YAML}
13
+
14
+ s.rubyforge_project = "trowel"
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_dependency('activesupport') # singularize
22
+
23
+ s.add_development_dependency('rake')
24
+ s.add_development_dependency('rspec')
25
+ s.add_development_dependency('supermodel')
26
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trowel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Zubin Henner
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-02 00:00:00.000000000 +10:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ requirement: &70129261171320 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70129261171320
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ requirement: &70129261170900 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70129261170900
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &70129261170480 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *70129261170480
48
+ - !ruby/object:Gem::Dependency
49
+ name: supermodel
50
+ requirement: &70129261170060 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *70129261170060
59
+ description: Loads ActiveRecord seed data from YAML
60
+ email:
61
+ - zubin.henner@gmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - .rspec
68
+ - Gemfile
69
+ - README.md
70
+ - Rakefile
71
+ - lib/trowel.rb
72
+ - lib/trowel/sow.rb
73
+ - lib/trowel/version.rb
74
+ - spec/fixtures/users.yml
75
+ - spec/spec_helper.rb
76
+ - spec/trowel_spec.rb
77
+ - trowel.gemspec
78
+ has_rdoc: true
79
+ homepage: https://github.com/zubin/trowel
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ segments:
92
+ - 0
93
+ hash: -4081092001574878935
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ segments:
101
+ - 0
102
+ hash: -4081092001574878935
103
+ requirements: []
104
+ rubyforge_project: trowel
105
+ rubygems_version: 1.6.2
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Loads ActiveRecord seed data from YAML
109
+ test_files:
110
+ - spec/fixtures/users.yml
111
+ - spec/spec_helper.rb
112
+ - spec/trowel_spec.rb