mongo_odm 0.1.8 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. data/Gemfile +7 -2
  2. data/Rakefile +15 -12
  3. data/lib/mongo_odm.rb +6 -8
  4. data/lib/mongo_odm/criteria.rb +20 -0
  5. data/lib/mongo_odm/cursor.rb +1 -1
  6. data/lib/mongo_odm/document.rb +9 -2
  7. data/lib/mongo_odm/document/associations.rb +0 -7
  8. data/lib/mongo_odm/document/attribute_methods.rb +2 -0
  9. data/lib/mongo_odm/document/attribute_methods/inspect.rb +19 -0
  10. data/lib/mongo_odm/document/attribute_methods/localization.rb +36 -32
  11. data/lib/mongo_odm/document/fields.rb +1 -1
  12. data/lib/mongo_odm/document/inspect.rb +1 -1
  13. data/lib/mongo_odm/document/persistence.rb +11 -3
  14. data/lib/mongo_odm/version.rb +1 -1
  15. data/spec/models/02-circle.rb +0 -1
  16. data/spec/models/03-big_red_circle.rb +1 -2
  17. data/spec/mongo_odm/collection_spec.rb +6 -0
  18. data/spec/mongo_odm/core_ext/conversions_spec.rb +14 -4
  19. data/spec/mongo_odm/cursor_spec.rb +6 -0
  20. data/spec/mongo_odm/document/attribute_methods/dirty_spec.rb +6 -0
  21. data/spec/mongo_odm/document/attribute_methods/localization_spec.rb +6 -0
  22. data/spec/mongo_odm/document/attribute_methods/query_spec.rb +6 -0
  23. data/spec/mongo_odm/document/attribute_methods/read_spec.rb +6 -0
  24. data/spec/mongo_odm/document/attribute_methods/write_spec.rb +6 -0
  25. data/spec/mongo_odm/document/attribute_methods_spec.rb +6 -0
  26. data/spec/mongo_odm/document/callbacks_spec.rb +6 -0
  27. data/spec/mongo_odm/document/fields_spec.rb +15 -33
  28. data/spec/mongo_odm/document/inspect_spec.rb +7 -0
  29. data/spec/mongo_odm/document/persistence_spec.rb +6 -0
  30. data/spec/mongo_odm/document/validations_spec.rb +6 -0
  31. data/spec/mongo_odm/document_spec.rb +15 -7
  32. data/spec/mongo_odm/mongo_odm_spec.rb +25 -30
  33. data/spec/spec_helper.rb +1 -2
  34. metadata +118 -19
  35. data/spec/spec.opts +0 -4
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
+ # Use `bundle install` in order to install these gems
1
2
  source 'http://rubygems.org'
2
3
 
3
4
  gem "activesupport", "3.0.0.beta3"
@@ -6,6 +7,10 @@ gem "mongo", "~>1.0.2"
6
7
  gem "bson_ext", "~>1.0.1"
7
8
  gem "tzinfo", "~>0.3.22"
8
9
 
9
- group :test do
10
- gem 'rspec', '~>1.3.0'
10
+ group :development do
11
+ gem 'jeweler', '1.4.0'
12
+ gem 'yard', '0.5.6'
13
+ gem 'rcov', '0.9.8'
14
+ gem 'rspec', '2.0.0.beta.12'
15
+ gem 'watchr', '~>0.6'
11
16
  end
data/Rakefile CHANGED
@@ -4,6 +4,7 @@ require 'rake'
4
4
  ## gem building
5
5
  require File.dirname(__FILE__) + "/lib/mongo_odm/version.rb"
6
6
  if Gem.available? 'jeweler'
7
+ require 'bundler'
7
8
  require 'jeweler'
8
9
  Jeweler::Tasks.new do |gemspec|
9
10
  gemspec.name = "mongo_odm"
@@ -14,11 +15,14 @@ if Gem.available? 'jeweler'
14
15
  gemspec.authors = ["Carlos Paramio"]
15
16
  gemspec.files = FileList["[A-Z]*", "{lib,spec}/**/*"]
16
17
  gemspec.version = MongoODM::VERSION
17
- gemspec.add_dependency 'activesupport', ['3.0.0.beta3']
18
- gemspec.add_dependency 'activemodel', ['3.0.0.beta3']
19
- gemspec.add_dependency 'mongo', ['1.0.1']
20
- gemspec.add_dependency 'bson', ['1.0.1']
21
- gemspec.add_dependency 'bson_ext', ['1.0.1']
18
+ bundle = Bundler::Definition.from_gemfile('Gemfile')
19
+ bundle.dependencies.each do |dependency|
20
+ if dependency.groups.include?(:default)
21
+ gemspec.add_dependency(dependency.name, dependency.requirement.to_s)
22
+ elsif dependency.groups.include?(:development)
23
+ gemspec.add_development_dependency(dependency.name, dependency.requirement.to_s)
24
+ end
25
+ end
22
26
  end
23
27
  Jeweler::GemcutterTasks.new
24
28
  else
@@ -27,19 +31,18 @@ end
27
31
 
28
32
  ## rspec tasks
29
33
  if Gem.available? 'rspec'
30
- require 'spec/rake/spectask'
34
+ require "rspec"
35
+ require "rspec/core/rake_task"
31
36
  desc "Run all specs"
32
- Spec::Rake::SpecTask.new('spec') do |t|
33
- t.spec_files = FileList['spec/**/*_spec.rb']
37
+ Rspec::Core::RakeTask.new(:spec) do |spec|
38
+ spec.pattern = "spec/**/*_spec.rb"
34
39
  end
35
40
 
36
41
  ## rcov task
37
42
  if Gem.available? 'rcov'
38
43
  require 'rcov/rcovtask'
39
- Spec::Rake::SpecTask.new(:rcov) do |spec|
40
- spec.libs << "lib" << "spec"
44
+ Rspec::Core::RakeTask.new(:rcov) do |spec|
41
45
  spec.pattern = "spec/**/*_spec.rb"
42
- spec.spec_opts = ["--options", "spec/spec.opts"]
43
46
  spec.rcov = true
44
47
  end
45
48
  end
@@ -51,7 +54,7 @@ end
51
54
  if Gem.available? 'yard'
52
55
  require 'yard'
53
56
  YARD::Rake::YardocTask.new do |yard|
54
- docfiles = FileList['lib/**/*.rb', 'README.rdoc']
57
+ docfiles = FileList['lib/**/*.rb']
55
58
  yard.files = docfiles
56
59
  yard.options = ["--no-private"]
57
60
  end
data/lib/mongo_odm.rb CHANGED
@@ -11,6 +11,7 @@ module MongoODM
11
11
  include ActiveSupport::Configurable
12
12
 
13
13
  autoload :VERSION
14
+ autoload :Criteria
14
15
  autoload :Cursor
15
16
  autoload :Collection
16
17
  autoload :Document
@@ -34,15 +35,12 @@ module MongoODM
34
35
  end
35
36
 
36
37
  def self.instanciate(value)
37
- return nil if value.nil?
38
- return instanciate_doc(value) if value.is_a?(Hash)
39
- return value if value.class.included_modules.include?(MongoODM::Document)
38
+ return value if value.is_a?(MongoODM::Document)
39
+ if value.is_a?(Hash)
40
+ klass = value["_class"] || value[:_class]
41
+ return klass.constantize.new(value) if klass
42
+ end
40
43
  value.class.type_cast(value.to_mongo)
41
44
  end
42
-
43
- def self.instanciate_doc(doc)
44
- return doc if doc["_class"].nil?
45
- doc["_class"].constantize.new(doc)
46
- end
47
45
 
48
46
  end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ module MongoODM
3
+
4
+ class Criteria
5
+ def initialize(collection, selector = {}, opts = {})
6
+ @collection, @selector, @opts = collection, selector, opts
7
+ end
8
+
9
+ def find(selector = {}, opts = {})
10
+ @selector.merge!(selector)
11
+ @opts.merge!(opts)
12
+ self
13
+ end
14
+
15
+ def method_missing(method_name, *args)
16
+ @collection.find(@selector, @opts).send(method_name, *args)
17
+ end
18
+ end
19
+
20
+ end
@@ -4,7 +4,7 @@ module MongoODM
4
4
  class Cursor < Mongo::Cursor
5
5
  def next_document
6
6
  doc = super
7
- MongoODM.instanciate_doc(doc)
7
+ MongoODM.instanciate(doc)
8
8
  end
9
9
  end
10
10
 
@@ -7,7 +7,7 @@ module MongoODM
7
7
  extend ActiveSupport::Concern
8
8
  extend ActiveSupport::Autoload
9
9
 
10
- autoload :Associations
10
+ #autoload :Associations
11
11
  autoload :AttributeMethods
12
12
  autoload :Callbacks
13
13
  autoload :Fields
@@ -25,10 +25,17 @@ module MongoODM
25
25
  include MongoODM::Document::AttributeMethods
26
26
  include MongoODM::Document::Fields
27
27
  include MongoODM::Document::Inspect
28
- include MongoODM::Document::Associations
28
+ #include MongoODM::Document::Associations
29
29
  include MongoODM::Document::Callbacks
30
30
  include MongoODM::Document::Validations
31
31
  end
32
+
33
+ module InstanceMethods
34
+ def ==(other)
35
+ return false unless other.is_a?(MongoODM::Document)
36
+ attributes == other.attributes
37
+ end
38
+ end
32
39
 
33
40
  end
34
41
  end
@@ -25,13 +25,6 @@ module MongoODM
25
25
  end
26
26
 
27
27
  module ClassMethods
