Skip to content
Browse files

Change how class methods are specified in Fabricator

  • Loading branch information...
1 parent aabbf12 commit 91abcca23ee70d4dad0ecc0237e4f19232ab78f0 @paulelliott paulelliott committed Feb 28, 2012
Showing with 32 additions and 36 deletions.
  1. +32 −36 lib/fabrication/fabricator.rb
View
68 lib/fabrication/fabricator.rb
@@ -1,48 +1,44 @@
class Fabrication::Fabricator
- class << self
-
- def define(name, options={}, &block)
- raise Fabrication::DuplicateFabricatorError, "'#{name}' is already defined" if schematics.include?(name)
- aliases = Array(options.delete(:aliases))
- schematic = schematics[name] = schematic_for(name, options, &block)
- Array(aliases).each do |as|
- schematics[as.to_sym] = schematic
- end
- schematic
+ def self.define(name, options={}, &block)
+ raise Fabrication::DuplicateFabricatorError, "'#{name}' is already defined" if schematics.include?(name)
+ aliases = Array(options.delete(:aliases))
+ schematic = schematics[name] = schematic_for(name, options, &block)
+ Array(aliases).each do |as|
+ schematics[as.to_sym] = schematic
end
+ schematic
+ end
- def generate(name, options={}, overrides={}, &block)
- Fabrication::Support.find_definitions if schematics.empty?
- raise Fabrication::UnknownFabricatorError, "No Fabricator defined for '#{name}'" unless schematics.has_key?(name)
- schematics[name].generate(options, overrides, &block)
- end
+ def self.generate(name, options={}, overrides={}, &block)
+ Fabrication::Support.find_definitions if schematics.empty?
+ raise Fabrication::UnknownFabricatorError, "No Fabricator defined for '#{name}'" unless schematics.has_key?(name)
+ schematics[name].generate(options, overrides, &block)
+ end
- def schematics
- @schematics ||= {}
- end
+ def self.schematics
+ @schematics ||= {}
+ end
- private
+ private
- def class_name_for(name, parent, options)
- options[:class_name] ||
- (parent && parent.klass.name) ||
- options[:from] ||
- name
- end
+ def self.class_name_for(name, parent, options)
+ options[:class_name] ||
+ (parent && parent.klass.name) ||
+ options[:from] ||
+ name
+ end
- def schematic_for(name, options, &block)
- parent = schematics[options[:from]]
- class_name = class_name_for(name, parent, options)
- klass = Fabrication::Support.class_for(class_name)
- raise Fabrication::UnfabricatableError, "No class found for '#{name}'" unless klass
- if parent
- parent.merge(&block).tap { |s| s.klass = klass }
- else
- Fabrication::Schematic.new(klass, &block)
- end
+ def self.schematic_for(name, options, &block)
+ parent = schematics[options[:from]]
+ class_name = class_name_for(name, parent, options)
+ klass = Fabrication::Support.class_for(class_name)
+ raise Fabrication::UnfabricatableError, "No class found for '#{name}'" unless klass
+ if parent
+ parent.merge(&block).tap { |s| s.klass = klass }
+ else
+ Fabrication::Schematic.new(klass, &block)
end
-
end
end

0 comments on commit 91abcca

Please sign in to comment.
Something went wrong with that request. Please try again.