mongo_light 0.0.1 → 0.0.2

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.
@@ -14,9 +14,15 @@ module MongoLight
14
14
  found = collection.find_one(real_id)
15
15
  found.nil? ? nil : self.new(unmap(found))
16
16
  end
17
- def find_one(selector = {})
17
+ def find_one(selector = {}, opts={})
18
18
  found = collection.find_one(map(selector))
19
- found.nil? ? nil : self.new(unmap(found))
19
+ if found.nil?
20
+ return nil
21
+ end
22
+ if opts.delete(:raw)
23
+ return unmap(found, true)
24
+ end
25
+ self.new(unmap(found))
20
26
  end
21
27
  def find(selector={}, opts={}, collection = nil)
22
28
  raw = opts.delete(:raw) || false
@@ -64,11 +70,11 @@ module MongoLight
64
70
  def collection
65
71
  self.class.collection
66
72
  end
67
- def save
68
- collection.save(self.class.map(@attributes))
73
+ def save(safe = false)
74
+ collection.save(self.class.map(@attributes), {:safe => safe})
69
75
  end
70
76
  def save!
71
- collection.save(self.class.map(@attributes), {:safe => true})
77
+ save(true)
72
78
  end
73
79
  end
74
80
  end
@@ -1,3 +1,4 @@
1
+ require 'active_support'
1
2
  module MongoLight
2
3
  module EmbeddedDocument
3
4
  extend ActiveSupport::Concern
@@ -10,24 +11,27 @@ module MongoLight
10
11
  define_method(k) { @attributes[k] }
11
12
  define_method("#{k}=") {|value| @attributes[k] = value }
12
13
  if v.is_a?(Hash)
13
- @unmap[v[:field].to_s] = {:prop => k, :class => v[:class]}
14
+ @unmap[v[:field].to_s] = {:prop => k, :class => v[:class], :array => v[:array]}
14
15
  else
15
16
  @unmap[v.to_s] = k
16
17
  end
17
18
  end
18
19
  end
19
20
  def map(raw)
20
- return {} if raw.blank? || !raw.is_a?(Hash)
21
+ return {} if raw.nil? || !raw.is_a?(Hash)
21
22
  hash = {}
22
23
  raw.each do |key, value|
24
+ sym = key.to_sym
23
25
  if value.is_a?(EmbeddedDocument)
24
26
  v = value.class.map(value.attributes)
25
27
  elsif value.is_a?(Hash)
26
28
  v = map(value)
29
+ elsif value.is_a?(Array) && @map[sym].is_a?(Hash) && @map[sym][:array]
30
+ v = value.map{|vv| vv.class.map(vv.attributes)}
27
31
  else
28
32
  v = value
29
33
  end
30
- hash[map_key(key.to_sym)] = v
34
+ hash[map_key(sym)] = v
31
35
  end
32
36
  return hash
33
37
  end
@@ -37,13 +41,17 @@ module MongoLight
37
41
  options
38
42
  end
39
43
  def unmap(data, raw = false)
40
- return {} if data.blank? || !data.is_a?(Hash)
44
+ return {} if data.nil? || !data.is_a?(Hash)
41
45
  hash = {}
42
46
  data.each do |key, value|
43
47
  if @unmap[key].is_a?(Hash)
44
48
  real_key = @unmap[key][:prop]
45
49
  c = @unmap[key][:class]
46
- v = raw ? c.unmap(value) : c.new(c.unmap(value))
50
+ if @unmap[key][:array]
51
+ v = value.map{|vv| raw ? c.unmap(vv) : c.new(c.unmap(vv))}
52
+ else
53
+ v = raw ? c.unmap(value) : c.new(c.unmap(value))
54
+ end
47
55
  else
48
56
  real_key = key == '_id' ? :_id : @unmap[key]
49
57
  v = value
@@ -4,7 +4,7 @@ module MongoLight
4
4
  BSON::ObjectId.new
