scheherazade 0.0.1 → 0.0.2

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/README.md CHANGED
@@ -8,22 +8,22 @@ Scheherazade
8
8
 
9
9
  * imagines plausible characters (creates valid objects automatically)
10
10
  * keeps track of her story (reuse objects within a given context)
11
- * isn't wearing much (minimal DSL, no instance_eval)
11
+ * isn't wearing much (minimal DSL, no `instance_eval`)
12
12
 
13
13
  ## Simple Example
14
14
 
15
- # Say we have a model like:
16
- class Department
17
- belongs_to :company
18
- has_many :employees
19
- validates_presence_of :company, :name
20
- end
21
-
22
- # Without any configuration, if we write in a test:
23
- story.imagine(Department) # creates a Department,
24
- # with a default name,
25
- # associated to a new Company
26
- story.imagine(Department) # creates another Department
15
+ # Say we have a model like:
16
+ class Department
17
+ belongs_to :company
18
+ has_many :employees
19
+ validates_presence_of :company, :name
20
+ end
21
+
22
+ # Without any configuration, if we write in a test:
23
+ story.imagine(Department) # creates a Department,
24
+ # with a default name,
25
+ # associated to a new Company
26
+ story.imagine(Department) # creates another Department
27
27
  # with another name
28
28
  # and associated to the same Company
29
29
 
@@ -33,7 +33,7 @@ For FactoryGirl (or Machinist) users: a Factory (or Blueprint) corresponds loose
33
33
 
34
34
  ### Characters (Models)
35
35
 
36
- Scheherazade creates ActiveRecord objects. The types of objects are called a 'character'. A generic character could be a `User` (or equivalently `:user`) or there could be more specialized characters (say `:admin`).
36
+ Scheherazade creates ActiveRecord objects. The types of objects are called a *character*. A generic character could be a `User` (or equivalently `:user`) or there could be more specialized characters (say `:admin`).
37
37
 
38
38
  All your models have a default character type; you can use the class directly or the corresponding symbol. Specialized characters must be defined within the current story before they can be used.
39
39
 
@@ -61,37 +61,31 @@ Scheherazade is meant to testing and has a logging feature that makes it easier
61
61
 
62
62
  ## Documentation
63
63
 
64
- The main class is `Story` and there are 2 important methods: Story#imagine and Story#fill. Story#get is a simple shorthand to get the current character or create it if there isn't any.
64
+ The main class is `Story` and there are 2 important methods: `Story#imagine` and `Story#fill`. `Story#get` is a simple shorthand to get the current character or create it if there isn't any.
65
65
 
66
66
  ## Complete example
67
67
 
68
- class User
69
- belongs_to :blog
70
- validates_presence_of :first_name, :last_name
71
- end
72
-
73
- class Blog
74
- has_many :users
75
- has_one :admin, :class_name => "User", :condition => {:admin => true}
76
- validates_presence_of :admin
68
+ class User
69
+ belongs_to :blog
70
+ validates_presence_of :first_name, :last_name
71
+ end
77
72
 
78
- has_many :posts
79
- end
73
+ class Blog
74
+ has_many :users
75
+ has_one :admin, :class_name => "User", :condition => {:admin => true}
76
+ validates_presence_of :admin
80
77
 
81
- story.instance_eval do
82
- fill User, :email do
83
- fill :admin, :admin => true,
84
- :nickname => ->(user, sequence){"The boss #{sequence}"}
78
+ has_many :posts
85
79
  end
86
80
 
87
- fill Blog, :posts
88
- end
89
-
90
- ## To do
81
+ story.instance_eval do
82
+ fill User, :email do
83
+ fill :admin, :admin => true,
84
+ :nickname => ->(user, sequence){"The boss #{sequence}"}
85
+ end
91
86
 
92
- Configurable automatic attributes
93
- Finish doc
94
- Finish specs
87
+ fill Blog, :posts
88
+ end
95
89
 
96
90
  ## Why? Scheherazade vs FactoryGirl vs Machinist
97
91
 
@@ -131,6 +125,26 @@ And then execute:
131
125
 
132
126
  $ bundle
133
127
 
128
+ To avoid pollution from one test/spec to another, you should start and end a story before each. For example with Rspec:
129
+
130
+ RSpec.configure do |config|
131
+ config.before(:each) do
132
+ story.begin
133
+ end
134
+
135
+ config.after(:each) do
136
+ story.end
137
+ end
138
+
139
+ Not sure if future versions should probably support that out of the box...?
140
+
141
+ ## To do
142
+
143
+ Configurable automatic attributes
144
+ Finish support for associations with integers/arrays
145
+ Finish doc
146
+ Finish specs
147
+
134
148
  ## Contributing
135
149
 
136
150
  1. Fork it
@@ -3,7 +3,7 @@ module Scheherazade
3
3
  attr_reader :fill_attributes, :characters, :counter, :current
4
4
 
5
5
  def self.current
6
- Thread.current[:scheherazade_stories].last
6
+ (Thread.current[:scheherazade_stories] ||= []).last || TOP
7
7
  end
8
8
 
9
9
  # Begins a story within the current story.
@@ -49,6 +49,8 @@ module Scheherazade
49
49
  @built = []
50
50
  end
51
51
 
52
+ TOP = new(nil)
53
+
52
54
  # Creates a character with the given attributes
53
55
  #
54
56
  # A character can be designated either by the model (e.g. `User`), the corresponding
@@ -142,6 +144,9 @@ module Scheherazade
142
144
  @after_imagine[current_fill] = block
143
145
  end
144
146
 
147
+ alias_method :==, :equal?
148
+ alias_method :eql?, :equal?
149
+
145
150
  protected
146
151
  def current_fill
147
152
  @filling.last or raise "Expected to be inside a story.fill"
@@ -165,5 +170,3 @@ module Scheherazade
165
170
 
166
171
  end
167
172
  end
168
-
169
- Scheherazade::Story.begin
@@ -1,3 +1,3 @@
1
1
  module Scheherazade
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scheherazade
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -91,7 +91,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
91
91
  version: '0'
92
92
  segments:
93
93
  - 0
94
- hash: -3557737727214523228
94
+ hash: -2747982078447649607
95
95
  required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  none: false
97
97
  requirements:
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  version: '0'
101
101
  segments:
102
102
  - 0
103
- hash: -3557737727214523228
103
+ hash: -2747982078447649607
104
104
  requirements: []
105
105
  rubyforge_project:
106
106
  rubygems_version: 1.8.24