mongoid_embedded_helper 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -11,3 +11,4 @@ vendor
11
11
 
12
12
 
13
13
 
14
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -1,7 +1,7 @@
1
1
  require 'mongoid'
2
2
 
3
3
  module Mongoid
4
- module EmbeddedHelper
4
+ module EmbeddedHelper
5
5
  def in_collection stack = []
6
6
  stack.extend(ArrayExt) if stack.empty?
7
7
  if embedded?
@@ -34,3 +34,54 @@ module Mongoid
34
34
  end
35
35
  end
36
36
  end
37
+
38
+ class NilClass
39
+ def integer?
40
+ false
41
+ end
42
+ end
43
+
44
+ module Mongoid::Extensions::Array
45
+ module Mutators
46
+ def adjust!(attrs = {})
47
+ attrs.each_pair do |key, value|
48
+ self.each do |doc|
49
+ doc.adjust_attribs!(attrs)
50
+ end
51
+ end
52
+ self
53
+ end
54
+ end
55
+ end
56
+
57
+ module Mongoid::Document
58
+ def adjust_attribs!(attrs = {})
59
+ run_callbacks(:before_update)
60
+ (attrs || {}).each_pair do |key, value|
61
+ next if !value.integer?
62
+
63
+ if set_allowed?(key)
64
+ current_val = @attributes[key.to_s]
65
+
66
+ if current_val.integer?
67
+ @attributes[key.to_s] = current_val + value
68
+ end
69
+
70
+ elsif write_allowed?(key)
71
+ current_val = send("#{key}")
72
+
73
+ if current_val.integer?
74
+ send("#{key}=", current_val + value)
75
+ end
76
+ end
77
+ identify if id.blank?
78
+ notify
79
+ run_callbacks(:after_update)
80
+ self
81
+ end
82
+ end
83
+ end
84
+
85
+ class Array
86
+ include Mongoid::Extensions::Array::Mutators
87
+ end
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mongoid_embedded_helper}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kristian Mandrup"]
12
+ s.date = %q{2010-07-05}
13
+ s.description = %q{Facilitates performing queries on collections in embedded Mongoid documents by performing query from the root node}
14
+ s.email = %q{kmandrup@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "Gemfile",
21
+ "MITLICENSE",
22
+ "README.markdown",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "autotest/discover.rb",
26
+ "lib/mongoid/embedded_helper.rb",
27
+ "lib/mongoid_embedded_helper.rb",
28
+ "mongoid_embedded_helper.gemspec",
29
+ "spec/mongoid/embedded_helper_spec.rb",
30
+ "spec/rspec.options",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/kristianmandrup/mongoid_embedded_helper}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.7}
37
+ s.summary = %q{Facilitates performing queries on collections in embedded Mongoid documents}
38
+ s.test_files = [
39
+ "spec/mongoid/embedded_helper_spec.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
+ s.add_runtime_dependency(%q<mongoid>, ["<= 2.0.0"])
49
+ s.add_runtime_dependency(%q<bson>, [">= 1.0.3"])
50
+ s.add_development_dependency(%q<rspec>, [">= 2.0.0.beta.12"])
51
+ else
52
+ s.add_dependency(%q<mongoid>, ["<= 2.0.0"])
53
+ s.add_dependency(%q<bson>, [">= 1.0.3"])
54
+ s.add_dependency(%q<rspec>, [">= 2.0.0.beta.12"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<mongoid>, ["<= 2.0.0"])
58
+ s.add_dependency(%q<bson>, [">= 1.0.3"])
59
+ s.add_dependency(%q<rspec>, [">= 2.0.0.beta.12"])
60
+ end
61
+ end
62
+
@@ -67,6 +67,11 @@ describe 'Mongoid Embedded Helper' do
67
67
  result = @person.lists[0].items[0].in_collection.where(:number.gt => 1).to_a
68
68
  result.size.should == 2
69
69
  end
70
+
71
+ it "should add 1 to all positions greater than 1" do
72
+ result = @person.lists[0].items.where(:pos.gt => 1).to_a.adjust!(:pos => 1)
73
+ result.map(&:pos).should == [3, 4]
74
+ end
70
75
  end
71
76
  end
72
77
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -82,6 +82,7 @@ files:
82
82
  - autotest/discover.rb
83
83
  - lib/mongoid/embedded_helper.rb
84
84
  - lib/mongoid_embedded_helper.rb
85
+ - mongoid_embedded_helper.gemspec
85
86
  - spec/mongoid/embedded_helper_spec.rb
86
87
  - spec/rspec.options
87
88
  - spec/spec_helper.rb