28
- def associate(type, options)
29
- name = options.name.to_s
30
- associations[name] = MetaData.new(type, options)
31
- define_method(name) { memoized(name) { type.instantiate(self, options) } }
32
- define_method("#{name}=") { |object| reset(name) { type.update(object, self, options) } }
33
- end
34
- protected :associate
35
28
  end
36
29
 
37
30
  end
@@ -12,6 +12,7 @@ module MongoODM
12
12
  autoload :Write
13
13
  autoload :Query
14
14
  autoload :Dirty
15
+ autoload :Inspect
15
16
  autoload :Localization
16
17
 
17
18
  included do
@@ -20,6 +21,7 @@ module MongoODM
20
21
  include MongoODM::Document::AttributeMethods::Write
21
22
  include MongoODM::Document::AttributeMethods::Query
22
23
  include MongoODM::Document::AttributeMethods::Dirty
24
+ include MongoODM::Document::AttributeMethods::Inspect
23
25
  include MongoODM::Document::AttributeMethods::Localization
24
26
  end
25
27
 
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ module MongoODM
4
+ module Document
5
+ module AttributeMethods
6
+ module Inspect
7
+
8
+ extend ActiveSupport::Concern
9
+
10
+ module InstanceMethods
11
+ def inspect_attribute(attr_name)
12
+ "#{attr_name}: #{read_attribute(attr_name).inspect}"
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
19
+ end
@@ -10,54 +10,58 @@ module MongoODM
10
10
 
11
11
  included do
12
12
  attribute_method_suffix "_localized?"
13
- attribute_method_suffix "_without_localization"
14
-
15
- alias_method_chain :read_attribute, :localization
16
- alias_method_chain :write_attribute, :localization
17
- alias_method_chain :inspect, :localization
13
+ attribute_method_suffix "_localized"
14
+ attribute_method_suffix "_localized="
15
+
16
+ alias_method_chain :inspect_attribute, :localization
18
17
  end
19
18
 
20
19
  module ClassMethods
21
- def define_method_attribute_localized?(attr_name)
22
- generated_attribute_methods.module_eval("def #{attr_name}_localized?; localized_attribute?('#{attr_name}'); end", __FILE__, __LINE__)
23
- end
24
- protected :define_method_attribute_localized?
25
-
26
- def define_method_attribute_without_localization(attr_name)
27
- generated_attribute_methods.module_eval("def #{attr_name}_without_localization; read_attribute_without_localization('#{attr_name}'); end", __FILE__, __LINE__)
28
- end
29
- protected :define_method_attribute_localized?
30
- end
31
-
32
- module InstanceMethods
33
20
  def localized_attribute?(attr_name)
34
- field = self.class.fields[attr_name]
21
+ field = fields[attr_name]
35
22
  localized = field ? !!field.options[:localized] : false
36
23
  raise MongoODM::Errors::InvalidLocalizedField.new(field.name, field.type) if localized && field && field.type != Hash
37
24
  localized
38
25
  end
39
26
 
40
- def read_attribute_with_localization(attr_name, locale = nil)
27
+ def define_method_attribute_localized?(attr_name)
28
+ generated_attribute_methods.module_eval("def #{attr_name}_localized?; self.class.localized_attribute?('#{attr_name}'); end", __FILE__, __LINE__)
29
+ end
30
+ protected :define_method_attribute_localized?
31
+
32
+ def define_method_attribute_localized(attr_name)
41
33
  if localized_attribute?(attr_name)
42
- locale = I18n.locale || I18n.default_locale if locale.nil?
43
- (read_attribute_without_localization(attr_name) || {}.with_indifferent_access)[locale]
44
- else
45
- read_attribute_without_localization(attr_name)
34
+ generated_attribute_methods.module_eval("def #{attr_name}_localized; read_localized_attribute('#{attr_name}'); end", __FILE__, __LINE__)
46
35
  end
47
36
  end
48
-
49
- def write_attribute_with_localization(attr_name, value, locale = nil)
37
+ protected :define_method_attribute_localized
38
+
39
+ def define_method_attribute_localized=(attr_name)
50
40
  if localized_attribute?(attr_name)
51
- locale = I18n.locale || I18n.default_locale if locale.nil?
52
- current_value = read_attribute_without_localization(attr_name) || {}.with_indifferent_access
53
- write_attribute_without_localization(attr_name, current_value.merge(locale => value))
54
- else
55
- write_attribute_without_localization(attr_name, value)
41
+ generated_attribute_methods.module_eval("def #{attr_name}_localized=(new_value); write_localized_attribute('#{attr_name}', new_value); end", __FILE__, __LINE__)
56
42
  end
57
43
  end
44
+ protected :define_method_attribute_localized=
45
+ end
46
+
47
+ module InstanceMethods
48
+ def read_localized_attribute(attr_name, locale = nil)
49
+ locale = I18n.locale || I18n.default_locale if locale.nil?
50
+ (read_attribute(attr_name) || {}.with_indifferent_access)[locale]
51
+ end
52
+
53
+ def write_localized_attribute(attr_name, value, locale = nil)
54
+ locale = I18n.locale || I18n.default_locale if locale.nil?
55
+ current_value = read_attribute(attr_name) || {}.with_indifferent_access
56
+ write_attribute(attr_name, current_value.merge(locale => value))
57
+ end
58
58
 
59
- def inspect_with_localization
60
- "#<#{self.class.name} #{attributes.except(*private_attribute_names).keys.map{|k| "#{localized_attribute?(k) ? "#{k}[#{I18n.locale}]" : k}: #{read_attribute(k).inspect}"}.join(", ")}>"
59
+ def inspect_attribute_with_localization(attr_name)
60
+ if self.class.localized_attribute?(attr_name)
61
+ "#{attr_name}[:#{I18n.locale}]: #{read_localized_attribute(attr_name).inspect}"
62
+ else
63
+ inspect_attribute_without_localization(attr_name)
64
+ end
61
65
  end
62
66
  end
63
67
 
@@ -23,9 +23,9 @@ module MongoODM
23
23
  # @param [Hash] options configuration options.
24
24
  # @option options [Object, Proc] :default (nil) a default value for the field, or a lambda to evaluate when providing the default.
25
25
  def initialize(name, type, options = {})
26
- @options = options.to_options
27
26
  @name = name.to_sym
28
27
  @type = type
28
+ @options = options.to_options
29
29
  end
30
30
 
31
31
  # @return [Object] The default value for this field if defined, or nil.
@@ -19,7 +19,7 @@ module MongoODM
19
19
  end
20
20
 
21
21
  def inspect
22
- "#<#{self.class.name} #{attributes.except(*private_attribute_names).keys.map{|k| "#{k}: #{read_attribute(k).inspect}"}.join(", ")}>"
22
+ "#<#{self.class.name} #{attributes.except(*private_attribute_names).keys.map{|k| inspect_attribute(k)}.join(", ")}>"
23
23
  end
24
24
  end
25
25
 
@@ -49,7 +49,7 @@ module MongoODM
49
49
 
50
50
  def to_mongo
51
51
  attributes.inject({}.with_indifferent_access) do |attrs, (key, value)|
52
- attrs[key] = value.to_mongo unless value.nil?
52
+ attrs[key] = value.to_mongo unless value.nil? # self.class.fields[key].default == value
53
53
  attrs
54
54
  end
55
55
  end
@@ -57,7 +57,11 @@ module MongoODM
57
57
 
58
58
  module ClassMethods
59
59
  def collection
60
- @collection ||= MongoODM::Collection.new(MongoODM.database, name.tableize)
60
+ @collection ||= if self.superclass.included_modules.include?(MongoODM::Document)
61
+ self.superclass.collection
62
+ else
63
+ MongoODM::Collection.new(MongoODM.database, name.tableize)
64
+ end
61
65
  end
62
66
 
63
67
  def set_collection(name_or_collection)
@@ -71,6 +75,10 @@ module MongoODM
71
75
  end
72
76
  end
73
77
 
78
+ def find(*args)
79
+ MongoODM::Criteria.new(collection, *args)
80
+ end
81
+
74
82
  def destroy_all(*args)
75
83
  documents = find(*args)
76
84
  count = documents.count
@@ -86,7 +94,7 @@ module MongoODM
86
94
 
87
95
  extend Forwardable
88
96
  def_delegators :collection, :db, :hint, :hint=, :pk_factory, :[], :count, :create_index, :distinct,
89
- :drop, :drop_index, :drop_indexes, :find, :find_and_modify, :find_one, :group,
97
+ :drop, :drop_index, :drop_indexes, :find_and_modify, :find_one, :group,
90
98
  :index_information, :insert, :<<, :map_reduce, :mapreduce, :options, :remove, :rename,
91
99
  :save, :stats, :update
92
100
  end
@@ -2,6 +2,6 @@
2
2
  module MongoODM
3
3
  VERSION_MAJOR = "0"
4
4
  VERSION_MINOR = "1"
5
- VERSION_BUILD = "8"
5
+ VERSION_BUILD = "9"
6
6
  VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_BUILD}"
7
7
  end
@@ -2,6 +2,5 @@
2
2
  class Circle < Shape
3
3
  include MongoODM::Document
4
4
 
5
- set_collection :shapes
6
5
  field :radius, Float, :default => 1.0
7
6
  end
@@ -1,8 +1,7 @@
1
1
  # encoding: utf-8
2
2
  class BigRedCircle < Circle
3
3
  include MongoODM::Document
4
-
5
- set_collection :shapes
4
+
6
5
  field :radius, Float, :default => 20.0
7
6
  field :color, String, :default => "red"
8
7
  end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM::Collection do
5
+
6
+ end
@@ -12,22 +12,26 @@ describe "Conversions" do
12
12
  end
13
13
 
14
14
  it "tries to convert any other value to an array" do
15
- Array.type_cast({:a => 1, :b => 2}).should == [[:a, 1], [:b, 2]]
16
15
  Array.type_cast([1, 2]).should == [1, 2]
16
+ Array.type_cast({:a => 1, :b => 2}).should == [[:a, 1], [:b, 2]]
17
+ end
18
+
19
+ it "tries to instantiate each of its values, recursively" do
20
+ Array.type_cast([3.1415, {"_class" => "Shape", "x" => 2.0, "y" => 3.0 } ]).should == [3.1415, Shape.new(:x => 2.0, :y => 3.0)]
21
+ Array.type_cast(["test", [ {"_class" => "Shape", "x" => 2.0, "y" => 3.0 } ]]).should == ["test", [ Shape.new(:x => 2.0, :y => 3.0) ]]
17
22
  end
18
23
 
19
24
  end
20
25
 
21
26
  describe "#to_mongo" do
22
27
 
23
- it "tries to instanciate each of its elements sending MongoODM.instanciate to its classes" do
28
+ it "tries to convert each of its elements to valid BSON" do
24
29
  num = 1
25
30
  float = 2.3
26
31
  string = "test"
27
32
  time = Time.now
28
33
  klass = Shape.new
29
- [num, float, string, time, klass].each{|i| i.should_receive(:to_mongo).once }
30
- [num, float, string, time, klass].to_mongo
34
+ [num, float, string, time, klass].to_mongo.should == [1, 2.3, "test", time.utc, {"_class"=>"Shape", "x"=>1.0, "y"=>1.0}]
31
35
  end
32
36
 
33
37
  end
@@ -370,6 +374,12 @@ describe "Conversions" do
370
374
  Hash.type_cast({:a => 1}).should == {:a => 1}
371
375
  end
372
376
 
377
+ it "tries to instantiate each of its keys and values, recursively" do
378
+ result = Hash.type_cast({"_class" => "Circle", "x" => 2.0, "y" => 3.0, "radius" => 5.0 } => { "test" => {"_class" => "Shape", "x" => 2.0, "y" => 3.0 }} )
379
+ result.keys.first.should == Circle.new(:x => 2.0, :y => 3.0, :radius => 5.0)
380
+ result.values.first.values.first.should == Shape.new(:x => 2.0, :y => 3.0)
381
+ end
382
+
373
383
  end
374
384
 
375
385
  describe "#to_mongo" do
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM::Cursor do
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM::Document::AttributeMethods::Dirty do
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM::Document::AttributeMethods::Localization do
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM::Document::AttributeMethods::Query do
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM::Document::AttributeMethods::Read do
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM::Document::AttributeMethods::Write do
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM::Document::AttributeMethods do
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM::Document::Callbacks do
5
+
6
+ end
@@ -8,16 +8,12 @@ describe MongoODM::Document::Fields do
8
8
  context "with a name and no type" do
9
9
 
10
10
  before do
11
- BlankSlate.field(:testing_no_type)
12
- end
13
-
14
- after do
15
- BlankSlate.instance_variable_set(:@fields, nil)
11
+ class TestingNoType < BlankSlate; field :testing_no_type; end
16
12
  end
17
13
 
18
14
  it "adds a String field to the class" do
19
- BlankSlate.fields[:testing_no_type].should be_kind_of(MongoODM::Document::Fields::Field)
20
- BlankSlate.fields[:testing_no_type].type.should == String
15
+ TestingNoType.fields[:testing_no_type].should be_kind_of(MongoODM::Document::Fields::Field)
16
+ TestingNoType.fields[:testing_no_type].type.should == String
21
17
  end
22
18
 
23
19
  end
@@ -25,12 +21,12 @@ describe MongoODM::Document::Fields do
25
21
  context "with a name and a type" do
26
22
 
27
23
  before do
28
- BlankSlate.field(:testing_float, Float)
24
+ class TestingFloat < BlankSlate; field :testing_float, Float; end
29
25
  end
30
26
 
31
27
  it "adds the typed field to the class" do
32
- BlankSlate.fields[:testing_float].should be_kind_of(MongoODM::Document::Fields::Field)
33
- BlankSlate.fields[:testing_float].type.should == Float
28
+ TestingFloat.fields[:testing_float].should be_kind_of(MongoODM::Document::Fields::Field)
29
+ TestingFloat.fields[:testing_float].type.should == Float
34
30
  end
35
31
 
36
32
  end
@@ -41,51 +37,37 @@ describe MongoODM::Document::Fields do
41
37
 
42
38
  context "on classes without defined fields" do
43
39
 
44
- before do
45
- @klass = BlankSlate
46
- end
47
-
48
40
  it "returns an empty hash" do
49
- @klass.fields.should == {}
41
+ BlankSlate.fields.should == {}
50
42
  end
51
43
 
52
44
  end
53
45
 
54
46
  context "on parent classes" do
55
47
 
56
- before do
57
- @klass = Shape
58
- end
59
-
60
48
  it "returns a hash with the defined fields" do
61
- expected_fields = [:x, :y, :color]
62
- expected_fields.each do |field_name|
63
- @klass.fields[field_name].should be_kind_of(MongoODM::Document::Fields::Field)
49
+ [:x, :y, :color].each do |field_name|
50
+ Shape.fields[field_name].should be_kind_of(MongoODM::Document::Fields::Field)
64
51
  end
65
52
  end
66
53
 
67
54
  it "allows indifferent access to the hash" do
68
- @klass.fields[:x].should == @klass.fields["x"]
55
+ Shape.fields[:x].should == Shape.fields["x"]
69
56
  end
70
57
 
71
58
  end
72
59
 
73
60
  context "on subclasses" do
74
61
 
75
- before do
76
- @klass = BigRedCircle
77
- end
78
-
79
62
  it "returns all the fields defined on the class and its subclasses" do
80
- expected_fields = [:x, :y, :radius, :color]
81
- expected_fields.each do |field_name|
82
- @klass.fields[field_name].should be_kind_of(MongoODM::Document::Fields::Field)
63
+ [:x, :y, :radius, :color].each do |field_name|
64
+ BigRedCircle.fields[field_name].should be_kind_of(MongoODM::Document::Fields::Field)
83
65
  end
84
66
  end
85
67
 
86
68
  it "overrides field descriptions on any parent class" do
87
- @klass.fields[:color].default.should == "red"
88
- @klass.fields[:radius].default.should == 20.0
69
+ BigRedCircle.fields[:color].default.should == "red"
70
+ BigRedCircle.fields[:radius].default.should == 20.0
89
71
  end
90
72
 
91
73
  end
@@ -176,7 +158,7 @@ describe MongoODM::Document::Fields::Field do
176
158
  @field = MongoODM::Document::Fields::Field.new(:invalid, InvalidClass)
177
159
  end
178
160
 
179
- it "returns the raw value" do
161
+ it "raises MongoODM::Errors::TypeCastMissing" do
180
162
  lambda { @field.type_cast("whatever") }.should raise_error(MongoODM::Errors::TypeCastMissing)
181
163
  end
182
164
 
@@ -9,6 +9,13 @@ describe MongoODM::Document::Inspect do
9
9
  end
10
10
  end
11
11
 
12
+ describe "#private_attribute_names" do
13
+ it "returns ['_class']" do
14
+ circle = Circle.new
15
+ circle.private_attribute_names.should == ['_class']
16
+ end
17
+ end
18
+
12
19
  describe "#inspect" do
13
20
  it "returns a string with the name of the class that includes the MongoODM::Document module and a list of its attributes with values" do
14
21
  circle = Circle.new(:x => 10, :y => 12.5, :radius => 1.2)
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM::Document::Persistence do
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM::Document::Validations do
5
+
6
+ end
@@ -2,11 +2,19 @@
2
2
  require "spec_helper"
3
3
 
4
4
  describe MongoODM::Document do
5
- # TODO: See ActiveModel::Lint::Tests for a minimal set of tests
6
- # test_valid?
7
- # test_new_record?
8
- # test_destroyed?
9
- # test_model_naming
10
- # test_errors_aref
11
- # test_errors_full_messages
5
+ # TODO: Test global behavior
6
+
7
+ describe "#==" do
8
+ it "returns false if the compared element is not a MongoODM::Document" do
9
+ (Shape.new == 1).should be_false
10
+ end
11
+
12
+ it "returns true if the compared document has the same attribute values" do
13
+ (Shape.new(:x => 2, :y => 3) == Shape.new(:x => 2, :y => 3)).should be_true
14
+ end
15
+
16
+ it "returns false if the compared document hasn't the same attribute values" do
17
+ (Shape.new(:x => 2, :y => 3) == Shape.new(:x => 1, :y => 3)).should be_false
18
+ end
19
+ end
12
20
  end
@@ -2,8 +2,31 @@
2
2
  require "spec_helper"
3
3
 
4
4
  describe MongoODM do
5
-
6
- describe "included modules" do
5
+
6
+ describe "#config" do
7
+
8
+ context "when no configuration was passed" do
9
+ it "returns an empty hash" do
10
+ MongoODM.config.should == {}
11
+ end
12
+
13
+ end
14
+
15
+ context "when a custom configuration was passed" do
16
+
17
+ before do
18
+ MongoODM.config = {:host => "localhost", :port => 9000}
19
+ end
20
+
21
+ after do
22
+ MongoODM.config = {}
23
+ end
24
+
25
+ it "should return the configuration as a hash" do
26
+ MongoODM.config.should == {:host => "localhost", :port => 9000}
27
+ end
28
+
29
+ end
7
30
 
8
31
  end
9
32
 
@@ -53,32 +76,4 @@ describe MongoODM do
53
76
  end
54
77
  end
55
78
 
56
- describe "#config" do
57
-
58
- context "when no configuration was passed" do
59
-
60
- it "returns an empty hash" do
61
- MongoODM.config.should == {}
62
- end
63
-
64
- end
65
-
66
- context "when a custom configuration was passed" do
67
-
68
- before do
69
- MongoODM.config = {:host => "localhost", :port => 9000}
70
- end
71
-
72
- after do
73
- MongoODM.config = {}
74
- end
75
-
76
- it "should return the configuration as a hash" do
77
- MongoODM.config.should == {:host => "localhost", :port => 9000}
78
- end
79
-
80
- end
81
-
82
- end
83
-
84
79
  end
data/spec/spec_helper.rb CHANGED
@@ -3,8 +3,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
4
4
 
5
5
  require "rubygems"
6
- require "spec"
7
- require "spec/mocks"
6
+ require "rspec"
8
7
  require "mongo_odm"
9
8
 
10
9
  # Load all example models
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 8
9
- version: 0.1.8
8
+ - 9
9
+ version: 0.1.9
10
10
  platform: ruby
11
11
  authors:
12
12
  - Carlos Paramio
@@ -14,12 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-10 00:00:00 +02:00
17
+ date: 2010-06-17 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: activesupport
22
- prerelease: false
23
22
  requirement: &id001 !ruby/object:Gem::Requirement
24
23
  none: false
25
24
  requirements:
@@ -32,10 +31,10 @@ dependencies:
32
31
  - beta3
33
32
  version: 3.0.0.beta3
34
33
  type: :runtime
34
+ prerelease: false
35
35
  version_requirements: *id001
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activemodel
38
- prerelease: false
39
38
  requirement: &id002 !ruby/object:Gem::Requirement
40
39
  none: false
41
40
  requirements:
@@ -48,29 +47,29 @@ dependencies:
48
47
  - beta3
49
48
  version: 3.0.0.beta3
50
49
  type: :runtime
50
+ prerelease: false
51
51
  version_requirements: *id002
52
52
  - !ruby/object:Gem::Dependency
53
53
  name: mongo
54
- prerelease: false
55
54
  requirement: &id003 !ruby/object:Gem::Requirement
56
55
  none: false
57
56
  requirements:
58
- - - "="
57
+ - - ~>
59
58
  - !ruby/object:Gem::Version
60
59
  segments:
61
60
  - 1
62
61
  - 0
63
- - 1
64
- version: 1.0.1
62
+ - 2
63
+ version: 1.0.2
65
64
  type: :runtime
65
+ prerelease: false
66
66
  version_requirements: *id003
67
67
  - !ruby/object:Gem::Dependency
68
- name: bson
69
- prerelease: false
68
+ name: bson_ext
70
69
  requirement: &id004 !ruby/object:Gem::Requirement
71
70
  none: false
72
71
  requirements:
73
- - - "="
72
+ - - ~>
74
73
  - !ruby/object:Gem::Version
75
74
  segments:
76
75
  - 1
@@ -78,22 +77,99 @@ dependencies:
78
77
  - 1
79
78
  version: 1.0.1
80
79
  type: :runtime
80
+ prerelease: false
81
81
  version_requirements: *id004
82
82
  - !ruby/object:Gem::Dependency
83
- name: bson_ext
84
- prerelease: false
83
+ name: tzinfo
85
84
  requirement: &id005 !ruby/object:Gem::Requirement
86
85
  none: false
87
86
  requirements:
88
- - - "="
87
+ - - ~>
89
88
  - !ruby/object:Gem::Version
90
89
  segments:
91
- - 1
92
90
  - 0
93
- - 1
94
- version: 1.0.1
91
+ - 3
92
+ - 22
93
+ version: 0.3.22
95
94
  type: :runtime
95
+ prerelease: false
96
96
  version_requirements: *id005
97
+ - !ruby/object:Gem::Dependency
98
+ name: jeweler
99
+ requirement: &id006 !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - "="
103
+ - !ruby/object:Gem::Version
104
+ segments:
105
+ - 1
106
+ - 4
107
+ - 0
108
+ version: 1.4.0
109
+ type: :development
110
+ prerelease: false
111
+ version_requirements: *id006
112
+ - !ruby/object:Gem::Dependency
113
+ name: yard
114
+ requirement: &id007 !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - "="
118
+ - !ruby/object:Gem::Version
119
+ segments:
120
+ - 0
121
+ - 5
122
+ - 6
123
+ version: 0.5.6
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: *id007
127
+ - !ruby/object:Gem::Dependency
128
+ name: rcov
129
+ requirement: &id008 !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - "="
133
+ - !ruby/object:Gem::Version
134
+ segments:
135
+ - 0
136
+ - 9
137
+ - 8
138
+ version: 0.9.8
139
+ type: :development
140
+ prerelease: false
141
+ version_requirements: *id008
142
+ - !ruby/object:Gem::Dependency
143
+ name: rspec
144
+ requirement: &id009 !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - "="
148
+ - !ruby/object:Gem::Version
149
+ segments:
150
+ - 2
151
+ - 0
152
+ - 0
153
+ - beta
154
+ - 12
155
+ version: 2.0.0.beta.12
156
+ type: :development
157
+ prerelease: false
158
+ version_requirements: *id009
159
+ - !ruby/object:Gem::Dependency
160
+ name: watchr
161
+ requirement: &id010 !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ~>
165
+ - !ruby/object:Gem::Version
166
+ segments:
167
+ - 0
168
+ - 6
169
+ version: "0.6"
170
+ type: :development
171
+ prerelease: false
172
+ version_requirements: *id010
97
173
  description: mongo_odm is a flexible persistence module for any Ruby class to MongoDB.
98
174
  email: carlos@evolve.st
99
175
  executables: []
@@ -111,6 +187,7 @@ files:
111
187
  - lib/mongo_odm.rb
112
188
  - lib/mongo_odm/collection.rb
113
189
  - lib/mongo_odm/core_ext/conversions.rb
190
+ - lib/mongo_odm/criteria.rb
114
191
  - lib/mongo_odm/cursor.rb
115
192
  - lib/mongo_odm/document.rb
116
193
  - lib/mongo_odm/document/associations.rb
@@ -118,6 +195,7 @@ files:
118
195
  - lib/mongo_odm/document/associations/has_one.rb
119
196
  - lib/mongo_odm/document/attribute_methods.rb
120
197
  - lib/mongo_odm/document/attribute_methods/dirty.rb
198
+ - lib/mongo_odm/document/attribute_methods/inspect.rb
121
199
  - lib/mongo_odm/document/attribute_methods/localization.rb
122
200
  - lib/mongo_odm/document/attribute_methods/query.rb
123
201
  - lib/mongo_odm/document/attribute_methods/read.rb
@@ -133,12 +211,22 @@ files:
133
211
  - spec/models/01-shape.rb
134
212
  - spec/models/02-circle.rb
135
213
  - spec/models/03-big_red_circle.rb
214
+ - spec/mongo_odm/collection_spec.rb
136
215
  - spec/mongo_odm/core_ext/conversions_spec.rb
216
+ - spec/mongo_odm/cursor_spec.rb
217
+ - spec/mongo_odm/document/attribute_methods/dirty_spec.rb
218
+ - spec/mongo_odm/document/attribute_methods/localization_spec.rb
219
+ - spec/mongo_odm/document/attribute_methods/query_spec.rb
220
+ - spec/mongo_odm/document/attribute_methods/read_spec.rb
221
+ - spec/mongo_odm/document/attribute_methods/write_spec.rb
222
+ - spec/mongo_odm/document/attribute_methods_spec.rb
223
+ - spec/mongo_odm/document/callbacks_spec.rb
137
224
  - spec/mongo_odm/document/fields_spec.rb
138
225
  - spec/mongo_odm/document/inspect_spec.rb
226
+ - spec/mongo_odm/document/persistence_spec.rb
227
+ - spec/mongo_odm/document/validations_spec.rb
139
228
  - spec/mongo_odm/document_spec.rb
140
229
  - spec/mongo_odm/mongo_odm_spec.rb
141
- - spec/spec.opts
142
230
  - spec/spec_helper.rb
143
231
  has_rdoc: true
144
232
  homepage: http://github.com/carlosparamio/mongo_odm
@@ -177,9 +265,20 @@ test_files:
177
265
  - spec/models/01-shape.rb
178
266
  - spec/models/02-circle.rb
179
267
  - spec/models/03-big_red_circle.rb
268
+ - spec/mongo_odm/collection_spec.rb
180
269
  - spec/mongo_odm/core_ext/conversions_spec.rb
270
+ - spec/mongo_odm/cursor_spec.rb
271
+ - spec/mongo_odm/document/attribute_methods/dirty_spec.rb
272
+ - spec/mongo_odm/document/attribute_methods/localization_spec.rb
273
+ - spec/mongo_odm/document/attribute_methods/query_spec.rb
274
+ - spec/mongo_odm/document/attribute_methods/read_spec.rb
275
+ - spec/mongo_odm/document/attribute_methods/write_spec.rb
276
+ - spec/mongo_odm/document/attribute_methods_spec.rb
277
+ - spec/mongo_odm/document/callbacks_spec.rb
181
278
  - spec/mongo_odm/document/fields_spec.rb
182
279
  - spec/mongo_odm/document/inspect_spec.rb
280
+ - spec/mongo_odm/document/persistence_spec.rb
281
+ - spec/mongo_odm/document/validations_spec.rb
183
282
  - spec/mongo_odm/document_spec.rb
184
283
  - spec/mongo_odm/mongo_odm_spec.rb
185
284
  - spec/spec_helper.rb
data/spec/spec.opts DELETED
@@ -1,4 +0,0 @@
1
- --colour
2
- --format nested
3
- --loadby mtime
4
- --reverse