mongo_doc_rails2 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. data/.document +5 -0
  2. data/.gitignore +8 -0
  3. data/HISTORY.md +11 -0
  4. data/LICENSE +20 -0
  5. data/README.textile +185 -0
  6. data/Rakefile +188 -0
  7. data/TODO +40 -0
  8. data/VERSION +1 -0
  9. data/data/.gitignore +2 -0
  10. data/examples/simple_document.rb +46 -0
  11. data/examples/simple_object.rb +34 -0
  12. data/features/collections.feature +9 -0
  13. data/features/embed_hash.feature +16 -0
  14. data/features/finders.feature +76 -0
  15. data/features/indexes.feature +28 -0
  16. data/features/mongodb.yml +7 -0
  17. data/features/mongodoc_base.feature +128 -0
  18. data/features/new_record.feature +36 -0
  19. data/features/partial_updates.feature +95 -0
  20. data/features/removing_documents.feature +68 -0
  21. data/features/saving_an_object.feature +15 -0
  22. data/features/scopes.feature +66 -0
  23. data/features/step_definitions/collection_steps.rb +17 -0
  24. data/features/step_definitions/document_steps.rb +149 -0
  25. data/features/step_definitions/documents.rb +40 -0
  26. data/features/step_definitions/embed_hash_steps.rb +6 -0
  27. data/features/step_definitions/finder_steps.rb +15 -0
  28. data/features/step_definitions/index_steps.rb +10 -0
  29. data/features/step_definitions/json_steps.rb +9 -0
  30. data/features/step_definitions/object_steps.rb +50 -0
  31. data/features/step_definitions/objects.rb +24 -0
  32. data/features/step_definitions/partial_update_steps.rb +31 -0
  33. data/features/step_definitions/query_steps.rb +66 -0
  34. data/features/step_definitions/removing_documents_steps.rb +14 -0
  35. data/features/step_definitions/scope_steps.rb +18 -0
  36. data/features/step_definitions/string_casting_steps.rb +29 -0
  37. data/features/step_definitions/util_steps.rb +7 -0
  38. data/features/string_casting.feature +10 -0
  39. data/features/support/support.rb +10 -0
  40. data/features/using_criteria.feature +142 -0
  41. data/lib/mongo_doc.rb +12 -0
  42. data/lib/mongo_doc/associations.rb +109 -0
  43. data/lib/mongo_doc/associations/collection_proxy.rb +121 -0
  44. data/lib/mongo_doc/associations/document_proxy.rb +65 -0
  45. data/lib/mongo_doc/associations/hash_proxy.rb +102 -0
  46. data/lib/mongo_doc/associations/proxy_base.rb +48 -0
  47. data/lib/mongo_doc/attributes.rb +84 -0
  48. data/lib/mongo_doc/bson.rb +31 -0
  49. data/lib/mongo_doc/collection.rb +82 -0
  50. data/lib/mongo_doc/connection.rb +88 -0
  51. data/lib/mongo_doc/contexts.rb +31 -0
  52. data/lib/mongo_doc/contexts/ids.rb +41 -0
  53. data/lib/mongo_doc/contexts/mongo.rb +272 -0
  54. data/lib/mongo_doc/criteria.rb +70 -0
  55. data/lib/mongo_doc/cursor.rb +32 -0
  56. data/lib/mongo_doc/document.rb +205 -0
  57. data/lib/mongo_doc/ext.rb +16 -0
  58. data/lib/mongo_doc/ext/array.rb +5 -0
  59. data/lib/mongo_doc/ext/binary.rb +7 -0
  60. data/lib/mongo_doc/ext/boolean_class.rb +17 -0
  61. data/lib/mongo_doc/ext/date.rb +19 -0
  62. data/lib/mongo_doc/ext/date_time.rb +17 -0
  63. data/lib/mongo_doc/ext/dbref.rb +7 -0
  64. data/lib/mongo_doc/ext/hash.rb +7 -0
  65. data/lib/mongo_doc/ext/min_max_keys.rb +13 -0
  66. data/lib/mongo_doc/ext/nil_class.rb +5 -0
  67. data/lib/mongo_doc/ext/numeric.rb +17 -0
  68. data/lib/mongo_doc/ext/object.rb +19 -0
  69. data/lib/mongo_doc/ext/object_id.rb +7 -0
  70. data/lib/mongo_doc/ext/regexp.rb +5 -0
  71. data/lib/mongo_doc/ext/string.rb +5 -0
  72. data/lib/mongo_doc/ext/symbol.rb +5 -0
  73. data/lib/mongo_doc/ext/time.rb +9 -0
  74. data/lib/mongo_doc/finders.rb +38 -0
  75. data/lib/mongo_doc/index.rb +46 -0
  76. data/lib/mongo_doc/matchers.rb +35 -0
  77. data/lib/mongo_doc/root.rb +26 -0
  78. data/lib/mongo_doc/scope.rb +64 -0
  79. data/lib/mongo_doc/validations.rb +12 -0
  80. data/lib/mongo_doc/validations/macros.rb +11 -0
  81. data/lib/mongo_doc/validations/validates_embedded.rb +13 -0
  82. data/lib/mongoid/contexts/enumerable.rb +151 -0
  83. data/lib/mongoid/contexts/paging.rb +42 -0
  84. data/lib/mongoid/criteria.rb +239 -0
  85. data/lib/mongoid/criterion/complex.rb +21 -0
  86. data/lib/mongoid/criterion/exclusion.rb +65 -0
  87. data/lib/mongoid/criterion/inclusion.rb +93 -0
  88. data/lib/mongoid/criterion/optional.rb +136 -0
  89. data/lib/mongoid/extensions/hash/criteria_helpers.rb +20 -0
  90. data/lib/mongoid/extensions/symbol/inflections.rb +36 -0
  91. data/lib/mongoid/matchers/all.rb +11 -0
  92. data/lib/mongoid/matchers/default.rb +26 -0
  93. data/lib/mongoid/matchers/exists.rb +13 -0
  94. data/lib/mongoid/matchers/gt.rb +11 -0
  95. data/lib/mongoid/matchers/gte.rb +11 -0
  96. data/lib/mongoid/matchers/in.rb +11 -0
  97. data/lib/mongoid/matchers/lt.rb +11 -0
  98. data/lib/mongoid/matchers/lte.rb +11 -0
  99. data/lib/mongoid/matchers/ne.rb +11 -0
  100. data/lib/mongoid/matchers/nin.rb +11 -0
  101. data/lib/mongoid/matchers/size.rb +11 -0
  102. data/mongo_doc_rails2.gemspec +237 -0
  103. data/mongod.example.yml +2 -0
  104. data/mongodb.example.yml +14 -0
  105. data/perf/mongo_doc_object.rb +83 -0
  106. data/perf/mongo_document.rb +84 -0
  107. data/perf/ruby_driver.rb +49 -0
  108. data/script/console +8 -0
  109. data/spec/array_including_argument_matcher.rb +62 -0
  110. data/spec/associations/collection_proxy_spec.rb +233 -0
  111. data/spec/associations/document_proxy_spec.rb +45 -0
  112. data/spec/associations/hash_proxy_spec.rb +181 -0
  113. data/spec/associations/proxy_base_spec.rb +92 -0
  114. data/spec/associations_spec.rb +218 -0
  115. data/spec/attributes_accessor_spec.rb +33 -0
  116. data/spec/attributes_spec.rb +145 -0
  117. data/spec/bson_matchers.rb +54 -0
  118. data/spec/bson_spec.rb +196 -0
  119. data/spec/collection_spec.rb +169 -0
  120. data/spec/connection_spec.rb +147 -0
  121. data/spec/contexts/ids_spec.rb +49 -0
  122. data/spec/contexts/mongo_spec.rb +235 -0
  123. data/spec/contexts_spec.rb +56 -0
  124. data/spec/criteria_spec.rb +69 -0
  125. data/spec/cursor_spec.rb +91 -0
  126. data/spec/document_ext.rb +9 -0
  127. data/spec/document_spec.rb +553 -0
  128. data/spec/embedded_save_spec.rb +73 -0
  129. data/spec/ext_spec.rb +89 -0
  130. data/spec/finders_spec.rb +61 -0
  131. data/spec/hash_matchers.rb +27 -0
  132. data/spec/index_spec.rb +79 -0
  133. data/spec/matchers_spec.rb +342 -0
  134. data/spec/mongodb.yml +6 -0
  135. data/spec/mongodb_pairs.yml +8 -0
  136. data/spec/new_record_spec.rb +128 -0
  137. data/spec/root_spec.rb +41 -0
  138. data/spec/scope_spec.rb +79 -0
  139. data/spec/spec.opts +2 -0
  140. data/spec/spec_helper.rb +14 -0
  141. data/spec/validations_spec.rb +30 -0
  142. metadata +346 -0
