mongoid_sync_with_deserialization 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.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 SunshineLibrary
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,20 @@
1
+ mongoid_sync_with_deserialization
2
+ =================================
3
+ 解决JSON同步数据时,不支持Time等类型序列化的问题。
4
+
5
+
6
+ TODO
7
+ ---------------------------------
8
+ 需要修改fields,才能ActiveModel公用
9
+
10
+ 参考
11
+ ---------------------------------
12
+ Store Data type in JSON, MongoDB has a solution. http://markembling.info/2011/07/json-date-time
13
+
14
+ ```text
15
+ MongoDB shell version: 1.8.2
16
+ connecting to: test
17
+ > db.tests.insert({'ts':new Date()})
18
+ > db.tests.find()
19
+ { "_id" : ObjectId("4e1f671d671bd7812369551e"), "ts" : ISODate("2011-07-14T22:01:01.947Z") }
20
+ ```
@@ -0,0 +1,45 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rails'
4
+ require 'mongoid'
5
+
6
+ module ::Mongoid
7
+ module SyncWithDeserialization
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ cattr_accessor :deserialization_parsers
12
+ before_save :deserialization_before_sync
13
+
14
+ def deserialization_before_sync
15
+ Utils.load_parsers self.class
16
+
17
+ self.class.deserialization_parsers.each do |_field, _field_parser|
18
+ _v = self.read_attribute(_field)
19
+ if _v.is_a?(String)
20
+ _v = _field_parser.call(_v) rescue _v
21
+ self.write_attribute(_field, _v)
22
+ end
23
+ Rails.logger.info "#{_field} : #{_field_parser} : #{_v}" if $IS_DEBUG_SyncWithDeserialization
24
+ end
25
+ end
26
+ end
27
+
28
+
29
+ module Utils
30
+ def self.load_parsers klass
31
+ return false if not klass.deserialization_parsers.blank?
32
+ klass.deserialization_parsers ||= {}
33
+
34
+ klass.fields.each do |_field_k, _field_v|
35
+ # Add more data parsers
36
+ if [Time, DateTime].include? _field_v.type
37
+ klass.deserialization_parsers[_field_k] = proc {|v| Time.zone.parse(v) }
38
+ end
39
+ end
40
+
41
+ end
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'mongoid_sync_with_deserialization'
3
+ s.version = '0.0.1'
4
+ s.date = '2013-12-05'
5
+ s.summary = File.read("README.markdown").split(/===+/)[1].strip.split("\n")[0]
6
+ s.description = s.summary
7
+ s.authors = ["David Chen"]
8
+ s.email = 'mvjome@gmail.com'
9
+ s.homepage = 'https://github.com/SunshineLibrary/mongoid_sync_with_deserialization/'
10
+ s.license = 'MIT'
11
+
12
+ s.add_dependency "mongoid"
13
+ s.add_dependency "activesupport", "> 3.2"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_sync_with_deserialization
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Chen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>'
36
+ - !ruby/object:Gem::Version
37
+ version: '3.2'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>'
44
+ - !ruby/object:Gem::Version
45
+ version: '3.2'
46
+ description: 解决JSON同步数据时,不支持Time等类型序列化的问题。
47
+ email: mvjome@gmail.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - .gitignore
53
+ - LICENSE
54
+ - README.markdown
55
+ - lib/mongoid_sync_with_deserialization.rb
56
+ - mongoid_sync_with_deserialization.gemspec
57
+ homepage: https://github.com/SunshineLibrary/mongoid_sync_with_deserialization/
58
+ licenses:
59
+ - MIT
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 1.8.23
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: 解决JSON同步数据时,不支持Time等类型序列化的问题。
82
+ test_files: []