peace_love 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -18,4 +18,13 @@ class Object
18
18
  end
19
19
 
20
20
  require 'mongo'
21
+
22
+ class BSON::ObjectID
23
+ def to_yaml(opts = {})
24
+ YAML.quick_emit(nil, opts) do |out|
25
+ out.scalar(nil, "oid(#{to_s})", :plain)
26
+ end
27
+ end
28
+ end
29
+
21
30
  $db = Mongo::Connection.new.db('sample-db')
@@ -2,9 +2,6 @@ require 'eg.helper'
2
2
  require 'angry_hash'
3
3
  require 'peace_love'
4
4
 
5
- eg.setup do
6
- PeaceLove.db = $db
7
- end
8
5
 
9
6
  module Kind
10
7
  def claws; "dainty" end
@@ -25,6 +22,12 @@ module Bear
25
22
  end
26
23
  end
27
24
 
25
+ eg.setup do
26
+ PeaceLove.db = $db
27
+ PeaceLove['bears'].mixin = nil
28
+ end
29
+
30
+
28
31
  eg 'loading doc' do
29
32
  $db['bears'].remove()
30
33
  $db['bears'].insert(:name => 'yogi', :liver => 'pure', :kind => {:fictional => true, :cartoon => true})
@@ -49,6 +52,8 @@ eg 'wrapping the cursor' do
49
52
  $db['bears'].remove()
50
53
  $db['bears'].insert(:name => 'yogi' , :liver => 'pure', :kind => {:fictional => true, :cartoon => true})
51
54
  $db['bears'].insert(:name => 'humphrey', :liver => 'cihrrotic', :kind => {:fictional => true, :cartoon => false})
55
+
56
+ PeaceLove['bears'].mixin = Bear
52
57
 
53
58
  i = 0
54
59
  PeaceLove['bears'].find.each {|b|
@@ -69,9 +74,9 @@ eg 'sub collection' do
69
74
  {:name => 'mrs. yogi', :liver => 'donated'}, {:name => 'yogi paw', :liver => 'jaundiced'}
70
75
  ])
71
76
 
77
+ PeaceLove['bears'].mixin = Bear
72
78
  yogi = PeaceLove['bears'].find_one(:name => 'yogi')
73
79
 
74
- Show(yogi.lovers)
75
80
  Assert(yogi.lovers[0].liver == 'DONATED')
76
81
  Assert(yogi.lovers[1].liver == 'JAUNDICED')
77
82
  end
@@ -87,3 +92,7 @@ eg 'saving object' do
87
92
  id = PeaceLove['bears'].insert( h )
88
93
  PeaceLove['bears'].find_one(id)
89
94
  end
95
+
96
+ eg 'id accessor' do
97
+ Assert(Bear.build(:id => 'abc').id == 'abc')
98
+ end
@@ -23,6 +23,8 @@ module PeaceLove
23
23
  end
24
24
  end
25
25
 
26
+ # TODO find_and_modify
27
+
26
28
  def __wrap_cursor(cursor)
27
29
  PeaceLove::Cursor.new(cursor,self)
28
30
  end
@@ -31,12 +33,14 @@ module PeaceLove
31
33
  return nil unless hash.respond_to?(:to_hash)
32
34
 
33
35
  hash = __extend( AngryHash[ hash ] )
34
- hash.extend mixin if mixin
35
36
  hash
36
37
  end
37
38
 
38
39
  def __extend(hash)
39
- hash.extend mixin if mixin
40
+ if mixin
41
+ hash.extend mixin
42
+ hash.__source_collection = self if hash.respond_to?(:__source_collection=)
43
+ end
40
44
  hash
41
45
  end
42
46
 
@@ -23,7 +23,7 @@ module PeaceLove
23
23
 
24
24
  (Mongo::Cursor.instance_methods - self.instance_methods).each do |name|
25
25
  next if name[-1] == ?=
26
- class_eval "def #{name}(*args,&block); @collection.#{name}(*args,&block) end" unless method_defined?(name)
26
+ class_eval "def #{name}(*args,&block); @cursor.#{name}(*args,&block) end" unless method_defined?(name)
27
27
  end
28
28
  end
29
29
  end
@@ -31,13 +31,18 @@ module PeaceLove
31
31
  mixin_registry[target_class][field.to_s] = [:array, mod, options]
32
32
  end
33
33
 
34
+ def extend_doc(doc,mod,parent_obj)
35
+ doc.extend mod
36
+ doc.__parent_doc = parent_obj if doc.respond_to?(:__parent_doc=)
37
+ doc
38
+ end
39
+
34
40
  def mixin_to(parent_obj,field,obj)
35
41
  # XXX - what does having multiple extensions really mean here?
36
42
  extensions = object_extensions[parent_obj.__id__]
37
43
 
38
44
  mixins = mixin_registry.values_at(*extensions).map {|m| m[field.to_s]}.compact
39
45
 
40
-
41
46
  mixins.each {|(kind,mod,options)|
42
47
  if options.key?(:default) && obj.nil?
43
48
  obj = options[:default]
@@ -47,10 +52,12 @@ module PeaceLove
47
52
 
48
53
  case kind
49
54
  when :single
50
- obj.extend mod
55
+ extend_doc(obj,mod,parent_obj)
51
56
  when :array
52
57
  # XXX - this is ok for now... we really need to typecheck, perhaps wrap in a smart-array
53
- obj.map! {|elt| elt.extend mod}
58
+
59
+
60
+ obj.map! {|elt| extend_doc elt, mod, parent_obj}
54
61
  end
55
62
  }
56
63
 
@@ -62,6 +69,26 @@ module PeaceLove
62
69
  Doc.mixin_to(self,key,super)
63
70
  end
64
71
 
72
+ def id
73
+ self['id']
74
+ end
75
+
76
+ def __source_collection=(col)
77
+ @source_collection = col
78
+ end
79
+ def __parent_doc(doc)
80
+ self.__source_collection = doc.__source_collection if doc.respond_to?(:__source_collection)
81
+ @parent_doc = doc
82
+ end
83
+
84
+ def __parent_doc
85
+ @parent_doc
86
+ end
87
+
88
+ def __source_collection
89
+ @source_collection
90
+ end
91
+
65
92
  module ClassMethods
66
93
  def sub_document(field,mod,options={})
67
94
  Doc.register_mixin(self,field,mod,options)
data/peace_love.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{peace_love}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Lachie Cox"]
12
- s.date = %q{2010-06-28}
12
+ s.date = %q{2010-07-14}
13
13
  s.description = %q{A simple mixin layer for enhancing hashes retrieved from MongoDB. It eschews the normal 'mapping' compulsion of mongo libraries.}
14
14
  s.email = %q{lachie@smartbomb.com.au}
15
15
  s.files = [
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Lachie Cox
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-28 00:00:00 +10:00
17
+ date: 2010-07-14 00:00:00 +10:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency