auto_serializer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/auto_serializer.rb +28 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e87f73d889e85706fded041c7e17474fa904d5d2
4
+ data.tar.gz: 3126fb808b0961148e3ba7256c5fcf29cbbaa1f4
5
+ SHA512:
6
+ metadata.gz: 58c6181e0c268fd6ec5b0b447f20df8e3820d6d159235fe9e9d2299a569693567533e80da019f973f9e367289a80540f9369e345363405a0f09f3598c9255ad7
7
+ data.tar.gz: 7254d5aade9f4629525ec878ca5fabeb126b34b09b58a728fdc3f0281767fa3ed0256ca11c4cc156e3e04cff3a9aa1fe3c3a97d42a72d2a34af0f58708c2615b
@@ -0,0 +1,28 @@
1
+ class AutoSerializer
2
+ # Create file name based on class/method name and its constructor arguments. Try deserialize from this file, otherwise initialize object and serialize to file.
3
+ def self.auto(klass_or_method, *arguments) #TODO methods
4
+ name = create_name(klass_or_method, arguments)
5
+ if File.exist? name
6
+ File.open(name) do |file|
7
+ return Marshal.load(file)
8
+ end
9
+ end
10
+
11
+ if klass_or_method.instance_of? Class
12
+ object = klass_or_method.new(*arguments)
13
+ else
14
+ object = method(klass_or_method).call(*arguments)
15
+ end
16
+ File.open(name, 'w') do |output|
17
+ Marshal.dump(object, output)
18
+ end
19
+ return object
20
+ end
21
+
22
+ private
23
+ # Create file name based on class/method name and its (constructor) arguments.
24
+ def self.create_name(klass_or_method, arguments)
25
+ '%s-%s.marshal' % [klass_or_method.to_s, arguments.map { |argument| argument.to_s.gsub('/', '_')[0..100] }.join(';')]
26
+ end
27
+ end
28
+
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auto_serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Krzysztof Wróbel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Serialize object by class/method name and (constructor) arguments.
14
+ email: djstrong@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/auto_serializer.rb
20
+ homepage: https://github.com/djstrong/auto_serializer
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.4.5
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Serialize object by class name and constructor arguments.
44
+ test_files: []