can_has_fixtures 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/HISTORY +4 -0
- data/LICENSE +25 -0
- data/README +78 -0
- data/Rakefile +76 -0
- data/lib/can_has_fixtures/core_ext/class.rb +49 -0
- data/lib/can_has_fixtures/core_ext/fixnum.rb +5 -0
- data/lib/can_has_fixtures/core_ext.rb +2 -0
- data/lib/can_has_fixtures/fixturize_mixin.rb +75 -0
- data/lib/can_has_fixtures/random.rb +51 -0
- data/lib/can_has_fixtures/util.rb +16 -0
- data/lib/can_has_fixtures/version.rb +3 -0
- data/lib/can_has_fixtures.rb +41 -0
- metadata +66 -0
data/HISTORY
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Copyright (c) 2008 Ben Burkert <ben@benburkert.com>
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
* Redistributions of source code must retain the above copyright notice,
|
8
|
+
this list of conditions and the following disclaimer.
|
9
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
11
|
+
and/or other materials provided with the distribution.
|
12
|
+
* Neither the name of this project nor the names of its contributors may be
|
13
|
+
used to endorse or promote products derived from this software without
|
14
|
+
specific prior written permission.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
17
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
20
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
21
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
22
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
23
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
24
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
25
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
= can_has_fixtures
|
2
|
+
|
3
|
+
Easily create fixtures.
|
4
|
+
|
5
|
+
Author:: Ben Burkert (mailto:ben@benburkert.com)
|
6
|
+
Version:: 0.1.0
|
7
|
+
Copyright:: Copyright (c) 2008 Ben Burkert. All rights reserved.
|
8
|
+
License:: New BSD License (http://opensource.org/licenses/bsd-license.php)
|
9
|
+
Website:: http://canhasgems.rubyforge.org/can_has_fixtures/
|
10
|
+
Repository:: git://github.com/benburkert/can_has_fixtures.git
|
11
|
+
|
12
|
+
== Dependencies
|
13
|
+
|
14
|
+
== Basic Usage
|
15
|
+
|
16
|
+
can_has_fixtures works with any ORM that provides a +create+ method that accepts an attribute
|
17
|
+
hash on the model classes.
|
18
|
+
|
19
|
+
can_has_fixtures works best with RSpec, but can be used from irb as well:
|
20
|
+
|
21
|
+
require 'can_has_fixtures'
|
22
|
+
|
23
|
+
First create a fixture for the model, in this case it's Post:
|
24
|
+
|
25
|
+
Post.fixture {{
|
26
|
+
:title => 3.random.words * ' ',
|
27
|
+
:created_at => Time.now,
|
28
|
+
:body => 5.random.paragraphs * '\n',
|
29
|
+
:author => User.gen
|
30
|
+
}}
|
31
|
+
|
32
|
+
If the a nested fixture is used (author, in this case), make sure to create add the fixture:
|
33
|
+
|
34
|
+
User.fixture {{
|
35
|
+
:first_name => Random.word,
|
36
|
+
:last_name => Random.word,
|
37
|
+
:user_name => Random.word,
|
38
|
+
:email => Random.email,
|
39
|
+
:password => Random.word
|
40
|
+
}}
|
41
|
+
|
42
|
+
Finally, call model class' +gen+ method to create the fixture:
|
43
|
+
|
44
|
+
@post = Post.gen
|
45
|
+
|
46
|
+
Fixtures can also be named by supplying the +fixture+ method a paramater:
|
47
|
+
|
48
|
+
Post.fixture(:short_post) {{
|
49
|
+
:title => 3.random.words * ' ',
|
50
|
+
:created_at => Time.now,
|
51
|
+
:body => Random.sentence,
|
52
|
+
:author => User.gen
|
53
|
+
}}
|
54
|
+
|
55
|
+
@post = Post.gen(:short_post)
|
56
|
+
|
57
|
+
can_has_fixtures is great for specing models. By using the model's class for the describe target,
|
58
|
+
simply call +fixture+ within the body of the spec:
|
59
|
+
|
60
|
+
describe Post do
|
61
|
+
fixture {{
|
62
|
+
:title => 3.random.words * ' ',
|
63
|
+
:created_at => Time.now,
|
64
|
+
:body => Random.sentence
|
65
|
+
}}
|
66
|
+
|
67
|
+
before(:each) do
|
68
|
+
@post = Post.gen
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should not be valid without a body" do
|
72
|
+
@post.body = nil
|
73
|
+
@post.save
|
74
|
+
|
75
|
+
@post.should_not be_valid
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require "rake"
|
4
|
+
require "rake/clean"
|
5
|
+
require "rake/gempackagetask"
|
6
|
+
require 'rake/rdoctask'
|
7
|
+
require "spec"
|
8
|
+
require "spec/rake/spectask"
|
9
|
+
|
10
|
+
DIR = File.dirname(__FILE__)
|
11
|
+
NAME = 'can_has_fixtures'
|
12
|
+
SUMMARY =<<-EOS
|
13
|
+
Library for generating fixtures.
|
14
|
+
EOS
|
15
|
+
|
16
|
+
require "lib/can_has_fixtures/version"
|
17
|
+
|
18
|
+
spec = Gem::Specification.new do |s|
|
19
|
+
s.name = NAME
|
20
|
+
s.summary = SUMMARY
|
21
|
+
|
22
|
+
s.version = CanHasFixtures::VERSION
|
23
|
+
s.platform = Gem::Platform::RUBY
|
24
|
+
|
25
|
+
s.require_path = "lib"
|
26
|
+
s.files = %w(Rakefile LICENSE HISTORY README) + Dir["lib/**/*"]
|
27
|
+
end
|
28
|
+
|
29
|
+
Rake::GemPackageTask.new(spec) do |package|
|
30
|
+
package.gem_spec = spec
|
31
|
+
package.need_zip = true
|
32
|
+
package.need_tar = true
|
33
|
+
end
|
34
|
+
|
35
|
+
##############################################################################
|
36
|
+
# Documentation
|
37
|
+
##############################################################################
|
38
|
+
task :doc => "doc:rerdoc"
|
39
|
+
namespace :doc do
|
40
|
+
|
41
|
+
Rake::RDocTask.new do |rdoc|
|
42
|
+
files = ['README', 'LICENSE', 'HISTORY',
|
43
|
+
'lib/**/*.rb', 'doc/**/*.rdoc', 'spec/**/*.rb']
|
44
|
+
rdoc.rdoc_files.add(files)
|
45
|
+
rdoc.main = 'README'
|
46
|
+
rdoc.title = 'I CAN HAS "CAN HAS FIXTURES?" DOCS? '
|
47
|
+
rdoc.template = 'tools/allison-2.0.3/lib/allison'
|
48
|
+
rdoc.rdoc_dir = 'doc/can_has_fixtures/rdoc'
|
49
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
50
|
+
end
|
51
|
+
|
52
|
+
desc "rdoc to rubyforge"
|
53
|
+
task :rubyforge => :doc do
|
54
|
+
sh %{chmod -R 755 doc}
|
55
|
+
sh %{/usr/bin/scp -r -p doc/can_has_fixtures/rdoc/* benburkert@rubyforge.org:/var/www/gforge-projects/canhasgems/can_has_fixtures}
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
##############################################################################
|
60
|
+
# rSpec & rcov
|
61
|
+
##############################################################################
|
62
|
+
desc "Run all specs"
|
63
|
+
Spec::Rake::SpecTask.new("specs") do |t|
|
64
|
+
t.spec_opts = ["--format", "specdoc", "--colour"]
|
65
|
+
t.spec_files = Dir["spec/**/*_spec.rb"].sort
|
66
|
+
end
|
67
|
+
|
68
|
+
##############################################################################
|
69
|
+
# release
|
70
|
+
##############################################################################
|
71
|
+
task :release => [:specs, "doc:rubyforge", :package] do
|
72
|
+
sh %{rubyforge add_release canhasgems can_has_fixtures "#{CanHasFixtures::VERSION}" pkg/#{NAME}-#{CanHasFixtures::VERSION}.gem}
|
73
|
+
%w[zip tgz].each do |ext|
|
74
|
+
sh %{rubyforge add_file canhasgems can_has_fixtures #{CanHasFixtures::VERSION} pkg/can_has_fixtures-#{CanHasFixtures::VERSION}.#{ext}}
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
class Class # :nodoc:
|
2
|
+
include FixturizeMixin
|
3
|
+
|
4
|
+
def cattr_reader(*syms)
|
5
|
+
syms.flatten.each do |sym|
|
6
|
+
next if sym.is_a?(Hash)
|
7
|
+
class_eval(<<-EOS, __FILE__, __LINE__)
|
8
|
+
unless defined? @@#{sym}
|
9
|
+
@@#{sym} = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.#{sym}
|
13
|
+
@@#{sym}
|
14
|
+
end
|
15
|
+
|
16
|
+
def #{sym}
|
17
|
+
@@#{sym}
|
18
|
+
end
|
19
|
+
EOS
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def cattr_writer(*syms)
|
24
|
+
options = syms.last.is_a?(Hash) ? syms.pop : {}
|
25
|
+
syms.flatten.each do |sym|
|
26
|
+
class_eval(<<-EOS, __FILE__, __LINE__)
|
27
|
+
unless defined? @@#{sym}
|
28
|
+
@@#{sym} = nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.#{sym}=(obj)
|
32
|
+
@@#{sym} = obj
|
33
|
+
end
|
34
|
+
|
35
|
+
#{"
|
36
|
+
|
37
|
+
def #{sym}=(obj)
|
38
|
+
@@#{sym} = obj
|
39
|
+
end
|
40
|
+
" unless options[:instance_writer] == false }
|
41
|
+
EOS
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def cattr_accessor(*syms)
|
46
|
+
cattr_reader(*syms)
|
47
|
+
cattr_writer(*syms)
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module FixturizeMixin
|
2
|
+
def self.included(base)
|
3
|
+
unless method_defined? :method_missing
|
4
|
+
def method_missing(meth, *args, &block); super; end
|
5
|
+
end
|
6
|
+
|
7
|
+
base.class_eval do
|
8
|
+
alias_method :method_missing_without_fixture_check, :method_missing
|
9
|
+
alias_method :method_missing, :method_missing_with_fixture_check
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing_with_fixture_check(meth, *args, &block)
|
14
|
+
if meth.to_s == "fixture"
|
15
|
+
if respond_to?(:description_args)
|
16
|
+
fixturize_description_args(*args, &block)
|
17
|
+
else
|
18
|
+
fixturize!
|
19
|
+
fixture(*args, &block)
|
20
|
+
end
|
21
|
+
else
|
22
|
+
method_missing_without_fixture_check(meth, *args, &block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def fixturize_description_args(*args, &block)
|
27
|
+
klass = description_args.first
|
28
|
+
|
29
|
+
case klass
|
30
|
+
when Class then fixturize!(klass) unless klass.respond_to?(:fixture)
|
31
|
+
end
|
32
|
+
|
33
|
+
klass.fixture(args.first || :default, &block)
|
34
|
+
end
|
35
|
+
|
36
|
+
def fixturize!(klass = self)
|
37
|
+
klass.cattr_accessor :fixture_procs
|
38
|
+
|
39
|
+
klass.fixture_procs = {}
|
40
|
+
|
41
|
+
class << klass
|
42
|
+
def fixture(identifier = :default, &block)
|
43
|
+
case identifier
|
44
|
+
when Class then add_fixture(identifier.to_s.snake_case.to_sym, &block)
|
45
|
+
when String then add_fixture(identifier.snake_case.to_sym, &block)
|
46
|
+
when Symbol then add_fixture(identifier, &block)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def add_fixture(sym, &block)
|
51
|
+
fixture_procs[sym] = fixture_procs[sym] ? fixture_procs[sym] << block : [block]
|
52
|
+
end
|
53
|
+
|
54
|
+
def generate(*args, &block)
|
55
|
+
args, attributes = args_and_options(*args)
|
56
|
+
identifier = args.first || :default
|
57
|
+
|
58
|
+
attr_procs = fixture_procs[identifier.to_s.snake_case.to_sym]
|
59
|
+
|
60
|
+
raise "#{identifier} fixture was not found" if attr_procs.nil? || attr_procs.empty?
|
61
|
+
|
62
|
+
proc = attr_procs[rand(attr_procs.size)]
|
63
|
+
|
64
|
+
values = proc.call(*args)
|
65
|
+
|
66
|
+
case values
|
67
|
+
when Hash then create(values.merge(attributes))
|
68
|
+
else raise "#{identifier.to_s.camel_case} fixture's attributes were not recognized"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
alias_method :gen, :generate
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
class Random
|
2
|
+
WORDS_PER_SENTENCE = 3..20
|
3
|
+
SENTENCES_PER_PARAGRAPH = 3..8
|
4
|
+
|
5
|
+
attr_accessor :count
|
6
|
+
|
7
|
+
def self.chain(target)
|
8
|
+
Random.new(target)
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(count = 1)
|
12
|
+
@count = count
|
13
|
+
end
|
14
|
+
|
15
|
+
def dictionary
|
16
|
+
@@dictionary ||= File.read("/usr/share/dict/words").split
|
17
|
+
end
|
18
|
+
|
19
|
+
def word
|
20
|
+
dictionary[rand(dictionary.size)]
|
21
|
+
end
|
22
|
+
|
23
|
+
def words
|
24
|
+
(1..count).collect { word }
|
25
|
+
end
|
26
|
+
|
27
|
+
def sentence(punctuate = true)
|
28
|
+
(1..WORDS_PER_SENTENCE.to_a[rand(WORDS_PER_SENTENCE.to_a.size)]).collect{ word }.join(' ') + (punctuate ? '.' : '')
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
def sentences(punctuate = true)
|
33
|
+
(1..count).collect { sentence(punctuate) }
|
34
|
+
end
|
35
|
+
|
36
|
+
def paragraph(end_with_newline = true)
|
37
|
+
(1..SENTENCES_PER_PARAGRAPH.to_a[rand(SENTENCES_PER_PARAGRAPH.to_a.size)]).collect{ sentence }.join(' ') + (end_with_newline ? '\n' : '')
|
38
|
+
end
|
39
|
+
|
40
|
+
def paragraphs(end_with_newline = true)
|
41
|
+
(1..count).collect { paragraph(end_with_newline) }
|
42
|
+
end
|
43
|
+
|
44
|
+
def email
|
45
|
+
"#{word}@#{word}.#{%w[com net org biz info gov][rand(6)]}"
|
46
|
+
end
|
47
|
+
|
48
|
+
def emails
|
49
|
+
(1..count).collect { email }
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class Object
|
2
|
+
def args_and_options(*args)
|
3
|
+
options = Hash === args.last ? args.pop : {}
|
4
|
+
return args, options
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class String
|
9
|
+
def snake_case
|
10
|
+
gsub(/\B[A-Z]/, '_\&').downcase
|
11
|
+
end
|
12
|
+
|
13
|
+
def camel_case
|
14
|
+
split('_').map{|e| e.capitalize}.join
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "can_has_fixtures/util"
|
2
|
+
require "can_has_fixtures/fixturize_mixin"
|
3
|
+
require "can_has_fixtures/core_ext"
|
4
|
+
|
5
|
+
require "can_has_fixtures/random"
|
6
|
+
|
7
|
+
module CanHasFixtures
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
attr_accessor :fixture_procs
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.included(base)
|
14
|
+
base.extend(ClassMethods)
|
15
|
+
|
16
|
+
unless method_defined? :method_missing
|
17
|
+
def method_missing(meth, *args, &block); super; end
|
18
|
+
end
|
19
|
+
|
20
|
+
base.class_eval do
|
21
|
+
alias_method :method_missing_without_fixture_check, :method_missing
|
22
|
+
alias_method :method_missing, :method_missing_missing_with_fixture_check
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def method_missing_missing_with_fixture_check(meth, *args, &block)
|
27
|
+
if meth.to_s == "fixture"
|
28
|
+
fixturize_description($1.to_sym, *args, &block)
|
29
|
+
else
|
30
|
+
method_missing_without_fixture_check(meth, *args, &block)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def fixture(*args, &block)
|
35
|
+
p "here"
|
36
|
+
end
|
37
|
+
|
38
|
+
def fixturize_description
|
39
|
+
p description_args
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: can_has_fixtures
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors: []
|
7
|
+
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-03-17 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email:
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- LICENSE
|
27
|
+
- HISTORY
|
28
|
+
- README
|
29
|
+
- lib/can_has_fixtures
|
30
|
+
- lib/can_has_fixtures/core_ext
|
31
|
+
- lib/can_has_fixtures/core_ext/class.rb
|
32
|
+
- lib/can_has_fixtures/core_ext/fixnum.rb
|
33
|
+
- lib/can_has_fixtures/core_ext.rb
|
34
|
+
- lib/can_has_fixtures/fixturize_mixin.rb
|
35
|
+
- lib/can_has_fixtures/random.rb
|
36
|
+
- lib/can_has_fixtures/util.rb
|
37
|
+
- lib/can_has_fixtures/version.rb
|
38
|
+
- lib/can_has_fixtures.rb
|
39
|
+
has_rdoc: false
|
40
|
+
homepage:
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.0.1
|
62
|
+
signing_key:
|
63
|
+
specification_version: 2
|
64
|
+
summary: Library for generating fixtures.
|
65
|
+
test_files: []
|
66
|
+
|