-
-
Save phildionne/6737819 to your computer and use it in GitHub Desktop.
Minidress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'bundler', '~> 1.3' | |
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
$:.unshift File.expand_path('../', __FILE__) | |
Gem::Specification.new do |s| | |
s.name = 'minidress' | |
s.version = '0.0.1' | |
s.platform = Gem::Platform::RUBY | |
s.files = `git ls-files`.split("\n") | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# More "proper" than a miniskirt (http://gist.github.com/273579). | |
class Minidress | |
@@factories = {} | |
class << self | |
def define(name, &block) | |
@@factories[name.to_s] = block | |
end | |
def build(name, attrs = {}) | |
new(name.to_s.classify.constantize.new, &@@factories[name.to_s]).record | |
end | |
def attributes_for(name, attrs = {}) | |
build(name, attrs).attributes | |
end | |
def create(name, attrs = {}) | |
build(name, attrs).tap { |record| record.save } | |
end | |
end | |
attr_reader :record | |
def initialize(record) | |
@record = record and yield self | |
end | |
def association(name) | |
send name, self.class.create(name) | |
end | |
def sequence(name) | |
send name, yield(@n ||= record.class.maximum(:id).to_i + 1, record) | |
end | |
private | |
def method_missing(name, *value) | |
record.send "#{name}=", *(block_given? ? yield(record) : value) | |
end | |
end | |
def Minidress(name, attrs = {}) | |
Minidress.create(name, attrs) | |
end | |
Factory = Minidress | |
alias Factory Minidress |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment