Skip to content

Commit

Permalink
Add convenience methods on options for getting fields, skip, limit, s…
Browse files Browse the repository at this point in the history
…ort.
  • Loading branch information
durran committed May 7, 2012
1 parent c090536 commit 53d7ce3
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/origin/options.rb
Expand Up @@ -5,6 +5,54 @@ module Origin
# such as skip, limit, and sorting criteria.
class Options < Smash

# Convenience method for getting the field options.
#
# @example Get the fields options.
# options.fields
#
# @return [ Hash ] The fields options.
#
# @since 1.0.0
def fields
self[:fields]
end

# Convenience method for getting the limit option.
#
# @example Get the limit option.
# options.limit
#
# @return [ Integer ] The limit option.
#
# @since 1.0.0
def limit
self[:limit]
end

# Convenience method for getting the skip option.
#
# @example Get the skip option.
# options.skip
#
# @return [ Integer ] The skip option.
#
# @since 1.0.0
def skip
self[:skip]
end

# Convenience method for getting the sort options.
#
# @example Get the sort options.
# options.sort
#
# @return [ Hash ] The sort options.
#
# @since 1.0.0
def sort
self[:sort]
end

# Store the value in the options for the provided key. The options will
# handle all necessary serialization and localization in this step.
#
Expand Down
100 changes: 100 additions & 0 deletions spec/origin/options_spec.rb
Expand Up @@ -29,6 +29,106 @@
end
end

describe "#fields" do

let(:options) do
described_class.new
end

context "when field options exist" do

before do
options[:fields] = { name: 1 }
end

it "returns the field options" do
options.fields.should eq({ "name" => 1 })
end
end

context "when field options do not exist" do

it "returns nil" do
options.fields.should be_nil
end
end
end

describe "#limit" do

let(:options) do
described_class.new
end

context "when limit options exist" do

before do
options[:limit] = 20
end

it "returns the limit options" do
options.limit.should eq(20)
end
end

context "when limit options do not exist" do

it "returns nil" do
options.limit.should be_nil
end
end
end

describe "#skip" do

let(:options) do
described_class.new
end

context "when skip options exist" do

before do
options[:skip] = 100
end

it "returns the skip options" do
options.skip.should eq(100)
end
end

context "when skip options do not exist" do

it "returns nil" do
options.skip.should be_nil
end
end
end

describe "#sort" do

let(:options) do
described_class.new
end

context "when sort options exist" do

before do
options[:sort] = { name: 1 }
end

it "returns the sort options" do
options.sort.should eq({ "name" => 1 })
end
end

context "when sort options do not exist" do

it "returns nil" do
options.sort.should be_nil
end
end
end

[ :store, :[]= ].each do |method|

describe "##{method}" do
Expand Down

0 comments on commit 53d7ce3

Please sign in to comment.