foca-kaleidoscope 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,75 @@
1
+ Kaleidoscope
2
+ ============
3
+
4
+ **Simple** and **clean** object factories for ActiveRecord objects.
5
+
6
+ Kaleidoscopes are ridiculously simple objects, and they provide us with
7
+ beautiful images with almost no work on our part.
8
+
9
+ Example plz?
10
+ ------------
11
+
12
+ Kaleidoscope.define do
13
+ factory(User) {{ :name => "Foo Bar", :email => "foo@bar.com", :password => "test" }}
14
+ factory(User, :admin) {{ :admin => true }}
15
+ factory(User, :named) do
16
+ lambda {|name| { :conditions => { :name => name }}}
17
+ end
18
+ end
19
+
20
+ User.factory.create #=> #<User id: 1, name: "Foo Bar", email: "foo@bar.com", password: "test", admin: false>
21
+ User.admin.create #=> #<User id: 2, name: nil, email: nil, password: nil, admin: true>
22
+
23
+ # Overwriting values
24
+ User.factory.create(:name => "foca") #=> #<User id: 3, name: "foca", email: "foo@bar.com", password: "test", admin: false>
25
+
26
+ # Don't save values automatically
27
+ User.factory.new #=> #<User id: nil, ...>
28
+
29
+ How does it work?
30
+ -----------------
31
+
32
+ It just uses named scopes behind the scenes. Each factory declaration defines
33
+ a new named scope with the provided name (or `factory` by default).
34
+
35
+ This means we don't touch *any* class. No methods are added on
36
+ `ActiveRecord::Base`.
37
+
38
+ If the named scope already exists (or you have a class method with that
39
+ same name), the factory declaration will fail with an `ArgumentError`.
40
+
41
+ But enough README, read the code, the factory declaration is 5 lines of code.
42
+ Literally.
43
+
44
+ Why?
45
+ ----
46
+
47
+ Just a thought experiment, but it might turn out to be useful after all. All
48
+ credit goes to [Jeremy McAnally][jeremy] for the original idea.
49
+
50
+ License
51
+ -------
52
+
53
+ Copyright (c) 2009 [Jeremy McAnally][jeremy] & [Nicolás Sanguinetti][foca] for [entp][].
54
+
55
+ Permission is hereby granted, free of charge, to any person obtaining a copy
56
+ of this software and associated documentation files (the "Software"), to deal
57
+ in the Software without restriction, including without limitation the rights
58
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
59
+ copies of the Software, and to permit persons to whom the Software is
60
+ furnished to do so, subject to the following conditions:
61
+
62
+ The above copyright notice and this permission notice shall be included in
63
+ all copies or substantial portions of the Software.
64
+
65
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
66
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
67
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
68
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
69
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
70
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
71
+ THE SOFTWARE.
72
+
73
+ [jeremy]: http://github.com/jeremymcanally
74
+ [foca]: http://github.com/foca
75
+ [entp]: http://entp.com
@@ -0,0 +1,39 @@
1
+ require "rubygems"
2
+ require "activerecord"
3
+
4
+ module Kaleidoscope
5
+ # Setup simple and clean factories for activerecord using named_scopes
6
+ # (no touching of the internals!)
7
+ #
8
+ # Kaleidoscope.factory(User) {{
9
+ # :email => Faker::Internet.email,
10
+ # :password => "test",
11
+ # :password_confirmation => "test"
12
+ # }}
13
+ #
14
+ # User.kaleidoscope.create #=> #<User id: 1, email: "foo@bar.com", ...>
15
+ # User.kaleidoscope.new #=> #<User id: nil, email: "bar@foo.com" ...>
16
+ #
17
+ # Kaleidoscope.factory(User, :admin) {{
18
+ # :admin => true
19
+ # }}
20
+ #
21
+ # User.admin.create #=> #<User id: 1, admin: true, ...>
22
+ def self.factory(model, name=nil, &block)
23
+ name ||= :factory
24
+ raise ArgumentError, "#{model.name} already has a #{name} method" if model.respond_to?(name)
25
+ definition = block.call
26
+ definition = { :conditions => definition } if Hash === definition
27
+ model.named_scope name, definition
28
+ end
29
+
30
+ # Call this to define multiple factories together. Only for aesthetic reasons.
31
+ #
32
+ # Kaleidoscope.define do
33
+ # factory(User, :admin) {{ :admin => true }}
34
+ # factory(User, :inactive) {{ :state => 'pending' }}
35
+ # end
36
+ def self.define(&block)
37
+ module_eval(&block)
38
+ end
39
+ end
@@ -0,0 +1,82 @@
1
+ require "test/unit"
2
+ require File.dirname(__FILE__) + "/../lib/kaleidoscope"
3
+
4
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
5
+
6
+ module TestHelpers
7
+ class User < ActiveRecord::Base
8
+ named_scope :lives_in, lambda {|country| { :conditions => { :country => country }}}
9
+
10
+ def self.create_table
11
+ connection.create_table table_name do |t|
12
+ t.string :name
13
+ t.string :email
14
+ t.string :password
15
+ t.string :country
16
+ t.boolean :admin, :default => false
17
+ t.timestamps
18
+ end
19
+ end
20
+
21
+ def self.drop_table
22
+ connection.drop_table table_name
23
+ end
24
+ end
25
+
26
+ Kaleidoscope.define do
27
+ factory(User) {{ :name => "Foo Bar", :email => "foo@bar.com", :password => "test" }}
28
+ factory(User, :admin) {{ :admin => true }}
29
+ factory(User, :named) do
30
+ lambda {|name| { :conditions => { :name => name }}}
31
+ end
32
+ end
33
+ end
34
+
35
+ class TestKaleidoscope < Test::Unit::TestCase
36
+ include TestHelpers
37
+
38
+ def setup
39
+ User.create_table
40
+ end
41
+
42
+ def teardown
43
+ User.drop_table
44
+ end
45
+
46
+ def test_models_are_created_with_correct_attributes
47
+ user = User.factory.create
48
+
49
+ assert_equal "Foo Bar", user.name
50
+ assert_equal "foo@bar.com", user.email
51
+ assert_equal "test", user.password
52
+ assert !user.admin?
53
+ end
54
+
55
+ def test_you_can_overwrite_attributes_the_normal_activerecord_way
56
+ user = User.admin.create(:name => "John")
57
+
58
+ assert_equal "John", user.name
59
+ assert user.admin?
60
+ end
61
+
62
+ def test_models_can_chain_factory_scopes
63
+ user = User.admin.named("John").create
64
+
65
+ assert_equal "John", user.name
66
+ assert user.admin?
67
+ end
68
+
69
+ def test_raises_when_you_declare_duplicate_factory
70
+ assert_raise ArgumentError do
71
+ Kaleidoscope.factory(User) {{ :name => "Bar Foo" }}
72
+ end
73
+ end
74
+
75
+ def test_works_with_existing_named_scopes_in_the_model
76
+ user = User.factory.lives_in("Uruguay").create
77
+
78
+ assert_equal "Uruguay", user.country
79
+ assert_equal "Foo Bar", user.name
80
+ assert_equal "foo@bar.com", user.email
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foca-kaleidoscope
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - "Nicol\xC3\xA1s Sanguinetti"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-22 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.2.2
23
+ version:
24
+ description: Easy and clean model factories for active record
25
+ email: contacto@nicolassanguinetti.info
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - README.markdown
34
+ - lib/kaleidoscope.rb
35
+ - test/test_kaleidoscope.rb
36
+ has_rdoc: false
37
+ homepage:
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project: kaleidoscope
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: Easy and clean model factories for active record
62
+ test_files: []
63
+