Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Added methods for arrays and hashes

  • Loading branch information...
1 parent 59da7fb commit c81505fed5d9137f47ea5b9901221f9beeb88cbd @SFEley committed
10 HISTORY.markdown
@@ -3,6 +3,16 @@ Candy History
This document aims to provide only an overview. Further, we've only really been tracking things since **v0.2**. For obsessive detail, just check out the `git log`.
+v0.2.10 - 2010-06-10 (the "This is not my beautiful hash" release)
+Made arrays enumerable finally (thanks to dominikh with issue #13) and added some vital hash methods to Piece that I needed.
+**NOTE:** I've been slow on updates the past few weeks. It's because I got a crazy new idea on how to interface with the Mongo parts (the "Candy::Crunch" part of this gem) and I've been spending my free dev time playing with that. It might take a bit longer, but if I can get it to work, the non-driver parts of Candy will be both simpler and more incredible. Ping me if you want to know more about what I'm babbling about.
+* Fixed Github issue #13
+* Added Piece#keys and Piece#values
v0.2.9 - 2010-05-14 (the "+1" release)
Moved methods around again, placing more of the database update methods into Candy::Crunch. Also began support for two flavors of
6 lib/candy/array.rb
@@ -9,6 +9,7 @@ module Candy
class CandyArray
include Crunch
include Embeddable
+ include Enumerable
# Creates the object with parent and attribute values set properly on the object and any children.
def self.embed(parent, attribute, *args)
@@ -36,6 +37,11 @@ def [](index)
+ # Iterates over each value in turn, so that we can have proper Enumerable support
+ def each(&block)
+ candy.each(&block)
+ end
# Appends a value to our array.
def <<(val)
property = candy_coat(@__candy_parent_key, val)
5 lib/candy/hash.rb
@@ -2,7 +2,7 @@
module Candy
- # A subclass of Hash that behaves like a Candy::Piece. This class has two major uses:
+ # An untyped hash that behaves like a Candy::Piece. This class has two major uses:
# * It's a convenient starting point if you just want to store a bunch of data in Mongo
# and don't need to implement any business logic in your own classes; and
@@ -13,13 +13,12 @@ module Candy
# If you don't tell them otherwise, top-level CandyHash objects store themselves in
# the 'candy' collection. You can change that at any time by setting a different
# collection at the class or object level.
- class CandyHash < Hash
+ class CandyHash
include Crunch
include Piece
self.collection = 'candy'
# Overrides the default behavior in Candy::Piece so that we DO NOT add our
# class name to the saved values.
def to_candy
9 lib/candy/piece.rb
@@ -149,6 +149,15 @@ def refresh
+ # Returns the keys we've stored.
+ def keys
+ candy.keys
+ end
+ # Returns the values we've stored.
+ def values
+ candy.values
+ end
# Convenience method for debugging. Shows the class, the Mongo ID, and the saved state hash.
def to_s
8 spec/candy/array_spec.rb
@@ -51,6 +51,14 @@
@this.bits[3].should == 'schadenfreude'
+ it "is enumerable" do
+{|b| b.upcase}.should == ['PEANUT', 'ALMONDS', 'TITANIUM']
+ end
+ it "is sortable" do
+ @this.bits.sort.should == ['almonds', 'peanut', 'titanium']
+ end
after(:each) do
6 spec/candy/hash_spec.rb
@@ -17,7 +17,11 @@
it "reads the hash" do
that = Zagnut(
that.filling.taste.should == 'caramel'
- that.filling.should be_a(Hash)
+ end
+ it "reads the hash with brackets" do
+ that = Zagnut(
+ that[:filling][:taste].should == 'caramel'
it "cascades changes" do
10 spec/candy/piece_spec.rb
@@ -229,6 +229,16 @@ class Jawbreaker
@verifier.find_one(ounces: 12)["crunchy"].should == :very
+ it "knows its keys" do
+ @this.keys.should == [:ounces, :crunchy]
+ end
+ it "knows its values" do
+ @this.values.should == [17, :very]
+ end
describe "embedding" do

0 comments on commit c81505f

Please sign in to comment.
Something went wrong with that request. Please try again.