mm_no_empties 0.0.1
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/.gitignore +18 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README.md +38 -0
- data/Rakefile +1 -0
- data/lib/mm_no_empties.rb +51 -0
- data/lib/mm_no_empties/version.rb +3 -0
- data/mm_no_empties.gemspec +23 -0
- data/spec/mm_no_empties_spec.rb +50 -0
- metadata +116 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
mm_no_empties
|
3
|
+
============
|
4
|
+
|
5
|
+
Models that use this plugin do not persist empty Arrays, Hashes or Sets. Nil values are also excluded as per standard MM behaviour.
|
6
|
+
|
7
|
+
Requirements
|
8
|
+
============
|
9
|
+
|
10
|
+
- Ruby 1.9
|
11
|
+
- MongoMapper 0.10.1 or greater
|
12
|
+
|
13
|
+
Installation
|
14
|
+
=======
|
15
|
+
|
16
|
+
Add this to your Gemfile if using Bundler: `gem 'mm_no_empties'`
|
17
|
+
|
18
|
+
Or install the gem from the command line: `gem install mm_no_empties`
|
19
|
+
|
20
|
+
Usage
|
21
|
+
=======
|
22
|
+
|
23
|
+
Use the MongoMapper `plugin` method to add MmNoEmpties to your model, for example:
|
24
|
+
|
25
|
+
```
|
26
|
+
class Group
|
27
|
+
include MongoMapper::Document
|
28
|
+
plugin MmNoEmpties
|
29
|
+
|
30
|
+
many :people, :class_name => 'Person'
|
31
|
+
|
32
|
+
key :names, Array
|
33
|
+
key :counts, Hash
|
34
|
+
key :tags, Set
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
Copyright (c) 2011 PeepAll Ltd, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'mongo_mapper'
|
2
|
+
require "mm_no_empties/version"
|
3
|
+
require 'mongo_mapper/plugins/keys/key'
|
4
|
+
|
5
|
+
module MmNoEmpties
|
6
|
+
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
module InstanceMethods
|
10
|
+
|
11
|
+
def attributes(include_all = false)
|
12
|
+
|
13
|
+
HashWithIndifferentAccess.new.tap do |attrs|
|
14
|
+
persistable_keys = if include_all
|
15
|
+
keys
|
16
|
+
else
|
17
|
+
keys.select do |name, key|
|
18
|
+
val = self[key.name]
|
19
|
+
key.type == ObjectId or
|
20
|
+
not (val.nil? or (val.respond_to?(:empty?) and val.empty?))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
persistable_keys.each do |name, key|
|
25
|
+
value = key.set(self[key.name])
|
26
|
+
attrs[name] = value
|
27
|
+
end
|
28
|
+
|
29
|
+
embedded_associations.each do |association|
|
30
|
+
if documents = instance_variable_get(association.ivar)
|
31
|
+
|
32
|
+
val = if association.is_a?(MongoMapper::Plugins::Associations::OneAssociation)
|
33
|
+
documents.to_mongo
|
34
|
+
else
|
35
|
+
documents.map { |document| document.to_mongo }
|
36
|
+
end
|
37
|
+
|
38
|
+
if include_all
|
39
|
+
attrs[association.name] = val
|
40
|
+
else
|
41
|
+
attrs[association.name] = val unless val.empty?
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
alias :to_mongo :attributes
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/mm_no_empties/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jonathan Chambers"]
|
6
|
+
gem.email = ["j.chambers@gmx.net"]
|
7
|
+
gem.description = %q{MongoMapper plugin that prevents any fields responding to empty? from being persisted if empty}
|
8
|
+
gem.summary = %q{plugin to stop MM persisting empties}
|
9
|
+
gem.homepage = 'https://github.com/jmchambers/mm_no_empties'
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "mm_no_empties"
|
15
|
+
gem.require_paths = ['lib']
|
16
|
+
gem.version = MmNoEmpties::VERSION
|
17
|
+
gem.license = 'MIT'
|
18
|
+
|
19
|
+
gem.add_development_dependency "rspec", "~> 2.7"
|
20
|
+
gem.add_development_dependency "bson_ext", "~> 1.5.0"
|
21
|
+
gem.add_dependency "mongo_mapper", "~> 0.10.1"
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require File.expand_path('../../lib/mm_no_empties', __FILE__)
|
2
|
+
|
3
|
+
describe MmNoEmpties do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
class Group
|
7
|
+
include MongoMapper::Document
|
8
|
+
plugin MmNoEmpties
|
9
|
+
|
10
|
+
many :people, :class_name => 'Person'
|
11
|
+
|
12
|
+
key :names, Array
|
13
|
+
key :counts, Hash
|
14
|
+
key :tags, Set
|
15
|
+
end
|
16
|
+
|
17
|
+
class Person
|
18
|
+
include MongoMapper::EmbeddedDocument
|
19
|
+
plugin MmNoEmpties
|
20
|
+
|
21
|
+
key :name
|
22
|
+
key :age
|
23
|
+
|
24
|
+
belongs_to :group
|
25
|
+
end
|
26
|
+
|
27
|
+
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
|
28
|
+
MongoMapper.database = 'mm_no_empties_test'
|
29
|
+
|
30
|
+
@group = Group.create
|
31
|
+
@person = Person.new(name: 'Jon', age: 33)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should not include empty Arrays, Hashes and Sets in 'attributes'" do
|
35
|
+
@group.attributes.keys.should_not include('names', 'counts', 'tags')
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should not include unused many associations in attributes" do
|
39
|
+
@group.attributes.keys.should_not include('people')
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should restore empty fields when loading from the database" do
|
43
|
+
Group.first.attributes(:include_all).keys.should include('names', 'counts', 'tags')
|
44
|
+
end
|
45
|
+
|
46
|
+
after(:all) do
|
47
|
+
Group.delete_all
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mm_no_empties
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Jonathan Chambers
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-12-23 00:00:00 +00:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 7
|
31
|
+
version: "2.7"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: bson_ext
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 1
|
44
|
+
- 5
|
45
|
+
- 0
|
46
|
+
version: 1.5.0
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: mongo_mapper
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ~>
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
- 10
|
60
|
+
- 1
|
61
|
+
version: 0.10.1
|
62
|
+
type: :runtime
|
63
|
+
version_requirements: *id003
|
64
|
+
description: MongoMapper plugin that prevents any fields responding to empty? from being persisted if empty
|
65
|
+
email:
|
66
|
+
- j.chambers@gmx.net
|
67
|
+
executables: []
|
68
|
+
|
69
|
+
extensions: []
|
70
|
+
|
71
|
+
extra_rdoc_files: []
|
72
|
+
|
73
|
+
files:
|
74
|
+
- .gitignore
|
75
|
+
- Gemfile
|
76
|
+
- MIT-LICENSE
|
77
|
+
- README.md
|
78
|
+
- Rakefile
|
79
|
+
- lib/mm_no_empties.rb
|
80
|
+
- lib/mm_no_empties/version.rb
|
81
|
+
- mm_no_empties.gemspec
|
82
|
+
- spec/mm_no_empties_spec.rb
|
83
|
+
has_rdoc: true
|
84
|
+
homepage: https://github.com/jmchambers/mm_no_empties
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
segments:
|
98
|
+
- 0
|
99
|
+
version: "0"
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
segments:
|
106
|
+
- 0
|
107
|
+
version: "0"
|
108
|
+
requirements: []
|
109
|
+
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 1.3.7
|
112
|
+
signing_key:
|
113
|
+
specification_version: 3
|
114
|
+
summary: plugin to stop MM persisting empties
|
115
|
+
test_files: []
|
116
|
+
|