5
5
  end
6
6
  def self.valid?(id)
7
- return false if id.blank? || id.class == Fixnum
7
+ return false if id.nil? || id.class == Fixnum
8
8
  return true if id.class == BSON::ObjectId
9
9
  BSON::ObjectId.legal?(id)
10
10
  end
@@ -0,0 +1,10 @@
1
+ module Mongo
2
+ class OperationFailure
3
+ def duplicate?
4
+ !(message =~ /duplicate key/).nil?
5
+ end
6
+ def duplicate_on?(field)
7
+ message.include? field.is_a?(Symbol) ? field.to_s : field
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module MongoLight
2
- Version = '0.0.1'
2
+ Version = '0.0.2'
3
3
  end
data/lib/mongo_light.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'mongo_light/id'
2
2
  require 'mongo_light/connection'
3
3
  require 'mongo_light/embedded_document'
4
- require 'mongo_light/document'
4
+ require 'mongo_light/document'
5
+ require 'mongo_light/mongo_extensions'
data/readme.markdown CHANGED
@@ -54,11 +54,16 @@ Next, use the following fancy syntax for your root document:
54
54
 
55
55
  class User
56
56
  include MongoLight::Document
57
- mongo_accessor({:name => :name, password => :pw, :email => :e, :comments => {:field => :c, :class => Comment}})
57
+ mongo_accessor({:name => :name, password => :pw, :email => :e, :comment => {:field => :c, :class => Comment}})
58
58
  end
59
59
 
60
60
  Again, this'll make `comments` accessible on your objects, but store it within the `c` field.
61
61
 
62
+ If you want an array of embedded documents, simply specify `:array => true`:
63
+
64
+ ...
65
+ :comments => {:field => :c, :class => Comment, array => true}})
66
+
62
67
  Please note that aliasing completely fails when querying embedded documents - you need to use the shortened name:
63
68
 
64
69
  User.find({:name => 'blah', 'c.t' => 'blah2}) #yes, it sucks
metadata CHANGED
@@ -1,62 +1,54 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mongo_light
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
4
5
  prerelease:
5
- version: 0.0.1
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Karl Seguin
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-06-27 00:00:00 Z
12
+ date: 2011-08-13 00:00:00.000000000 Z
14
13
  dependencies: []
15
-
16
14
  description:
17
- email:
18
- - karlseguin@gmail.com
15
+ email:
16
+ - karl@openmymind.net
19
17
  executables: []
20
-
21
18
  extensions: []
22
-
23
19
  extra_rdoc_files: []
24
-
25
- files:
20
+ files:
26
21
  - lib/mongo_light/connection.rb
27
22
  - lib/mongo_light/document.rb
28
23
  - lib/mongo_light/embedded_document.rb
29
24
  - lib/mongo_light/id.rb
25
+ - lib/mongo_light/mongo_extensions.rb
30
26
  - lib/mongo_light/version.rb
31
27
  - lib/mongo_light.rb
32
28
  - license
33
29
  - readme.markdown
34
30
  homepage: http://github.com/karlseguib/MongoLight
35
31
  licenses: []
36
-
37
32
  post_install_message:
38
33
  rdoc_options: []
39
-
40
- require_paths:
34
+ require_paths:
41
35
  - lib
42
- required_ruby_version: !ruby/object:Gem::Requirement
36
+ required_ruby_version: !ruby/object:Gem::Requirement
43
37
  none: false
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: "0"
48
- required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
43
  none: false
50
- requirements:
51
- - - ">="
52
- - !ruby/object:Gem::Version
53
- version: "0"
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
54
48
  requirements: []
55
-
56
49
  rubyforge_project:
57
- rubygems_version: 1.8.5
50
+ rubygems_version: 1.8.6
58
51
  signing_key:
59
52
  specification_version: 3
60
53
  summary: A Lightweight ORM for Rails and MongoDB
61
54
  test_files: []
62
-