From 83da44aecbc92e58082a6929d65a46355126fea8 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 20 Feb 2013 13:09:29 -0500 Subject: [PATCH] initial commit --- .gitignore | 17 +++++++ Gemfile | 4 ++ LICENSE.txt | 22 +++++++++ README.md | 21 +++++++++ Rakefile | 1 + lib/seed-fu-mongoid.rb | 27 +++++++++++ lib/seed-fu-mongoid/capistrano.rb | 10 ++++ lib/seed-fu-mongoid/document_seeder.rb | 63 ++++++++++++++++++++++++++ lib/seed-fu-mongoid/railtie.rb | 17 +++++++ lib/seed-fu-mongoid/seed_proxy.rb | 26 +++++++++++ lib/seed-fu-mongoid/version.rb | 3 ++ seed-fu-mongoid.gemspec | 21 +++++++++ 12 files changed, 232 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100644 lib/seed-fu-mongoid.rb create mode 100644 lib/seed-fu-mongoid/capistrano.rb create mode 100644 lib/seed-fu-mongoid/document_seeder.rb create mode 100644 lib/seed-fu-mongoid/railtie.rb create mode 100644 lib/seed-fu-mongoid/seed_proxy.rb create mode 100644 lib/seed-fu-mongoid/version.rb create mode 100644 seed-fu-mongoid.gemspec diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d87d4be --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..7d71128 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in seed-fu-mongoid.gemspec +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..8ad8cf3 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2013 John Bintz + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..febe227 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +I missed [Seed Fu](https://github.com/mbleigh/seed-fu) when I was working on a Mongoid app, so I hacked this together in about 20 minutes. +No tests whatsoever but it works with my very simple Seed Fu-like seed data, so if you like it, contribute some tests! + +## Usage + +Seed files in `db/fixtures/*.rb`: + +``` ruby +PermissionType.seed(id: 1, type: 'Users') +PermissionType.seed(id: 2, type: 'Sites') +PermissionType.seed(id: 3, type: 'Cats') +``` + +Then `rake db:seed_fu` if you're in Rails. + +Simple! + +### Capistrano + +`require 'seed-fu-mongoid/capistrano'` to have your data get re-seeded on deploy. + diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..2995527 --- /dev/null +++ b/Rakefile @@ -0,0 +1 @@ +require "bundler/gem_tasks" diff --git a/lib/seed-fu-mongoid.rb b/lib/seed-fu-mongoid.rb new file mode 100644 index 0000000..a795d90 --- /dev/null +++ b/lib/seed-fu-mongoid.rb @@ -0,0 +1,27 @@ +require "seed-fu-mongoid/version" +require "seed-fu-mongoid/document_seeder" +require "seed-fu-mongoid/seed_proxy" +require 'mongoid/document' + +if defined?(Rails::Railtie) + require 'seed-fu-mongoid/railtie' +end + +# punch the duck out of Mongoid::Document + +module Mongoid::Document + module ClassMethods + def seed_once(*constraints) + seeder = SeedFuMongoid::DocumentSeeder.new(self, constraints, block) + + if seeder.new? + seeder.seed! + end + end + + def seed(*constraints, &block) + SeedFuMongoid::DocumentSeeder.new(self, constraints, block).seed! + end + end +end + diff --git a/lib/seed-fu-mongoid/capistrano.rb b/lib/seed-fu-mongoid/capistrano.rb new file mode 100644 index 0000000..9759766 --- /dev/null +++ b/lib/seed-fu-mongoid/capistrano.rb @@ -0,0 +1,10 @@ +Capistrano::Configuration.instance.load do + namespace :db do + desc "Load seed data into Mongoid database" + task :seed_fu do + cd "#{current_path} && bundle exec rake RAILS_ENV=#{rails_env} db:seed_fu" + end + end + + after 'deploy:update_code', 'db:seed_fu' +end diff --git a/lib/seed-fu-mongoid/document_seeder.rb b/lib/seed-fu-mongoid/document_seeder.rb new file mode 100644 index 0000000..4ef05c5 --- /dev/null +++ b/lib/seed-fu-mongoid/document_seeder.rb @@ -0,0 +1,63 @@ +module SeedFuMongoid + class DocumentSeeder + class ConstraintNotDefined < StandardError ; end + + attr_reader :constraints + + def initialize(klass, constraints, block) + @klass, @constraints, @block = klass, constraints, block + + if @constraints.empty? + @constraints = [ :id ] + end + end + + def proxy + return @proxy if @proxy + + @proxy = SeedProxy.new + + @block.call(@proxy) + + @proxy + end + + def document + @doc ||= begin + @klass.find_by(constraint_search) + rescue => e + @klass.new + end + end + + def constraint_search + return @constraint_search if @constraint_search + + @constraint_search = {} + constraints.each do |constraint| + if proxy[constraint] + @constraint_search[constraint] = proxy[constraint] + else + raise ConstraintNotDefined.new(constraint) + end + end + + @constraint_search + end + + def seed! + proxy.each do |key, value| + document[key] = value + end + + puts "#{@klass.name} #{document.attributes}" + + document.upsert + end + + def new? + !document.persisted? + end + end +end + diff --git a/lib/seed-fu-mongoid/railtie.rb b/lib/seed-fu-mongoid/railtie.rb new file mode 100644 index 0000000..ba595c6 --- /dev/null +++ b/lib/seed-fu-mongoid/railtie.rb @@ -0,0 +1,17 @@ +module SeedFuMongoid + class Railtie < ::Rails::Railtie + rake_tasks do + namespace :db do + desc "Load seed data from db/fixtures" + task :seed_fu do + Dir['db/fixtures/**/*.rb'].each do |file| + puts "== Seed from #{file}" + + load file + end + end + end + end + end +end + diff --git a/lib/seed-fu-mongoid/seed_proxy.rb b/lib/seed-fu-mongoid/seed_proxy.rb new file mode 100644 index 0000000..6e1d769 --- /dev/null +++ b/lib/seed-fu-mongoid/seed_proxy.rb @@ -0,0 +1,26 @@ +module SeedFuMongoid + class SeedProxy + attr_reader :data + + def initialize + @data = {} + end + + def method_missing(method, value, *args) + if method.to_s[-1..-1] == '=' + @data[method.to_s[0..-2].to_sym] = value + else + super + end + end + + def [](key) + @data[key] + end + + def each(&block) + @data.each(&block) + end + end +end + diff --git a/lib/seed-fu-mongoid/version.rb b/lib/seed-fu-mongoid/version.rb new file mode 100644 index 0000000..f210775 --- /dev/null +++ b/lib/seed-fu-mongoid/version.rb @@ -0,0 +1,3 @@ +module SeedFuMongoid + VERSION = "0.0.1" +end diff --git a/seed-fu-mongoid.gemspec b/seed-fu-mongoid.gemspec new file mode 100644 index 0000000..3a420b2 --- /dev/null +++ b/seed-fu-mongoid.gemspec @@ -0,0 +1,21 @@ +# -*- encoding: utf-8 -*- +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'seed-fu-mongoid/version' + +Gem::Specification.new do |gem| + gem.name = "seed-fu-mongoid" + gem.version = SeedFuMongoid::VERSION + gem.authors = ["John Bintz"] + gem.email = ["john@coswellproductions.com"] + gem.description = %q{Seed Fu-like functionality for Mongoid} + gem.summary = %q{Seed Fu-like functionality for Mongoid} + gem.homepage = "" + + gem.files = `git ls-files`.split($/) + gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } + gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) + gem.require_paths = ["lib"] + + gem.add_dependency 'mongoid' +end