Permalink
Browse files

added property view to configuration

  • Loading branch information...
1 parent f935871 commit 8e57d0c461c8a7e788f7f5c0a472f83c829fef7d @phifty committed Aug 14, 2011
@@ -143,6 +143,30 @@
end
+ describe "fetching a property collection" do
+
+ before :each do
+ GOM::Storage.store @object, :test_storage
+ end
+
+ after :each do
+ GOM::Storage.remove @related_object
+ GOM::Storage.remove @object
+ end
+
+ it "should provide a collection of the property view" do
+ collection = GOM::Storage.collection :test_storage, :test_property_view
+ collection.should be_instance_of(GOM::Object::Collection)
+ end
+
+ it "should provide a collection that filters the objects by the given conditions" do
+ collection = GOM::Storage.collection :test_storage, :test_property_view
+ collection.map(&:number) == [ 16 ]
+ collection.map(&:class).uniq.should == [ GOM::Spec::Object ]
+ end
+
+ end
+
describe "fetching a class collection" do
before :each do
@@ -97,6 +97,10 @@ def self.map_reduce_view(hash)
View::MapReduce.new *hash.values_at(:map_function, :reduce_function)
end
+ def self.property_view(hash)
+ View::Property.new *hash.values_at(:filter, :properties)
+ end
+
def self.setup_all
@configurations.values.each do |configuration|
configuration.setup
@@ -3,5 +3,6 @@ module GOM::Storage::Configuration::View
autoload :Class, File.join(File.dirname(__FILE__), "view", "class")
autoload :MapReduce, File.join(File.dirname(__FILE__), "view", "map_reduce")
+ autoload :Property, File.join(File.dirname(__FILE__), "view", "property")
end
@@ -0,0 +1,12 @@
+
+# Contains all parameters to describe a property view.
+class GOM::Storage::Configuration::View::Property
+
+ attr_accessor :filter
+ attr_accessor :properties
+
+ def initialize(filter, properties)
+ @filter, @properties = filter, properties
+ end
+
+end
View
@@ -1,6 +1,36 @@
class FakeAdapter < GOM::Storage::Adapter
+ class PropertyCollectionFetcher
+
+ def initialize(store, storage_name, filter, properties)
+ @store, @storage_name, @filter, @properties = store, storage_name, filter, properties
+ end
+
+ def drafts
+ drafts = [ ]
+ @store.each do |object_id, draft|
+ match = true
+ (@filter || { }).each do |property_name, condition|
+ condition_kind, condition_value = *condition
+ match &&= case condition_kind
+ when :equals
+ property_name == :model_class ?
+ draft.class_name == condition_value :
+ draft.properties[property_name] == condition_value
+ when :greater_than
+ draft.properties[property_name] > condition_value
+ else
+ raise ArgumentError, "condition kind #{condition_kind} is not implemented"
+ end
+ end
+ drafts << draft if match
+ end
+ drafts
+ end
+
+ end
+
class ClassCollectionFetcher
def initialize(store, storage_name, class_name)
@@ -77,7 +107,10 @@ def count
def collection(view_name)
raise GOM::Storage::Adapter::NoSetupError unless @store
+ view = configuration.views[view_name]
case view_name.to_sym
+ when :test_property_view
+ GOM::Object::Collection.new PropertyCollectionFetcher.new(@store, configuration.name, view.filter, view.properties), configuration.name
when :test_object_class_view
GOM::Object::Collection.new ClassCollectionFetcher.new(@store, configuration.name, "GOM::Spec::Object"), configuration.name
when :test_map_view
@@ -3,13 +3,13 @@
describe GOM::Storage::Configuration::View::Class do
before :each do
- @class = described_class.new "Object"
+ @view = described_class.new "Object"
end
describe "initialize" do
it "should set the view's class name" do
- @class.class_name.should == "Object"
+ @view.class_name.should == "Object"
end
end
@@ -3,17 +3,17 @@
describe GOM::Storage::Configuration::View::MapReduce do
before :each do
- @map_reduce = described_class.new "map function", "reduce function"
+ @view = described_class.new "map function", "reduce function"
end
describe "initialize" do
it "should set the view's map function" do
- @map_reduce.map.should == "map function"
+ @view.map.should == "map function"
end
it "should set the view's reduce function" do
- @map_reduce.reduce.should == "reduce function"
+ @view.reduce.should == "reduce function"
end
end
@@ -0,0 +1,23 @@
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "..", "spec_helper"))
+
+describe GOM::Storage::Configuration::View::Property do
+
+ before :each do
+ @view = described_class.new "filter", "properties"
+ end
+
+ describe "initialize" do
+
+ it "should set the filter" do
+ filter = @view.filter
+ filter.should == "filter"
+ end
+
+ it "should set the properties" do
+ properties = @view.properties
+ properties.should == "properties"
+ end
+
+ end
+
+end
@@ -12,6 +12,14 @@
name :test_storage
adapter :test
view {
+ name :test_property_view
+ kind :property
+ filter {
+ model_class :equals, "Object"
+ }
+ properties :model_class, :number
+ }
+ view {
name :test_object_class_view
kind :class
model_class Object
@@ -99,6 +107,13 @@
describe "#self.views" do
+ it "should return a hash including property views" do
+ view = @configuration.views[:test_property_view]
+ view.should be_instance_of(described_class::View::Property)
+ view.filter.should == { :model_class => [ :equals, "Object" ] }
+ view.properties.should == [ :model_class, :number ]
+ end
+
it "should return a hash including class views" do
view = @configuration.views[:test_object_class_view]
view.should be_instance_of(described_class::View::Class)
@@ -5,6 +5,15 @@
name :test_storage
adapter :fake_adapter
view {
+ name :test_property_view
+ kind :property
+ filter {
+ model_class :equals, "GOM::Spec::Object"
+ number :greater_than, 13
+ }
+ properties :number
+ }
+ view {
name :test_object_class_view
kind :class
model_class Object

0 comments on commit 8e57d0c

Please sign in to comment.