stores_in_mongo 0.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f358c17127a0af455506fbbcd4b0174bb8103462
4
+ data.tar.gz: 07aa5ad59c2f3cf4dc1a2cf840ca7d592bfbb2c1
5
+ SHA512:
6
+ metadata.gz: b6e0c68b15a11c835fa0d007b2ca754c44daa719689dbd05e59db9fa9297f13dc1d0d309f2431be5e1c3ce3f10110d89c7d409e4b90f20ce2c1fb95129716406
7
+ data.tar.gz: 31134763f3bf829ebcee4a8274f780d04e3394848ac9a0ced4d504924b645d6401c6cfd0ad67f6880fe02ce715b1b2059515ca73c02ed533bacc7dedde2e834e
@@ -0,0 +1,26 @@
1
+ module StoresInMongo
2
+ module Base
3
+ def stores_in_mongo(field)
4
+ class_attribute :mongo_data_field
5
+ self.mongo_data_field = field.to_s
6
+ include StoresInMongo::DocumentMethods
7
+
8
+ before_save :save_document
9
+ before_destroy :destroy_document
10
+
11
+ klass = const_set("MongoDocument", Class.new)
12
+ klass.include Mongoid::Document
13
+ klass.include Mongoid::Timestamps
14
+ klass.field self.mongo_data_field.to_sym, :type => Hash, default: {}
15
+
16
+ # TODO: allow customization `document` name
17
+ define_method(self.mongo_data_field) do |reload = false|
18
+ document(reload)[self.mongo_data_field]
19
+ end
20
+
21
+ define_method(self.mongo_data_field + "=") do |data|
22
+ document[self.mongo_data_field] = data
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,45 @@
1
+ module StoresInMongo
2
+ module DocumentMethods
3
+ def reload
4
+ super
5
+ find_or_initialize_document if document_loaded?
6
+ return self
7
+ end
8
+
9
+ def dup
10
+ copy = super
11
+ copy.document = document.dup if document_loaded?
12
+ return copy
13
+ end
14
+
15
+ private
16
+
17
+ def document_loaded?
18
+ @document.present?
19
+ end
20
+
21
+ def document(reload = false)
22
+ return @document if !reload && document_loaded?
23
+ @document = find_or_initialize_document
24
+ end
25
+
26
+ def save_document
27
+ return true if document.nil?
28
+ document.save
29
+ self.document_id = document.id
30
+ end
31
+
32
+ def destroy_document
33
+ return true if document.nil?
34
+ document.destroy
35
+ end
36
+
37
+ def find_or_initialize_document
38
+ @document = fetch_document || self.class::MongoDocument.new
39
+ end
40
+
41
+ def fetch_document
42
+ self.class::MongoDocument.where(id: self.document_id).first
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,4 @@
1
+ require "base.rb"
2
+ require "document_methods.rb"
3
+
4
+ ActiveRecord::Base.extend(StoresInMongo::Base)
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stores_in_mongo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Schwartz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Use stores_in_mongo <field> in an ActiveRecord object, use it as if it
14
+ were local. Includes ActiveRecord-like caching behavior.
15
+ email: ozydingo@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/base.rb
21
+ - lib/document_methods.rb
22
+ - lib/stores_in_mongo.rb
23
+ homepage: https://github.com/ozydingo/stores_in_mongo
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.4.5
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Seamlessly access and store mongo document fields from an ActiveRecord object
47
+ test_files: []