@@ -0,0 +1,68 @@
1
+ Feature: Removing Documents
2
+
3
+ Background:
4
+ Given an empty Contact document collection
5
+ And a Contact document named 'hashrocket' :
6
+ | Name | Type | Note |
7
+ | Hashrocket | company | Premier Rails development shop! |
8
+ And 'hashrocket' has interests, an array of:
9
+ | Interest |
10
+ | ruby |
11
+ | rails |
12
+ | employment |
13
+ | contract work |
14
+ | restaurants |
15
+ | hotels |
16
+ | flights |
17
+ | car rentals |
18
+ And 'hashrocket' has many addresses :
19
+ | Street | City | State | Zip Code |
20
+ | 320 First Street North | Jacksonville Beach | FL | 32250 |
21
+ | 1 Lake Michigan Street | Chicago | IL | 60611 |
22
+ | 1 Main Street | Santiago | Chile | |
23
+ And I save the document 'hashrocket'
24
+ And a Contact document named 'rocketeer' :
25
+ | Name | Note |
26
+ | Rocketeer Mike | Fantastic developer |
27
+ And 'rocketeer' has interests, an array of:
28
+ | Interest |
29
+ | ruby |
30
+ | rails |
31
+ | restaurants |
32
+ | employment |
33
+ And 'rocketeer' has many addresses :
34
+ | Street | City | State | Zip Code |
35
+ | 1 Main Street | Atlantic Beach | FL | 32233 |
36
+ And I save the document 'rocketeer'
37
+ And a Contact document named 'contractor' :
38
+ | Name | Note |
39
+ | Contractor Joe | Knows MongoDB |
40
+ And 'contractor' has interests, an array of:
41
+ | Interest |
42
+ | ruby |
43
+ | rails |
44
+ | contract work |
45
+ | flights |
46
+ | car rentals |
47
+ | hotels |
48
+ | restaurants |
49
+ And 'contractor' has many addresses :
50
+ | Street | City | State | Zip Code |
51
+ | 1 Main St. | Jacksonville | FL | 32218 |
52
+ And I save the document 'contractor'
53
+ And a Place document named 'hashrocket_hq' :
54
+ | Name | Type |
55
+ | Hashrocket | company |
56
+ And 'hashrocket_hq' has one Address as address (identified by 'hq_address'):
57
+ | Street | City | State | Zip Code |
58
+ | 1 Main St. | Jacksonville | FL | 32218 |
59
+ And I save the document 'hashrocket_hq'
60
+
61
+ Scenario: Simple Remove
62
+ Given the document 'contractor' roundtrips
63
+ When I remove 'contractor'
64
+ Then the document 'contractor' is not found
65
+
66
+ Scenario: Embedded Remove
67
+ When the document 'hashrocket_hq' roundtrips
68
+ Then an exception is raised if I remove 'hq_address'
@@ -0,0 +1,15 @@
1
+ Feature: saving an object
2
+
3
+ Scenario: saving simple json
4
+ Given a new collection named 'test'
5
+ When I save the json '{"name":"name"}'
6
+ Then the collection should have 1 document
7
+ And the json '{"name":"name"}' roundtrips
8
+
9
+ Scenario: saving a ruby object
10
+ Given a new collection named 'test'
11
+ And an object 'movie'
12
+ When I save the object 'movie'
13
+ Then the collection should have 1 document
14
+ And the object 'movie' roundtrips
15
+
@@ -0,0 +1,66 @@
1
+ Feature: Named Scopes
2
+
3
+ Background:
4
+ Given an empty Contact document collection
5
+ And a Contact document named 'hashrocket' :
6
+ | Name | Type |
7
+ | Hashrocket | company |
8
+ And 'hashrocket' has interests, an array of:
9
+ | Interest |
10
+ | ruby |
11
+ | rails |
12
+ | employment |
13
+ | contract work |
14
+ | restaurants |
15
+ | hotels |
16
+ | flights |
17
+ | car rentals |
18
+ And 'hashrocket' has many addresses :
19
+ | Street | City | State | Zip Code |
20
+ | 320 First Street North | Jacksonville Beach | FL | 32250 |
21
+ | 1 Lake Michigan Street | Chicago | IL | 60611 |
22
+ | 1 Main Street | Santiago | Chile | |
23
+ And I save the document 'hashrocket'
24
+ And a Contact document named 'rocketeer' :
25
+ | Name |
26
+ | Rocketeer Mike |
27
+ And 'rocketeer' has interests, an array of:
28
+ | Interest |
29
+ | ruby |
30
+ | rails |
31
+ | restaurants |
32
+ | employment |
33
+ And 'rocketeer' has many addresses :
34
+ | Street | City | State | Zip Code |
35
+ | 1 Main Street | Atlantic Beach | FL | 32233 |
36
+ And I save the document 'rocketeer'
37
+ And a Contact document named 'contractor' :
38
+ | Name |
39
+ | Contractor Joe |
40
+ And 'contractor' has interests, an array of:
41
+ | Interest |
42
+ | ruby |
43
+ | rails |
44
+ | contract work |
45
+ | flights |
46
+ | car rentals |
47
+ | hotels |
48
+ | restaurants |
49
+ And 'contractor' has many addresses :
50
+ | Street | City | State | Zip Code |
51
+ | 1 Main St. | Jacksonville | FL | 32218 |
52
+ And I save the document 'contractor'
53
+
54
+ Scenario: Simple named scope
55
+ When I query contacts with scope 'rubyists'
56
+ Then the query result has 3 documents
57
+
58
+ Scenario: Simple chained scope
59
+ When I query contacts with scopes 'rubyists, contract_work'
60
+ Then the query result has 2 documents
61
+ And one of the query results is the document 'contractor'
62
+
63
+ Scenario: Named scope with lambda
64
+ When I query contacts with lambda scope 'in_state' with parameters 'IL'
65
+ Then the query result has 1 documents
66
+ And one of the query results is the document 'hashrocket'
@@ -0,0 +1,17 @@
1
+ Given /a new collection named '(.*)'/ do |name|
2
+ MongoDoc::Connection.database.drop_collection(name)
3
+ @collection = MongoDoc::Collection.new(name)
4
+ end
5
+
6
+ Given /^an empty (\w+) collection$/ do |name|
7
+ MongoDoc::Connection.database.drop_collection(name)
8
+ MongoDoc::Connection.database.create_collection(name, :strict => true)
9
+ end
10
+
11
+ Then /the collection should have (\d+) documents?/ do |count|
12
+ @collection.count.should == count.to_i
13
+ end
14
+
15
+ When "I query the collection '$collection_name' with the criteria $criteria" do |collection_name, criteria|
16
+ @query = eval("@collection.#{criteria}")
17
+ end
@@ -0,0 +1,149 @@
1
+ Given /^an empty (\w+) document collection$/ do |doc|
2
+ klass = doc.constantize
3
+ Given "an empty #{klass.collection_name} collection"
4
+ end
5
+
6
+ Given /^an? (\w+) document named '(.*)' :$/ do |doc, name, table|
7
+ @all = []
8
+ klass = doc.constantize
9
+ table.hashes.each do |hash|
10
+ @last = klass.new
11
+ hash.each do |attr, value|
12
+ @last.send("#{attr.underscore.gsub(' ', '_')}=", value)
13
+ end
14
+ @all << @last
15
+ end
16
+ instance_variable_set("@#{name}", @last)
17
+ end
18
+
19
+ Given /^'(.*)' has one (.*?) as (.*) :$/ do |doc_name, class_name, assoc_name, table|
20
+ doc = instance_variable_get("@#{doc_name}")
21
+ obj = class_name.constantize.new
22
+ table.hashes.each do |hash|
23
+ hash.each do |key, value|
24
+ obj.send("#{key.underscore.gsub(' ', '_')}=", value)
25
+ end
26
+ end
27
+ doc.send("#{assoc_name.underscore.gsub(' ', '_')}=", obj)
28
+ @last = obj
29
+ end
30
+
31
+ Given /^'(.*)' has (?:a|an|many) (.*) :$/ do |doc_name, assoc_name, table|
32
+ doc = instance_variable_get("@#{doc_name}")
33
+ table.hashes.each do |hash|
34
+ doc.send(assoc_name).build(hash.inject({}) do |attrs, (attr, value)|
35
+ attrs["#{attr.underscore.gsub(' ', '_')}"] = value
36
+ attrs
37
+ end)
38
+ end
39
+ @all = doc.send(assoc_name)
40
+ @last = @all.last
41
+ end
42
+
43
+ Given /^I set the id on the document '(.*)' to (.*)$/ do |doc_name, value|
44
+ doc = instance_variable_get("@#{doc_name}")
45
+ doc._id = BSON::ObjectID.from_string("%024x" % value.to_i(16))
46
+ end
47
+
48
+ Given /^'(.+)' has one (.+?) as (.+?) \(identified by '(.+)'\):$/ do |doc_name, class_name, assoc_name, var_name, table|
49
+ doc = instance_variable_get("@#{doc_name}")
50
+ obj = class_name.constantize.new
51
+ table.hashes.each do |hash|
52
+ hash.each do |key, value|
53
+ obj.send("#{key.underscore.gsub(' ', '_')}=", value)
54
+ end
55
+ end
56
+ instance_variable_set("@#{var_name}", obj)
57
+ doc.send("#{assoc_name.underscore.gsub(' ', '_')}=", obj)
58
+ @last = obj
59
+ end
60
+
61
+ When /^I save the document '(.*)'$/ do |name|
62
+ object = instance_variable_get("@#{name}")
63
+ @last_return = object.save
64
+ end
65
+
66
+ When /^I save the last document$/ do
67
+ @last_return = @last.save
68
+ end
69
+
70
+ When /^I create an (.*) '(.*)' from the hash '(.*)'$/ do |doc, name, hash|
71
+ klass = doc.constantize
72
+ attrs = instance_variable_get("@#{hash}")
73
+ instance_variable_set("@#{name}", klass.create(attrs))
74
+ end
75
+
76
+ When /^I update the document '(.*)' with the hash named '(.*)'$/ do |doc_name, hash_name|
77
+ doc = instance_variable_get("@#{doc_name}")
78
+ attrs = instance_variable_get("@#{hash_name}")
79
+ @last_return = doc.update_attributes(attrs)
80
+ end
81
+
82
+ When /^I query (.*) with criteria (.*)$/ do |doc, criteria_text|
83
+ klass = doc.singularize.camelize
84
+ @query = @last_return = eval("#{klass}.criteria.#{criteria_text}")
85
+ end
86
+
87
+ When /^I query (.*) with the '(.*)' id$/ do |doc, name|
88
+ klass = doc.singularize.camelize.constantize
89
+ doc = instance_variable_get("@#{name}")
90
+ @query = @last_return = klass.criteria.id(doc.id).entries
91
+ end
92
+
93
+ When /^I find a (.*) using the id of '(.*)'$/ do |type, doc_name|
94
+ klass = type.camelize.constantize
95
+ doc = instance_variable_get("@#{doc_name}")
96
+ @last_return = klass.find(doc.id)
97
+ end
98
+
99
+ When /^'(.+)' is the first (.+?) of '(.+)'$/ do |var_name, single_assoc, doc_name|
100
+ doc = instance_variable_get("@#{doc_name}")
101
+ instance_variable_set("@#{var_name}", doc.send(single_assoc.pluralize).first)
102
+ end
103
+
104
+ Then /^'(.*)' is not a new record$/ do |name|
105
+ instance_variable_get("@#{name}").new_record?.should be_false
106
+ end
107
+
108
+ Then /the (.*) collection should have (\d+) documents?/ do |doc, count|
109
+ klass = doc.constantize
110
+ klass.count.should == count.to_i
111
+ end
112
+
113
+ Then /^the document '(.*)' roundtrips$/ do |name|
114
+ object = instance_variable_get("@#{name}")
115
+ from_db = object.class.find_one(object._id)
116
+ from_db.should == object
117
+ instance_variable_set("@#{name}", from_db)
118
+ end
119
+
120
+ Then /^the document '(.+)' does not roundtrip$/ do |name|
121
+ object = instance_variable_get("@#{name}")
122
+ from_db = object.class.find_one(object._id)
123
+ from_db.should_not == object
124
+ end
125
+
126
+ Then /^the last return value is (.+)$/ do |bool_val|
127
+ @last_return.should send("be_#{bool_val}")
128
+ end
129
+
130
+ Then /^the first (.*) of '(.*)' is not a new record$/ do |assoc, name|
131
+ object = instance_variable_get("@#{name}")
132
+ plural = assoc.pluralize
133
+ object.send(plural).first.should_not be_new_record
134
+ end
135
+
136
+ Then /^the (\w*) of '(.*)' is not a new record$/ do |assoc, name|
137
+ object = instance_variable_get("@#{name}")
138
+ object.send(assoc).should_not be_new_record
139
+ end
140
+
141
+ Then /^the (\w*) of '(.*)' roundtrips$/ do |assoc, name|
142
+ object = instance_variable_get("@#{name}")
143
+ from_db = object.class.find_one(object._id)
144
+ object.send(assoc).id.should == from_db.send(assoc).id
145
+ end
146
+
147
+ Then /^the size of the last return value is (.*)$/ do |count|
148
+ @last_return.size.should == count.to_i
149
+ end
@@ -0,0 +1,40 @@
1
+ class Address
2
+ include MongoDoc::Document
3
+
4
+ attr_accessor :street
5
+ attr_accessor :city
6
+ attr_accessor :state
7
+ attr_accessor :zip_code
8
+ end
9
+
10
+ class Place
11
+ include MongoDoc::Document
12
+
13
+ attr_accessor :name
14
+ attr_accessor :type
15
+ embed :address
16
+ end
17
+
18
+ class Contact
19
+ include MongoDoc::Document
20
+
21
+ attr_accessor :name
22
+ attr_accessor :type
23
+ attr_accessor :note
24
+ attr_accessor :interests
25
+ embed_many :addresses
26
+
27
+ scope :rubyists, any_in(:interests => ['ruby'])
28
+ scope :contract_work, any_in(:interests => ['contract work'])
29
+ scope :in_state, lambda {|state| where('addresses.state' => state)}
30
+ end
31
+
32
+ class Event
33
+ include MongoDoc::Document
34
+
35
+ attr_accessor :name
36
+ attr_accessor :venue
37
+ attr_accessor :date, :type => Date
38
+
39
+ embed_hash :addresses
40
+ end
@@ -0,0 +1,6 @@
1
+ Given /^I put the '(\w+)' object on key '(\w+)' of the '(\w+)' hash of '(\w+)'$/ do |value_name, key, assoc, doc_name|
2
+ value = instance_variable_get("@#{value_name}")
3
+ doc = instance_variable_get("@#{doc_name}")
4
+ doc.send(assoc)[key] = value
5
+ end
6
+
@@ -0,0 +1,15 @@
1
+ def finder_query=(finder)
2
+ @query = finder
3
+ end
4
+
5
+ When /^I query (.+) with (\w+)$/ do |doc, finder|
6
+ self.finder_query = klass(doc).send(finder)
7
+ end
8
+
9
+ When /^I query (.+) to find_one with the (.+) of the '(.+)' document$/ do |collection, id, doc_name|
10
+ self.finder_query = klass(collection).find_one(instance_variable_get("@#{doc_name}").send(id))
11
+ end
12
+
13
+ Then /^the query result was (\d+) documents$/ do |count|
14
+ query.should == count.to_i
15
+ end
@@ -0,0 +1,10 @@
1
+ When /^I create an index named (.*) on the (.*) collection$/ do |index_name, doc|
2
+ klass = doc.constantize
3
+ klass.index(index_name)
4
+ end
5
+
6
+ Then /^there is an index on (.*) on the (.*) collection$/ do |index_name, doc|
7
+ klass = doc.constantize
8
+ klass.collection.index_information.should include("#{index_name}_1")
9
+ end
10
+
@@ -0,0 +1,9 @@
1
+ When /^I save the json '(\{.*\})'$/ do |json_text|
2
+ json = JSON.parse(json_text)
3
+ @last_save = @collection.save(json)
4
+ end
5
+
6
+ Then /^the json '(\{.*\})' roundtrips$/ do |json_text|
7
+ json = JSON.parse(json_text)
8
+ @collection.find_one(@last_save).should be_mongo_eql(json, false)
9
+ end
@@ -0,0 +1,50 @@
1
+ Given /^an object '(.*)'$/ do |name|
2
+ @movie = Movie.new
3
+ @movie.title = 'Gone with the Wind'
4
+ @movie.director = 'Victor Fleming'
5
+ @movie.writers = ['Sidney Howard']
6
+ @director = Director.new
7
+ @director.name = 'Victor Fleming'
8
+ @award = AcademyAward.new
9
+ @award.year = '1940'
10
+ @award.category = 'Best Director'
11
+ @director.awards = [@award]
12
+ @movie.director = @director
13
+ end
14
+
15
+ Given /^a hash named '(.*)':$/ do |name, table|
16
+ @all = []
17
+ table.hashes.each do |hash|
18
+ @last = hash.inject({}) do |h, (key, value)|
19
+ h["#{key.underscore.gsub(' ', '_')}"] = value
20
+ h
21
+ end
22
+ @all << @last
23
+ end
24
+ instance_variable_set("@#{name}", @last)
25
+ end
26
+
27
+ Given /^'(.*)' has (.*), an array of:$/ do |name, attribute, table|
28
+ object = instance_variable_get("@#{name}")
29
+ object.send(attribute + "=", [])
30
+ table.hashes.each do |hash|
31
+ hash.each {|key, value| object.send(attribute) << value}
32
+ end
33
+ end
34
+
35
+
36
+ When /^I save the object '(.*)'$/ do |name|
37
+ object = instance_variable_get("@#{name}")
38
+ @last_save = @collection.save(object)
39
+ end
40
+
41
+ Then /^the object '(.*)' roundtrips$/ do |name|
42
+ object = instance_variable_get("@#{name}")
43
+ object.instance_variable_set("@_id", @last_save)
44
+ @collection.find_one(@last_save).should == object
45
+ end
46
+
47
+ Then /^the attribute '(.*)' of '(.*)' is '(.*)'$/ do |attr, var, value|
48
+ object = instance_variable_get("@#{var}")
49
+ object.send(attr).to_s.should == value
50
+ end