peace_love 0.0.5 → 0.1.0

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.
data/Rakefile CHANGED
@@ -9,6 +9,7 @@ begin
9
9
  gemspec.authors = ["Lachie Cox"]
10
10
 
11
11
  gemspec.add_dependency("mongo", ['~>1.0.0'])
12
+ gemspec.add_dependency("angry_hash", ['=0.0.5'])
12
13
  end
13
14
  rescue LoadError
14
15
  puts "Jeweler not available. Install it with: gem install jeweler"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.1.0
File without changes
File without changes
@@ -19,6 +19,7 @@ end
19
19
 
20
20
  require 'mongo'
21
21
 
22
+ # In YAML, output BSON ObjectIds succinctly.
22
23
  class BSON::ObjectID
23
24
  def to_yaml(opts = {})
24
25
  YAML.quick_emit(nil, opts) do |out|
@@ -0,0 +1,58 @@
1
+ require 'eg.helper'
2
+ require 'peace_love'
3
+
4
+ PeaceLove.db = $db
5
+
6
+ eg.setup do
7
+ PeaceLove['beans'].remove
8
+ end
9
+
10
+ module Bean
11
+ include PeaceLove::Doc
12
+ mongo_collection 'beans'
13
+
14
+ def is_a_bean?; true end
15
+ end
16
+
17
+ eg 'arthur' do
18
+ lima_bean = { :name => 'lima' }
19
+ human_bean = { :name => 'Arthur', :prefered_drinks => %w[tea] }
20
+
21
+ PeaceLove['beans'].insert(lima_bean)
22
+ PeaceLove['beans'].insert(human_bean)
23
+
24
+ arthur = PeaceLove['beans'].find_one(:name => 'Arthur')
25
+
26
+ Show(arthur)
27
+
28
+ Show( arthur.prefered_drinks ) #=> [ 'tea' ]
29
+ Show( arthur.is_a_bean? ) #=> true
30
+
31
+ Show(arthur.is_a? Hash)
32
+ Show(arthur.class)
33
+
34
+ PeaceLove['beans'].update({:name => 'Arthur'}, '$push' => {'prefered_drinks' => 'pan-galactic gargle blaster'})
35
+ arthur = PeaceLove['beans'].find_one(:name => 'Arthur')
36
+
37
+ Show( arthur.prefered_drinks )
38
+ end
39
+
40
+ eg 'chaos taste' do
41
+ module Taste
42
+ def zesty?
43
+ spicy? && sour?
44
+ end
45
+ end
46
+
47
+ module Bean2
48
+ include PeaceLove::Doc
49
+ mongo_collection 'beans'
50
+
51
+ sub_doc :taste, Taste
52
+ end
53
+
54
+ PeaceLove['beans'].mixin = Bean2
55
+
56
+ chaos = PeaceLove['beans'].build(:name => 'chaos', :taste => {:sour => true, :spicy => true})
57
+ Show( chaos.taste.zesty? ) #=> true
58
+ end
data/examples/usage.eg.rb CHANGED
@@ -3,118 +3,144 @@ require 'angry_hash'
3
3
  require 'peace_love'
4
4
 
5
5
 
6
+
7
+ ## Fixtures
8
+ # Here are a couple of modules we're going to mix into hashes from Mongo.
9
+
10
+ # Note that `PeaceLove.db` needs to be set before `mongo_collection` is called.
11
+ # This will be fixed in the future.
12
+ PeaceLove.db = $db
13
+
6
14
  module Kind
7
- def claws; "dainty" end
15
+ def healthy; "healthy" end
8
16
 
9
- def for_kids?
10
- fictional? && cartoon?
17
+ def kids_love?
18
+ fictional?
11
19
  end
12
20
  end
13
21
 
14
- module Bear
22
+ module Bean
15
23
  include PeaceLove::Doc
16
- sub_doc :kind, Kind
17
- sub_col :lovers, Bear
24
+ sub_doc :kind , Kind
25
+ sub_col :examples, Bean
18
26
 
19
- def claws; "woah" end
20
- def liver
21
- super.upcase
22
- end
27
+ mongo_collection 'beans'
28
+
29
+ def texture; super.upcase end
23
30
  end
24
31
 
25
32
  eg.setup do
26
- PeaceLove.db = $db
27
33
 
28
- @mbears = $db['bears']
29
- @mbears.remove()
34
+ @mongo_beans = $db['beans']
35
+ @mongo_beans.remove()
30
36
 
31
- Bear.collection = 'bears'
32
- @plbears = PeaceLove['bears']
37
+ @peace_love_beans = PeaceLove['beans']
33
38
  end
39
+
34
40
  eg.helpers do
35
- attr_reader :mbears, :plbears
41
+ attr_reader :mongo_beans, :peace_love_beans
36
42
  end
37
43
 
38
44
 
39
- eg 'loading a document with a mixin' do
40
- mbears.insert(:name => 'yogi', :liver => 'pure', :kind => {:fictional => true, :cartoon => true})
45
+ eg 'loading a document' do
46
+ mongo_beans.insert(:name => 'lima',
47
+ :texture => 'crunchy',
48
+ :colours => %w[green white],
49
+ :kind => {:grown => true,
50
+ :fictional => false
51
+ }
52
+ )
53
+
54
+ lima = peace_love_beans.find_one(:name => 'lima')
41
55
 
42
- yogi = plbears.find_one(:name => 'yogi')
56
+ # attributes on the document
57
+ Assert( lima.name == 'lima' )
58
+ Assert( lima.colours == %w[green white] )
43
59
 
44
- Assert( yogi.claws == 'woah' )
45
- Assert( yogi.kind.claws == 'dainty' )
46
- Assert( yogi.name == 'yogi' )
60
+ # methods on a document
61
+ Assert( lima.texture == 'CRUNCHY' )
62
+
63
+ # sub-document
64
+ Assert( lima.kind.grown? )
65
+
66
+ # methods on a sub-doc
67
+ Assert( lima.kind.healthy == 'healthy' )
68
+ Assert( ! lima.kind.kids_love? )
47
69
 
48
- Assert( yogi.liver == 'PURE' )
49
- Assert( yogi.kind.for_kids? )
50
70
  end
51
71
 
52
72
 
53
- eg 'loading a non-existent doc' do
54
- b = plbears.find_one(:name => 'bertrand')
73
+ eg 'loading a non-existent documents' do
74
+ b = peace_love_beans.find_one(:name => 'delicious')
55
75
 
56
76
  Assert( b.nil? )
57
77
  end
58
78
 
59
79
 
60
- eg 'wrapping the mongo cursor' do
61
- mbears.insert(:name => 'yogi' , :liver => 'pure', :kind => {:fictional => true, :cartoon => true})
62
- mbears.insert(:name => 'humphrey', :liver => 'cihrrotic', :kind => {:fictional => true, :cartoon => false})
63
80
 
64
-
81
+ eg 'loading a list of documents' do
82
+ mongo_beans.insert(:name => 'baked' , :texture => 'Mushy' , :kind => {:fictional => false })
83
+ mongo_beans.insert(:name => 'magical', :texture => 'sparkly', :kind => {:fictional => true })
84
+
65
85
  i = 0
66
- plbears.find.each {|b|
86
+ peace_love_beans.find.each {|b|
67
87
 
68
88
  case i
69
89
  when 0
70
- Assert( b.kind.claws == 'dainty' )
71
- Assert( b.liver == 'PURE' )
90
+ Assert( b.name == 'baked' )
91
+ Assert( ! b.kind.kids_love? )
92
+ Assert( b.texture == 'MUSHY' )
72
93
  when 1
73
- Assert( b.kind.claws == 'dainty' )
74
- Assert( b.liver == 'CIHRROTIC' )
94
+ Assert( b.name == 'magical' )
95
+ Assert( b.kind.kids_love? )
96
+ Assert( b.texture == 'SPARKLY' )
75
97
  end
76
98
 
77
99
  i += 1
78
100
  }
79
101
  end
80
102
 
81
-
82
- eg 'mixing in to each element of a sub collection' do
83
- mbears.remove()
84
- mbears.insert(:name => 'yogi', :liver => 'pure', :kind => {:fictional => true, :cartoon => true}, :lovers =>
85
- [
86
- {:name => 'mrs. yogi', :liver => 'donated'}, {:name => 'yogi paw', :liver => 'jaundiced'}
103
+ eg 'looking into an array sub collection' do
104
+ mongo_beans.insert(:name => 'jelly', :texture => 'wibbly', :kind => {:fictional => false},
105
+ :examples => [
106
+ {:name => 'red' , :texture => 'raspberry'},
107
+ {:name => 'black', :texture => 'shunned' }
87
108
  ])
109
+
88
110
 
89
- yogi = plbears.find_one(:name => 'yogi')
111
+ jelly = peace_love_beans.find_one(:name => 'jelly')
90
112
 
91
- Assert( yogi.lovers[0].liver == 'DONATED' )
92
- Assert( yogi.lovers[1].liver == 'JAUNDICED' )
113
+ Assert( jelly.examples[0].texture == 'RASPBERRY' )
114
+ Assert( jelly.examples[1].texture == 'SHUNNED' )
93
115
  end
94
116
 
95
117
 
96
- eg 'building a blank doc' do
97
- yogi = plbears.build
118
+ eg 'building a blank document' do
119
+ bean = peace_love_beans.build
98
120
 
99
- Assert( yogi.claws == 'woah' )
100
- Assert( yogi.kind.claws == 'dainty' )
121
+ Assert( bean.nothing.nil? )
122
+ Assert( bean.kind.healthy == 'healthy' )
101
123
  end
102
124
 
103
125
 
104
126
  eg 'saving a hash' do
105
- h = AngryHash[ :somesuch => 'second thing' ]
106
- id = plbears.insert( h )
127
+ h = { :some_key => 'some value' }
128
+ id = peace_love_beans.insert( h )
107
129
 
108
- Assert( plbears.find_one(id).somesuch == 'second thing' )
130
+ Assert( peace_love_beans.find_one(id).some_key == 'some value' )
109
131
  end
110
132
 
111
133
 
112
134
  eg 'the id accessor works in ruby 1.8 & 1.9' do
113
- Assert(Bear.build(:id => 'abc').id == 'abc')
135
+ Assert( Bean.build(:id => 'abc').id == 'abc' )
114
136
  end
115
137
 
138
+ __END__
139
+
140
+ eg 'looking into a hash sub collection' do
141
+ end
116
142
 
117
143
  # TODO
118
- #eg 'setting the mixin on the collection' do
119
- #PeaceLove['bears'].mixin = Bear
120
- #end
144
+ eg 'setting the mixin on the collection' do
145
+ PeaceLove['beans'].mixin = Bear
146
+ end
@@ -15,10 +15,14 @@ module PeaceLove
15
15
 
16
16
 
17
17
  def object_extensions
18
- @object_extensions ||= Hash.new {|h,k| h[k] = []}
18
+ @object_extensions ||= {}
19
19
  end
20
+
20
21
  def mark_extension(obj,with)
21
- object_extensions[obj.__id__] << with
22
+ if (previously_with = object_extensions[obj.__id__]) && previously_with != with
23
+ raise "object #{obj} has already been extended by a different PeaceLove::Doc (was: #{previously_with}, now: #{with})"
24
+ end
25
+ object_extensions[obj.__id__] = with
22
26
  end
23
27
 
24
28
  def mixin_registry
@@ -28,6 +32,7 @@ module PeaceLove
28
32
  def register_mixin(target_class,field,mod,options)
29
33
  mixin_registry[target_class][field.to_s] = [:single, mod, options]
30
34
  end
35
+
31
36
  def register_mixin_array(target_class, field, mod, options)
32
37
  mixin_registry[target_class][field.to_s] = [:array, mod, options]
33
38
  end
@@ -39,12 +44,11 @@ module PeaceLove
39
44
  end
40
45
 
41
46
  def mixin_to(parent_obj,field,obj)
42
- # XXX - what does having multiple extensions really mean here?
43
- extensions = object_extensions[parent_obj.__id__]
47
+ extension = object_extensions[parent_obj.__id__]
44
48
 
45
- mixins = mixin_registry.values_at(*extensions).map {|m| m[field.to_s]}.compact
49
+ if mixin = mixin_registry[extension][field.to_s]
50
+ kind,mod,options = *mixin
46
51
 
47
- mixins.each {|(kind,mod,options)|
48
52
  if options.key?(:default) && obj.nil?
49
53
  obj = options[:default]
50
54
  end
@@ -52,15 +56,15 @@ module PeaceLove
52
56
  # XXX - what happens when obj is nil
53
57
 
54
58
  case kind
55
- when :single
56
- extend_doc(obj,mod,parent_obj)
57
- when :array
58
- # XXX - this is ok for now... we really need to typecheck, perhaps wrap in a smart-array
59
+ when :single
60
+ extend_doc(obj,mod,parent_obj)
61
+ when :array
62
+ # XXX - this is ok for now... we really need to typecheck, perhaps wrap in a smart-array
59
63
 
60
64
 
61
- obj.map! {|elt| extend_doc elt, mod, parent_obj}
62
- end
63
- }
65
+ obj.map! {|elt| extend_doc elt, mod, parent_obj}
66
+ end
67
+ end
64
68
 
65
69
  obj
66
70
  end
@@ -77,6 +81,7 @@ module PeaceLove
77
81
  def __source_collection=(col)
78
82
  @source_collection = col
79
83
  end
84
+
80
85
  def __parent_doc(doc)
81
86
  self.__source_collection = doc.__source_collection if doc.respond_to?(:__source_collection)
82
87
  @parent_doc = doc
@@ -95,8 +100,11 @@ module PeaceLove
95
100
  @collection = PeaceLove[collection_name]
96
101
  @collection.set_mixin(self)
97
102
  end
103
+ alias mongo_collection collection=
98
104
 
99
- def collection; @collection end
105
+ def collection
106
+ @collection
107
+ end
100
108
 
101
109
  def sub_document(field,mod,options={})
102
110
  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.5"
8
+ s.version = "0.1.0"
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-07-14}
12
+ s.date = %q{2010-07-18}
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 = [
@@ -32,7 +32,10 @@ Gem::Specification.new do |s|
32
32
  s.rubygems_version = %q{1.3.6}
33
33
  s.summary = %q{Peace, Love and Mongo.}
34
34
  s.test_files = [
35
- "examples/eg.helper.rb",
35
+ "examples/collections.eg.rb",
36
+ "examples/documents.eg.rb",
37
+ "examples/eg.helper.rb",
38
+ "examples/readme.eg.rb",
36
39
  "examples/usage.eg.rb"
37
40
  ]
38
41
 
@@ -42,11 +45,14 @@ Gem::Specification.new do |s|
42
45
 
43
46
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
44
47
  s.add_runtime_dependency(%q<mongo>, ["~> 1.0.0"])
48
+ s.add_runtime_dependency(%q<angry_hash>, ["= 0.0.5"])
45
49
  else
46
50
  s.add_dependency(%q<mongo>, ["~> 1.0.0"])
51
+ s.add_dependency(%q<angry_hash>, ["= 0.0.5"])
47
52
  end
48
53
  else
49
54
  s.add_dependency(%q<mongo>, ["~> 1.0.0"])
55
+ s.add_dependency(%q<angry_hash>, ["= 0.0.5"])
50
56
  end
51
57
  end
52
58
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 5
9
- version: 0.0.5
9
+ version: 0.1.0
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-07-14 00:00:00 +10:00
17
+ date: 2010-07-18 00:00:00 +10:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -31,6 +31,20 @@ dependencies:
31
31
  version: 1.0.0
32
32
  type: :runtime
33
33
  version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: angry_hash
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 0
44
+ - 5
45
+ version: 0.0.5
46
+ type: :runtime
47
+ version_requirements: *id002
34
48
  description: A simple mixin layer for enhancing hashes retrieved from MongoDB. It eschews the normal 'mapping' compulsion of mongo libraries.
35
49
  email: lachie@smartbomb.com.au
36
50
  executables: []
@@ -83,5 +97,8 @@ signing_key:
83
97
  specification_version: 3
84
98
  summary: Peace, Love and Mongo.
85
99
  test_files:
100
+ - examples/collections.eg.rb
101
+ - examples/documents.eg.rb
86
102
  - examples/eg.helper.rb
103
+ - examples/readme.eg.rb
87
104
  - examples/usage.eg.rb