lib/test_dummy.rb in test_dummy-0.1.1 vs lib/test_dummy.rb in test_dummy-0.1.3

- old
+ new

@@ -1,52 +1,59 @@ -require 'test_dummy/railtie' - module TestDummy + require 'test_dummy/railtie' + + autoload(:Helper, File.expand_path('test_dummy/helper', File.dirname(__FILE__))) + autoload(:TestHelper, File.expand_path('test_dummy/test_helper', File.dirname(__FILE__))) + def self.included(base) base.send(:extend, ClassMethods) base.send(:include, InstanceMethods) end - # Combines several sets of parameters together into a single set in order - # of lowest priority to highest priority. Supplied list can contain nil - # values which will be ignored. Returns a Hash with symbolized keys. - def self.combine_attributes(*sets) - combined_attributes = { } + module Support + # Combines several sets of parameters together into a single set in order + # of lowest priority to highest priority. Supplied list can contain nil + # values which will be ignored. Returns a Hash with symbolized keys. + def self.combine_attributes(*sets) + combined_attributes = { } - # Apply sets in order they are listed - sets.compact.each do |set| - set.each do |k, v| - case (v) - when nil - # Ignore nil assignments - else - combined_attributes[k.to_sym] = v + # Apply sets in order they are listed + sets.compact.each do |set| + set.each do |k, v| + case (v) + when nil + # Ignore nil assignments + else + combined_attributes[k.to_sym] = v + end end end - end - combined_attributes + combined_attributes + end end - # Adds a mixin to the core DummyMethods module + # Adds a mixin to the core Helper module def self.add_module(new_module) - DummyMethods.send(:extend, new_module) + Helper.send(:extend, new_module) end - # Used in an initializer to define things that can be dummyd by all + # Used in an initializer to define things that can be dummied by all # models if these properties are available. - def self.can_dummy(*names, &block) + def self.to_dummy(*names, &block) case (names.last) when Hash options = names.pop end if (options and options[:with]) block = options[:with] end - DummyMethods.send( + # Create a temporary Module and use this to roll up the methods defined + # into the Helper module + Helper.send( :extend, names.inject(Module.new) do |m, name| m.send(:define_method, name, &block) m end @@ -56,14 +63,10 @@ # Used in an initializer to define configuration parameters. def self.config(&block) TestDummy.instance_eval(&block) end - module DummyMethods - # Container for common data faking methods as they are defined. - end - module ClassMethods # Returns a Hash which describes the dummy configuration for this # Model class. def dummy_attributes @test_dummy ||= { } @@ -71,11 +74,11 @@ # Declares how to fake one or more attributes. Accepts a block # that can receive up to two parameters, the first the instance of # the model being created, the second the parameters supplied to create # it. The first and second parameters may be nil. - def can_dummy(*names, &block) + def to_dummy(*names, &block) options = nil case (names.last) when Hash options = names.pop @@ -91,11 +94,11 @@ names.flatten.each do |name| name = name.to_sym # For associations, delay creation of block until first call # to allow for additional relationships to be defined after - # the can_dummy call. Leave placeholder (true) instead. + # the to_dummy call. Leave placeholder (true) instead. @test_dummy[name] = block || true @test_dummy_order << name end end @@ -113,11 +116,11 @@ # Builds a dummy model with some parameters set as supplied. The # new model is provided to the optional block for manipulation before # the dummy operation is completed. Returns a dummy model which has not # been saved. def build_dummy(with_attributes = nil) - model = new(self.class.combine_attributes(scope(:create), with_attributes)) + model = new(TestDummy::Support.combine_attributes(scoped.scope_for_create, with_attributes)) yield(model) if (block_given?) self.execute_dummy_operation(model, with_attributes) @@ -138,11 +141,11 @@ end # Builds a dummy model with some parameters set as supplied. The # new model is provided to the optional block for manipulation before # the dummy operation is completed and the model is saved. Returns a - # dummy model. Will throw ActiveRecord::RecordInvalid if there was a + # dummy model. Will throw ActiveRecord::RecordInvalid if there was al20 # validation failure, or ActiveRecord::RecordNotSaved if the save was # blocked by a callback. def create_dummy!(with_attributes = nil, &block) model = build_dummy(with_attributes, &block) @@ -151,19 +154,19 @@ model end # Produces dummy data for a single attribute. def dummy(name, with_attributes = nil) - with_attributes = TestDummy.combine_attributes(scope(:create), with_attributes) + with_attributes = TestDummy.combine_attributes(scoped.scope_for_create, with_attributes) dummy_method_call(nil, with_attributes, dummy_method(name)) end # Produces a complete set of dummy attributes. These can be used to # create a model. def dummy_attributes(with_attributes = nil) - with_attributes = TestDummy.combine_attributes(scope(:create), with_attributes) + with_attributes = TestDummy.combine_attributes(scoped.scope_for_create, with_attributes) @test_dummy_order.each do |field| unless (with_attributes.key?(field)) result = dummy(field, with_attributes) @@ -217,10 +220,10 @@ case (block) when Module block.method(name) when Symbol - DummyMethods.method(name) + Helper.method(name) when true # Configure association dummyr the first time it is called if (reflection = reflect_on_association(name)) primary_key = reflection.primary_key_name.to_sym