dumb_serializer 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e4666d774b35a854ec83e83657e5458f84b058a3
4
+ data.tar.gz: 8e4128eb1cdee441390a82d5a243b0798f099122
5
+ SHA512:
6
+ metadata.gz: d2fd3e0d019d2797978ece0699d2f6e84bed959c16563c1ba62b40bc5736a8f4694d85d857c669e9c4978d35d986219c9846f41b12b74fa3ffe43efad27ed871
7
+ data.tar.gz: 451ddf1401e592c034fd01bf6ce6773b9cfea0ae713a55f02fee363358f245b7d74e08e09ab83451e8b7b62ab0896b7b820ab7bae24caa0ac35583a6acf400e4
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ current_path = File.expand_path(File.dirname(__FILE__))
3
+ Dir["#{current_path}/dumb_serializer/*.rb"].each { |file| require_relative file }
4
+
5
+ # Module to supply class objects with mongodb storage.
6
+ module DumbSerializer
7
+ CORE_TYPES =
8
+ [Fixnum, Bignum, String, TrueClass, FalseClass, Array, Hash, NilClass].freeze
9
+
10
+ def self.included(klass)
11
+ klass.extend Destroyable
12
+ klass.extend Loadable
13
+ klass.extend Configurable
14
+ klass.include Serializable
15
+ end
16
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+ module DumbSerializer
3
+ class AttrsScanner
4
+ class CoreTypeScanError < StandardError; end
5
+
6
+ def initialize(object)
7
+ @object = object
8
+ end
9
+
10
+ def scan
11
+ raise CoreTypeScanError if belongs_to_core?
12
+ recursive_build
13
+ end
14
+
15
+ private
16
+
17
+ def object_attrs
18
+ object.instance_variables.each_with_object({}) do |attr, hash|
19
+ hash[attr.to_s.delete('@').to_sym] = object.instance_variable_get(attr)
20
+ end
21
+ end
22
+
23
+ def decompose(attrs)
24
+ attrs.each do |name, value|
25
+ self.object = value
26
+
27
+ next if belongs_to_core?
28
+ attrs[name] = nested? ? recursive_build : non_variable_instance
29
+ end
30
+ end
31
+
32
+ def recursive_build
33
+ { dd_class: object_class, dd_vars: decompose(object_attrs) }
34
+ end
35
+
36
+ def non_variable_instance
37
+ { dd_class: object_class }
38
+ end
39
+
40
+ def object_class
41
+ object.class.to_s
42
+ end
43
+
44
+ def belongs_to_core?
45
+ CORE_TYPES.include?(object.class)
46
+ end
47
+
48
+ def nested?
49
+ object.instance_variables.any?
50
+ end
51
+
52
+ attr_accessor :object
53
+ end
54
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ module DumbSerializer
3
+ module Destroyable
4
+ def remove(id)
5
+ MongoConnector.client[collection_name].find(_id: BSON::ObjectId(id)).delete_one
6
+ end
7
+
8
+ def drop
9
+ MongoConnector.client[collection_name].drop
10
+ end
11
+
12
+ private
13
+
14
+ def collection_name
15
+ class_name.downcase
16
+ end
17
+
18
+ def class_name
19
+ to_s
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+ module DumbSerializer
3
+ module Loadable
4
+ def find(id)
5
+ doc = find_in_db(id) # handle error on no result!
6
+ doc_to_object(doc)
7
+ end
8
+
9
+ def all
10
+ docs = MongoConnector.client[collection_name].find.to_a
11
+ docs.each_with_object([]) do |doc, array|
12
+ array << doc_to_object(doc)
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def find_in_db(id)
19
+ collection = MongoConnector.client[collection_name]
20
+ collection.find(_id: BSON::ObjectId(id)).to_a.first
21
+ end
22
+
23
+ def doc_to_object(doc)
24
+ build(doc)
25
+ end
26
+
27
+ def build(partial)
28
+ return partial if CORE_TYPES.include?(partial.class)
29
+ return partial.to_h if hash?(partial)
30
+ partial['dd_vars'].each { |name, value| partial['dd_vars'][name] = build(value) }
31
+ klass = class_get(partial['dd_class'])
32
+ build_partial(klass, partial['dd_vars'])
33
+ end
34
+
35
+ def build_partial(klass, vars)
36
+ vars.each_with_object(klass.new) do |(key, value), partial|
37
+ partial.instance_variable_set("@#{key}".to_sym, value)
38
+ end
39
+ end
40
+
41
+ def hash?(object)
42
+ object.instance_of?(BSON::Document) && !object['dd_class']
43
+ end
44
+
45
+ def class_get(str)
46
+ Object.const_get(str)
47
+ end
48
+
49
+ def collection_name
50
+ to_s
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+ require 'mongo'
3
+
4
+ module DumbSerializer
5
+ module MongoConnector
6
+ DEFAULT_OPTIONS = {
7
+ database: 'dumb_serializer',
8
+ host: '127.0.0.1',
9
+ port: '27017'
10
+ }
11
+
12
+ class << self
13
+ attr_reader :db_config
14
+
15
+ def client
16
+ @client || client_setup
17
+ end
18
+
19
+ def db_config=(options)
20
+ @db_config ||= DEFAULT_OPTIONS
21
+ @db_config.merge!(options)
22
+ @client&.close
23
+ @client = Mongo::Client.new(connection_url(@db_config))
24
+ end
25
+
26
+ private
27
+
28
+ def client_setup
29
+ @client = Mongo::Client.new(connection_url(DEFAULT_OPTIONS))
30
+ end
31
+
32
+ def connection_url(options)
33
+ 'mongodb://' + options[:host] + ':' + options[:port].to_s + '/' + options[:database]
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+ module DumbSerializer
3
+ module Serializable
4
+ def serialize
5
+ hashed = attrs_scanner.scan
6
+ collection = MongoConnector.client[collection_name]
7
+ collection.insert_one(hashed)
8
+ end
9
+
10
+ private
11
+
12
+ def collection_name
13
+ included_class.to_s
14
+ end
15
+
16
+ def included_class
17
+ self.class
18
+ end
19
+
20
+ def attrs_scanner
21
+ AttrsScanner.new(self)
22
+ end
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dumb_serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Robert Pawlas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Mongo mapper for ruby objects.
14
+ email: robspawlas@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/dumb_serializer.rb
20
+ - lib/dumb_serializer/attrs_scanner.rb
21
+ - lib/dumb_serializer/destroyable.rb
22
+ - lib/dumb_serializer/loadable.rb
23
+ - lib/dumb_serializer/mongo_connector.rb
24
+ - lib/dumb_serializer/serializable.rb
25
+ homepage: ''
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.6.8
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: DumbSerializer
49
+ test_files: []
50
+ has_rdoc: