diff --git a/Gemfile b/Gemfile index 7d71128..b827593 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,6 @@ source 'https://rubygems.org' # Specify your gem's dependencies in seed-fu-mongoid.gemspec gemspec + +gem 'guard-cucumber', :git => 'git://github.com/johnbintz/guard-cucumber', :branch => 'paths_from_profile' + diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..1dc2142 --- /dev/null +++ b/Guardfile @@ -0,0 +1,10 @@ +# added by cuke-pack + +group :wip do + guard 'cucumber', :env => :cucumber, :paths_from_profile => true, :cli => '--color -p wip' do + watch(%r{^features/.+.feature$}) + watch(%r{^(app|lib).*}) { 'features' } + watch(%r{^features/support/.+$}) { 'features' } + watch(%r{^features/step_definitions/(.+).rb$}) { 'features' } + end +end diff --git a/Rakefile b/Rakefile index 2995527..ffc9f29 100644 --- a/Rakefile +++ b/Rakefile @@ -1 +1,6 @@ require "bundler/gem_tasks" + +task :default do + system 'bundle exec cucumber' + exit $?.exitstatus if $?.exitstatus != 0 +end diff --git a/config/cucumber.yml b/config/cucumber.yml new file mode 100644 index 0000000..917d185 --- /dev/null +++ b/config/cucumber.yml @@ -0,0 +1,2 @@ +<%= require 'cuke-pack/profiles' ; CukePack::Profiles.write %> + diff --git a/config/mongoid.yml b/config/mongoid.yml new file mode 100644 index 0000000..35c89b0 --- /dev/null +++ b/config/mongoid.yml @@ -0,0 +1,6 @@ +test: + sessions: + default: + database: seed_fu_mongoid_test + hosts: + - localhost:27017 diff --git a/features/seed_from_block.feature b/features/seed_from_block.feature new file mode 100644 index 0000000..a3d5d18 --- /dev/null +++ b/features/seed_from_block.feature @@ -0,0 +1,4 @@ +Feature: Seed From Block + Scenario: Seed new records from block + When I seed records from a block + Then I should have the new records diff --git a/features/seed_from_hash.feature b/features/seed_from_hash.feature new file mode 100644 index 0000000..35be28e --- /dev/null +++ b/features/seed_from_hash.feature @@ -0,0 +1,9 @@ +Feature: Seed From Hash + Scenario: Seed new records from hash + When I seed records from a hash + Then I should have the new records + + Scenario: Update existing records from a hash + Given I have an existing record + When I seed records from a hash + Then I should have the new records diff --git a/features/step_definitions/given/i_have_an_existing_record.rb b/features/step_definitions/given/i_have_an_existing_record.rb new file mode 100644 index 0000000..a4a2767 --- /dev/null +++ b/features/step_definitions/given/i_have_an_existing_record.rb @@ -0,0 +1,6 @@ +Given(/^I have an existing record$/) do + record = Record.new + record[:_id] = 1 + record[:data] = 'other' + record.upsert +end diff --git a/features/step_definitions/then/i_should_have_the_new_records.rb b/features/step_definitions/then/i_should_have_the_new_records.rb new file mode 100644 index 0000000..0ec0d5f --- /dev/null +++ b/features/step_definitions/then/i_should_have_the_new_records.rb @@ -0,0 +1,4 @@ +Then(/^I should have the new records$/) do + Record.count.should == 1 + Record.find(1).data.should == 'data' +end diff --git a/features/step_definitions/when/i_seed_records_from_a_block.rb b/features/step_definitions/when/i_seed_records_from_a_block.rb new file mode 100644 index 0000000..c09a199 --- /dev/null +++ b/features/step_definitions/when/i_seed_records_from_a_block.rb @@ -0,0 +1,6 @@ +When(/^I seed records from a block$/) do + Record.seed :id do |s| + s.id = 1 + s.data = 'data' + end +end diff --git a/features/step_definitions/when/i_seed_records_from_a_hash.rb b/features/step_definitions/when/i_seed_records_from_a_hash.rb new file mode 100644 index 0000000..9ad8830 --- /dev/null +++ b/features/step_definitions/when/i_seed_records_from_a_hash.rb @@ -0,0 +1,4 @@ +When(/^I seed records from a hash$/) do + Record.seed :id, + { :id => 1, :data => 'data' } +end diff --git a/features/support/cuke-pack.rb b/features/support/cuke-pack.rb new file mode 100644 index 0000000..c1dd7f9 --- /dev/null +++ b/features/support/cuke-pack.rb @@ -0,0 +1,6 @@ +# write out missing steps automatically +require 'cuke-pack/support/step_writer' + +# use advanced in_progress mode +require 'cuke-pack/support/in_progress' + diff --git a/features/support/env.rb b/features/support/env.rb new file mode 100644 index 0000000..5055393 --- /dev/null +++ b/features/support/env.rb @@ -0,0 +1,25 @@ +require 'database_cleaner' +require 'mongoid' + +Mongoid.load!('config/mongoid.yml', :test) + +DatabaseCleaner[:mongoid].strategy = :truncation + +Before do + DatabaseCleaner[:mongoid].start +end + +After do + DatabaseCleaner[:mongoid].clean +end + +$: << File.expand_path('lib') + +require 'seed-fu-mongoid' + +class Record + include Mongoid::Document + + field :data, type: String +end + diff --git a/lib/seed-fu-mongoid.rb b/lib/seed-fu-mongoid.rb index 8e1747c..aff9c19 100644 --- a/lib/seed-fu-mongoid.rb +++ b/lib/seed-fu-mongoid.rb @@ -31,8 +31,8 @@ module Mongoid::Document end end - def seed(*constraints, &block) - SeedFuMongoid::DocumentSeeder.new(self, constraints, block).seed! + def seed(*constraints_and_objects, &block) + SeedFuMongoid::DocumentSeeder.new(self, constraints_and_objects, block).seed! end end end diff --git a/lib/seed-fu-mongoid/document_seeder.rb b/lib/seed-fu-mongoid/document_seeder.rb index 4ef05c5..efd7026 100644 --- a/lib/seed-fu-mongoid/document_seeder.rb +++ b/lib/seed-fu-mongoid/document_seeder.rb @@ -4,55 +4,94 @@ module SeedFuMongoid attr_reader :constraints - def initialize(klass, constraints, block) - @klass, @constraints, @block = klass, constraints, block + def initialize(klass, constraints_and_objects, block) + @klass, @block = klass, block + + @constraints = [] + @objects = [] + + constraints_and_objects.each do |constraint_or_object| + case constraint_or_object + when Symbol + @constraints << constraint_or_object + when Hash + @objects << constraint_or_object + end + end if @constraints.empty? @constraints = [ :id ] end end - def proxy - return @proxy if @proxy + class Document + def initialize(klass, constraints, data_or_block) + @klass, @constraints = klass, constraints - @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] + case data_or_block + when Hash + @data = data_or_block else - raise ConstraintNotDefined.new(constraint) + @block = data_or_block end end - @constraint_search + def data + return @data if @data + + @data = SeedProxy.new + + @block.call(@data) + + @data + 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 data[constraint] + @constraint_search[constraint] = proxy[constraint] + else + raise ConstraintNotDefined.new(constraint) + end + end + + @constraint_search + end + + def seed! + data.each do |key, value| + document[key] = value + end + + puts "#{@klass.name} #{document.attributes}" + + document.upsert + end + end + + def create_document(block_or_object) + Document.new(@klass, @constraints, block_or_object) end def seed! - proxy.each do |key, value| - document[key] = value + if @objects.empty? + create_document(@block).seed! + else + @objects.each do |object| + create_document(object).seed! + end end - - puts "#{@klass.name} #{document.attributes}" - - document.upsert end def new? diff --git a/seed-fu-mongoid.gemspec b/seed-fu-mongoid.gemspec index 3a420b2..66988ae 100644 --- a/seed-fu-mongoid.gemspec +++ b/seed-fu-mongoid.gemspec @@ -17,5 +17,12 @@ Gem::Specification.new do |gem| gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.require_paths = ["lib"] - gem.add_dependency 'mongoid' + gem.add_dependency 'mongoid', '>= 3.0.0' + gem.add_development_dependency 'cucumber' + gem.add_development_dependency 'rspec' + gem.add_development_dependency 'database_cleaner' + gem.add_development_dependency 'cuke-pack' + gem.add_development_dependency 'guard' + gem.add_development_dependency 'rb-fsevent' + gem.add_development_dependency 'guard-cucumber' end