foca-kaleidoscope 0.1.0 → 0.1.1

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.markdown CHANGED
@@ -26,6 +26,24 @@ Example plz?
26
26
  # Don't save values automatically
27
27
  User.factory.new #=> #<User id: nil, ...>
28
28
 
29
+ Generating unique values
30
+ ------------------------
31
+
32
+ Most of the time, our models require certain fields to have unique content. In
33
+ order to generate that with Kaleidoscope, just call `Kaleidoscope.unique`:
34
+
35
+ Kaleidoscope.unique #=> 0
36
+ Kaleidoscope.unique #=> 1
37
+ # and so on, and so forth
38
+
39
+ If you need repeatable values, call `Kaleidoscope.reset_uniques!` and the next
40
+ call to `Kaleidoscope.unique` will return 0 again.
41
+
42
+ You can pass a block to `unique`, in which case we will pass the unique value
43
+ to the block and return whatever the block outputs:
44
+
45
+ Kaleidoscope.unique {|i| "user_#{i}" } #=> "user_0"
46
+
29
47
  How does it work?
30
48
  -----------------
31
49
 
data/lib/kaleidoscope.rb CHANGED
@@ -36,4 +36,25 @@ module Kaleidoscope
36
36
  def self.define(&block)
37
37
  module_eval(&block)
38
38
  end
39
+
40
+ # Generate a unique value each time it's called (starting from zero)
41
+ # if passed a block it passes the value to the block and returns the output
42
+ #
43
+ # Kaleidoscope.factory(User) {{ :login => "user_#{Kaleidoscope.unique}" }}
44
+ #
45
+ # User.factory.create #=> #<User login: "user_0">
46
+ # User.factory.create #=> #<User login: "user_1">
47
+ # ...etc...
48
+ def self.unique(&block)
49
+ @unique_value ||= -1
50
+ @unique_value += 1
51
+ block ||= lambda {|i| i }
52
+ block.call(@unique_value)
53
+ end
54
+
55
+ # Reset the unique generator so the next unique value generated is zero again
56
+ # Useful to run between tests to get predictable values.
57
+ def self.reset_uniques!
58
+ @unique_value = -1
59
+ end
39
60
  end
@@ -36,6 +36,7 @@ class TestKaleidoscope < Test::Unit::TestCase
36
36
  include TestHelpers
37
37
 
38
38
  def setup
39
+ Kaleidoscope.reset_uniques!
39
40
  User.create_table
40
41
  end
41
42
 
@@ -79,4 +80,23 @@ class TestKaleidoscope < Test::Unit::TestCase
79
80
  assert_equal "Foo Bar", user.name
80
81
  assert_equal "foo@bar.com", user.email
81
82
  end
83
+
84
+ def test_it_generates_unique_values
85
+ assert_equal 0, Kaleidoscope.unique
86
+ assert_equal 1, Kaleidoscope.unique
87
+ assert_equal 2, Kaleidoscope.unique
88
+ end
89
+
90
+ def test_passes_unique_values_to_block_if_given
91
+ assert_equal "user_0", Kaleidoscope.unique {|i| "user_#{i}" }
92
+ end
93
+
94
+ def test_it_can_reset_the_unique_generator
95
+ assert_equal 0, Kaleidoscope.unique
96
+ assert_equal 1, Kaleidoscope.unique
97
+
98
+ Kaleidoscope.reset_uniques!
99
+
100
+ assert_equal 0, Kaleidoscope.unique
101
+ end
82
102
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foca-kaleidoscope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Nicol\xC3\xA1s Sanguinetti"