candy 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -84,23 +84,19 @@ module Candy
84
84
  Wrapper.wrap(c).should == c
85
85
  end
86
86
 
87
- it "can wrap a symbol in a way that preserves its symbolic nature" do
88
- Wrapper.wrap(:oldglory).should == "__sym_oldglory"
89
- end
90
-
91
87
  it "wraps an array recursively" do
92
88
  a = [5, 'hi', [':symbol', 0], nil]
93
89
  Wrapper.wrap(a).should == a
94
90
  end
95
91
 
96
- it "wraps a hash's keys" do
92
+ it "wraps keys lightly" do
97
93
  h = {"foo" => "bar", :yoo => "yar"}
98
- Wrapper.wrap(h).keys.should == ["foo", "__sym_yoo"]
94
+ Wrapper.wrap(h).keys.should == ["'foo'", "yoo"]
99
95
  end
100
96
 
101
97
  it "wraps a hash's values" do
102
98
  h = {:foo => :bar, :yoo => [:yar, 5]}
103
- Wrapper.wrap(h).values.should == ["__sym_bar", ["__sym_yar", 5]]
99
+ Wrapper.wrap(h).values.should == [:bar, [:yar, 5]]
104
100
  end
105
101
 
106
102
  it "rejects procs" do
@@ -161,9 +157,6 @@ module Candy
161
157
  Wrapper.unwrap(5).should == 5
162
158
  end
163
159
 
164
- it "turns symbolized strings back into symbols" do
165
- Wrapper.unwrap("__sym_blah").should == :blah
166
- end
167
160
 
168
161
  it "turns hashed objects back into objects" do
169
162
  obj = Wrapper.unwrap(@wrapped)
@@ -174,14 +167,14 @@ module Candy
174
167
  end
175
168
 
176
169
  it "traverses a hash and unwraps whatever it needs to" do
177
- hash = {"__sym_foo" => "__sym_bar", "missile" => @wrapped}
170
+ hash = {"foo" => :bar, "'missile'" => @wrapped}
178
171
  unwrapped = Wrapper.unwrap(hash)
179
172
  unwrapped[:foo].should == :bar
180
173
  unwrapped["missile"].should be_a(Missile)
181
174
  end
182
175
 
183
176
  it "traverses an array and unwraps whatever it needs to" do
184
- array = ["__sym_foo", 5, @wrapped, nil, "hi"]
177
+ array = [:foo, 5, @wrapped, nil, "hi"]
185
178
  unwrapped = Wrapper.unwrap(array)
186
179
  unwrapped[0].should == :foo
187
180
  unwrapped[1].should == 5
@@ -1,201 +1,11 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Candy" do
4
- # An example class to contain our methods
5
- class Zagnut
6
- include Candy
7
- end
8
-
9
- before(:all) do
10
- @verifier = Zagnut.collection
11
- end
12
4
 
13
5
  before(:each) do
14
6
  @this = Zagnut.new
15
- end
16
-
17
-
18
- it "inserts a document immediately" do
19
- @this.id.should be_a(Mongo::ObjectID)
20
- end
21
-
22
- it "saves any attribute it doesn't already handle to the database" do
23
- @this.bite = "Tasty!"
24
- @verifier.find_one["bite"].should == "Tasty!"
25
- end
26
-
27
- it "retrieves any attribute it doesn't already know about from the database" do
28
- @verifier.update({:_id => @this.id}, {:chew => "Yummy!", :bite => "Ouch."})
29
- @this.chew.should == "Yummy!"
30
- end
31
-
32
- it "can roundtrip effectively" do
33
- @this.swallow = "Gulp."
34
- @this.swallow.should == "Gulp."
35
- end
36
-
37
- it "handles missing attributes gracefully" do
38
- @this.licks.should == nil
39
- end
40
-
41
- it "allows multiple attributes to be set" do
42
- @this.licks = 7
43
- @this.center = 0.5
44
- @this.licks.should == 7
45
- end
7
+ end
46
8
 
