miniskirt 0.9.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/miniskirt.rb +21 -16
  2. data/miniskirt_test.rb +22 -7
  3. metadata +26 -10
data/miniskirt.rb CHANGED
@@ -1,4 +1,5 @@
1
- require "active_support"
1
+ require 'active_support/inflector'
2
+ require 'active_support/core_ext/hash'
2
3
  # Factory girl, relaxed.
3
4
  #
4
5
  # Factory.define :user do |f|
@@ -11,38 +12,42 @@ require "active_support"
11
12
  # f.user { Factory :user } # Blocks, if you must.
12
13
  # end
13
14
  class Miniskirt < Struct.new(:__klass__)
14
- undef_method *instance_methods.grep(/^(?!__|object_id)/) # BlankerSlate.
15
- @@factories = {} and private_class_method :new
15
+ undef_method *instance_methods.grep(/^(?!__|object_id)/)
16
+ @@attrs = {} and private_class_method :new
16
17
 
17
18
  class << self
18
- def define(name)
19
- @@factories[name = name.to_s] = {} and yield new(name)
19
+ def define name, options = {}
20
+ @@attrs[name = name.to_s] = [{}, options] and yield new(name)
20
21
  end
21
22
 
22
- def build(name, attrs = {})
23
- (name, n = name.to_s) and (m = name.classify.constantize).new do |rec|
24
- attrs.symbolize_keys!.reverse_update(@@factories[name]).each do |k, v|
25
- rec.send "#{k}=", case v when String # Sequence and interpolate.
23
+ def build name, attrs = {}
24
+ (h, opts, n = @@attrs[name = name.to_s]) and klass = opts[:class] || name
25
+ p = opts[:parent] and (h, klass = @@attrs[p = p.to_s][0].merge(h), p)
26
+ (m = klass.is_a?(Class) ? klass : klass.classify.constantize).new do |r|
27
+ attrs.symbolize_keys!.reverse_update(h).each do |k, v|
28
+ r.send "#{k}=", case v when String # Sequence and interpolate.
26
29
  v.sub(/%\d*d/) {|d| d % n ||= m.maximum(:id).to_i + 1} % attrs % n
27
- when Proc then v.call(rec) else v
30
+ when Proc then v.call(r) else v
28
31
  end
29
32
  end
30
33
  end
31
34
  end
32
35
 
33
- def create(name, attrs = {})
36
+ def create name, attrs = {}
34
37
  build(name, attrs).tap { |record| record.save! }
35
38
  end
36
39
  end
37
40
 
38
- def method_missing(name, value = nil, &block)
39
- @@factories[__klass__][name] = block || value
41
+ def method_missing name, value = nil, &block
42
+ @@attrs[__klass__][0][name] = block || value
40
43
  end
41
44
  end
42
45
 
43
- def Miniskirt(name, attrs = {})
46
+ def Miniskirt name, attrs = {}
44
47
  Miniskirt.create(name, attrs)
45
48
  end
46
49
 
47
- Factory = Miniskirt
48
- alias Factory Miniskirt
50
+ unless Object.const_defined? :Factory
51
+ Factory = Miniskirt
52
+ alias Factory Miniskirt
53
+ end
data/miniskirt_test.rb CHANGED
@@ -1,11 +1,11 @@
1
- require 'miniskirt'
1
+ require './miniskirt'
2
2
  require 'test/unit'
3
3
 
4
4
  class MiniskirtTest < Test::Unit::TestCase
5
5
  def test_should_define_factories
6
- factories = Miniskirt.class_variable_get :@@factories
6
+ factories = Miniskirt.class_variable_get :@@attrs
7
7
  assert_not_nil factories["user"]
8
- assert_not_nil factories["post"]
8
+ assert_not_nil factories["blog_entry"]
9
9
  end
10
10
 
11
11
  def test_should_build_object
@@ -60,13 +60,24 @@ class MiniskirtTest < Test::Unit::TestCase
60
60
  def test_should_sequence
61
61
  user1 = Factory.create :user
62
62
  user2 = Factory.create :user
63
- assert_equal user1.login.succ, user2.login
63
+ assert_equal user1.login.sub(/\d+$/) { |n| n.to_i.succ.to_s }, user2.login
64
64
  end
65
65
 
66
66
  def test_should_interpolate
67
67
  user = Factory.create :user
68
68
  assert_equal user.email, "#{user.login}@example.com"
69
69
  end
70
+
71
+ def test_should_inherit
72
+ admin = Factory.create :admin
73
+ assert_equal 'admin', admin.login
74
+ assert_equal 'admin@example.com', admin.email
75
+ end
76
+
77
+ def test_should_alias
78
+ blog_entry = Factory.create :blog_entry
79
+ assert_equal 'admin', blog_entry.user.login
80
+ end
70
81
  end
71
82
 
72
83
  class Mock
@@ -79,7 +90,7 @@ class Mock
79
90
  yield self
80
91
  end
81
92
 
82
- def save
93
+ def save!
83
94
  @@maximum = @@maximum.to_i + 1 unless @saved
84
95
  @saved = true
85
96
  end
@@ -97,12 +108,16 @@ class Post < Mock
97
108
  attr_accessor :user
98
109
  end
99
110
 
111
+ Miniskirt.define :admin, :parent => :user do |f|
112
+ f.login "admin"
113
+ end
114
+
100
115
  Miniskirt.define :user do |f|
101
116
  f.login "johndoe%d"
102
117
  f.email "%{login}@example.com"
103
118
  f.password f.password_confirmation("foobarbaz")
104
119
  end
105
120
 
106
- Miniskirt.define :post do |f|
107
- f.user { Miniskirt :user }
121
+ Miniskirt.define :blog_entry, :class => Post do |f|
122
+ f.user { Miniskirt :admin }
108
123
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: miniskirt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Stephen Celis
@@ -9,19 +14,23 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-02-11 00:00:00 -06:00
17
+ date: 2011-01-02 00:00:00 -06:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: activesupport
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
20
25
  requirements:
21
26
  - - ">="
22
27
  - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 2
23
31
  version: "2.2"
24
- version:
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  description: Test::Unit begot MiniTest; factory_girl begets Miniskirt.
26
35
  email: stephen@stephencelis.com
27
36
  executables: []
@@ -32,8 +41,9 @@ extra_rdoc_files: []
32
41
 
33
42
  files:
34
43
  - miniskirt.rb
44
+ - miniskirt_test.rb
35
45
  has_rdoc: true
36
- homepage: http://gist.github.com/273579
46
+ homepage: http://github.com/stephencelis/miniskirt
37
47
  licenses: []
38
48
 
39
49
  post_install_message:
@@ -42,21 +52,27 @@ rdoc_options: []
42
52
  require_paths:
43
53
  - .
44
54
  required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
45
56
  requirements:
46
57
  - - ">="
47
58
  - !ruby/object:Gem::Version
59
+ segments:
60
+ - 1
61
+ - 8
62
+ - 7
48
63
  version: 1.8.7
49
- version:
50
64
  required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
51
66
  requirements:
52
67
  - - ">="
53
68
  - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
54
71
  version: "0"
55
- version:
56
72
  requirements: []
57
73
 
58
74
  rubyforge_project:
59
- rubygems_version: 1.3.5
75
+ rubygems_version: 1.3.7
60
76
  signing_key:
61
77
  specification_version: 3
62
78
  summary: factory_girl, relaxed