47
- it "can set properties explicity" do
48
- @this.set(:licks, 17)
49
- @this.licks.should == 17
50
- end
51
-
52
- it "can set properties from a hash" do
53
- @this.set(:licks => 19, :center => -2.5)
54
- @this.licks.should == 19
55
- @this.center.should == -2.5
56
- end
57
-
58
- it "wraps objects" do
59
- o = Object.new
60
- @this.object = o
61
- @verifier.find_one["object"]["__object_"]["class"].should == "Object"
62
- end
63
-
64
- it "unwraps objects" do
65
- @verifier.update({:_id => @this.id}, {:center => {"__object_" => {:class => "Object", :ivars => {"@foo" => "bar"}}}})
66
- @this.center.should be_an(Object)
67
- @this.center.instance_variable_get(:@foo).should == "bar"
68
- end
69
-
70
- describe "retrieval" do
71
- it "can find a record by its ID" do
72
- @this.licks = 10
73
- that = Zagnut.find(@this.id)
74
- that.licks.should == 10
75
- end
76
-
77
- it "roundtrips across identical objects" do
78
- that = Zagnut.find(@this.id)
79
- @this.calories = 7500
80
- that.calories.should == 7500
81
- end
82
-
83
- it "returns nil on an object that can't be found" do
84
- id = Mongo::ObjectID.new
85
- Zagnut.find(id).should be_nil
86
- end
87
-
88
- it "can get a single object by attributes" do
89
- @this.pieces = 7.5
90
- @this.color = "red"
91
- that = Zagnut.first("pieces" => 7.5)
92
- that.color.should == "red"
93
- end
94
-
95
- it "returns nil if a first object can't be found" do
96
- @this.pieces = 11
97
- Zagnut.first("pieces" => 5).should be_nil
98
- end
99
- end
100
-
101
- describe "collections" do
102
- before(:each) do
103
- @this.color = "red"
104
- @this.weight = 11.8
105
- @that = Zagnut.new
106
- @that.color = "red"
107
- @that.pieces = 6
108
- @that.weight = -5
109
- @the_other = Zagnut.new
110
- @the_other.color = "blue"
111
- @the_other.pieces = 7
112
- @the_other.weight = 0
113
- end
114
-
115
- it "can get all objects in a collection" do
116
- those = Zagnut.all
117
- those.count.should == 3
118
- end
119
-
120
- it "can get all objects matching a search condition" do
121
- those = Zagnut.all(:color => "red")
122
- those.count.should == 2
123
- end
124
-
125
- it "still returns if nothing matches" do
126
- Zagnut.all(:color => "green").to_a.should == []
127
- end
128
-
129
- it "can take options" do
130
- those = Zagnut.all(:color => "red", :sort => ["weight", :asc])
131
- those.collect{|z| z.weight}.should == [-5, 11.8]
132
- end
133
-
134
- end
135
-
136
- describe "arrays" do
137
- it "can push items" do
138
- @this.push(:colors, 'red')
139
- @this.colors.should == ['red']
140
- end
141
-
142
- it "can push an array of items" do
143
- @this.push(:potpourri, 'red', 75, nil)
144
- @this.potpourri.should == ['red', 75, nil]
145
- end
146
- end
147
-
148
- describe "numbers" do
149
- it "can be incremented by 1 when not set" do
150
- @this.inc(:bites)
151
- @this.bites.should == 1
152
- end
153
-
154
- it "can be incremented by 1 when set" do
155
- @this.bites = 11
156
- @this.inc(:bites)
157
- @this.bites.should == 12
158
- end
159
-
160
- it "can be incremented by any number" do
161
- @this.bites = -6
162
- @this.inc(:bites, 15)
163
- @this.bites.should == 9
164
- end
165
- end
166
-
167
- describe "timestamp" do
168
- it "can be set on creation" do
169
- Zagnut.class_eval("timestamp :create")
170
- z = Zagnut.new
171
- z.created_at.should be_a(Time)
172
- z.updated_at.should be_nil
173
- end
174
-
175
- it "can be set on modification" do
176
- Zagnut.class_eval("timestamp :update")
177
- z = Zagnut.new
178
- z.created_at.should be_nil
179
- z.updated_at.should be_nil
180
- z.bites = 11
181
- z.created_at.should be_nil
182
- z.updated_at.should be_a(Time)
183
- end
184
-
185
- it "sets both by default" do
186
- Zagnut.class_eval("timestamp")
187
- z = Zagnut.new
188
- z.bites = 11
189
- z.created_at.should be_a(Time)
190
- z.updated_at.should be_a(Time)
191
- end
192
-
193
-
194
- after(:each) do
195
- Zagnut.class_eval("timestamp nil")
196
- end
197
-
198
- end
199
9
 
200
10
  after(:each) do
201
11
  Zagnut.collection.remove
@@ -5,11 +5,15 @@ require 'spec'
5
5
  require 'spec/autorun'
6
6
  require 'mocha'
7
7
 
8
+
9
+ # Support methods
10
+ Candy.db = 'candy_test'
11
+ Dir[File.join(File.dirname(__FILE__), 'support', '*.rb')].each {|f| require f}
12
+
8
13
  Spec::Runner.configure do |config|
9
14
  config.mock_with :mocha
10
15
 
11
16
  config.before(:all) do
12
- $MONGO_DB = 'candy_test'
13
17
  end
14
18
 
15
19
  config.after(:all) do
@@ -0,0 +1,10 @@
1
+ # An example class to be embedded in another
2
+ class KitKat
3
+ include Candy::Piece
4
+ end
5
+
6
+ class KitKats
7
+ include Candy::Collection
8
+ collects :kit_kat
9
+ end
10
+
@@ -0,0 +1,10 @@
1
+ # An example class to contain our methods
2
+ class Zagnut
3
+ include Candy::Piece
4
+ end
5
+
6
+ class Zagnuts
7
+ include Candy::Collection
8
+
9
+ collects :zagnut
10
+ end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: candy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 1
9
+ version: 0.2.1
5
10
  platform: ruby
6
11
  authors:
7
12
  - Stephen Eley
@@ -9,56 +14,56 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-02-16 00:00:00 -05:00
17
+ date: 2010-04-05 00:00:00 -04:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: mongo
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
23
- version: "0.18"
24
- version:
27
+ segments:
28
+ - 0
29
+ - 19
30
+ - 1
31
+ version: 0.19.1
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  - !ruby/object:Gem::Dependency
26
35
  name: rspec
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
30
38
  requirements:
31
39
  - - ">="
32
40
  - !ruby/object:Gem::Version
41
+ segments:
42
+ - 1
43
+ - 2
44
+ - 9
33
45
  version: 1.2.9
34
- version:
35
- - !ruby/object:Gem::Dependency
36
- name: yard
37
46
  type: :development
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- version: "0"
44
- version:
47
+ version_requirements: *id002
45
48
  - !ruby/object:Gem::Dependency
46
49
  name: mocha
47
- type: :development
48
- version_requirement:
49
- version_requirements: !ruby/object:Gem::Requirement
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
50
52
  requirements:
51
53
  - - ">="
52
54
  - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ - 9
58
+ - 8
53
59
  version: 0.9.8
54
- version:
60
+ type: :development
61
+ version_requirements: *id003
55
62
  description: |
56
- Candy is a lightweight ORM for the MongoDB database. If MongoMapper is Rails, Candy is Sinatra.
57
- It provides a module you mix into any class, enabling the class to connect to Mongo on its own
58
- and push its objects into a collection. Candied objects act like OpenStructs, allowing attributes
59
- to be defined and updated in Mongo immediately without having to be declared in the class.
60
- Mongo's atomic operators are used whenever possible, and a smart serializer (Candy::Wrapper)
61
- converts almost any object for assignment to any attribute.
63
+ Candy provides simple, transparent object persistence for the MongoDB database. Classes that
64
+ include Candy modules save all properties to Mongo automatically, can be recursively embedded,
65
+ and can retrieve records with chainable open-ended class methods, eliminating the need for
66
+ method calls like 'save' and 'find.'
62
67
 
63
68
  email: sfeley@gmail.com
64
69
  executables: []
@@ -66,27 +71,40 @@ executables: []
66
71
  extensions: []
67
72
 
68
73
  extra_rdoc_files:
69
- - LICENSE
74
+ - LICENSE.markdown
70
75
  - README.markdown
71
76
  files:
72
77
  - .document
73
78
  - .gitignore
74
- - LICENSE
79
+ - HISTORY.markdown
80
+ - LICENSE.markdown
75
81
  - README.markdown
76
82
  - Rakefile
77
83
  - VERSION
78
84
  - candy.gemspec
79
85
  - lib/candy.rb
86
+ - lib/candy/array.rb
87
+ - lib/candy/collection.rb
80
88
  - lib/candy/crunch.rb
89
+ - lib/candy/embeddable.rb
81
90
  - lib/candy/exceptions.rb
91
+ - lib/candy/factory.rb
92
+ - lib/candy/hash.rb
93
+ - lib/candy/piece.rb
82
94
  - lib/candy/qualified_const_get.rb
83
95
  - lib/candy/wrapper.rb
96
+ - spec/candy/array_spec.rb
97
+ - spec/candy/collection_spec.rb
84
98
  - spec/candy/crunch_spec.rb
99
+ - spec/candy/hash_spec.rb
100
+ - spec/candy/piece_spec.rb
85
101
  - spec/candy/wrapper_spec.rb
86
102
  - spec/candy_spec.rb
87
103
  - spec/spec.opts
88
104
  - spec/spec.watchr
89
105
  - spec/spec_helper.rb
106
+ - spec/support/kitkat_fixture.rb
107
+ - spec/support/zagnuts_fixture.rb
90
108
  has_rdoc: true
91
109
  homepage: http://github.com/SFEley/candy
92
110
  licenses: []
@@ -100,23 +118,31 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
118
  requirements:
101
119
  - - ">="
102
120
  - !ruby/object:Gem::Version
121
+ segments:
122
+ - 0
103
123
  version: "0"
104
- version:
105
124
  required_rubygems_version: !ruby/object:Gem::Requirement
106
125
  requirements:
107
126
  - - ">="
108
127
  - !ruby/object:Gem::Version
128
+ segments:
129
+ - 0
109
130
  version: "0"
110
- version:
111
131
  requirements: []
112
132
 
113
133
  rubyforge_project:
114
- rubygems_version: 1.3.5
134
+ rubygems_version: 1.3.6
115
135
  signing_key:
116
136
  specification_version: 3
117
- summary: The simplest MongoDB ORM
137
+ summary: Transparent persistence for MongoDB
118
138
  test_files:
139
+ - spec/candy/array_spec.rb
140
+ - spec/candy/collection_spec.rb
119
141
  - spec/candy/crunch_spec.rb
142
+ - spec/candy/hash_spec.rb
143
+ - spec/candy/piece_spec.rb
120
144
  - spec/candy/wrapper_spec.rb
121
145
  - spec/candy_spec.rb
122
146
  - spec/spec_helper.rb
147
+ - spec/support/kitkat_fixture.rb
148
+ - spec/support/zagnuts_fixture.rb