libis-tools 0.9.56 → 0.9.57

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 97d3c16d53f3b37947752b8efa13d313265c569b
4
- data.tar.gz: 7fab21f7cad512ed195ea01937fb60d0b5b5b8af
3
+ metadata.gz: f41609527ad591aacb554d78427bd483c72c3e98
4
+ data.tar.gz: d8c7b2d1a9df6ed014844cad0d4789e4b0a8da91
5
5
  SHA512:
6
- metadata.gz: e1543c8b2515dac9c1c8704e6453e972638233c771dcddcdc0443aaf739c1441dfd80e56b918c9c520667af1e294f9e8c836de71dd9d0ceff7ed4fa81a051839
7
- data.tar.gz: 4935cdd72ec551e872ca26d7c6036c98524d34b4149296ac7572c484a034537fcce0901477d6f4199307996620a379d2d311bca3fe2857e2766355cc3a9027aa
6
+ metadata.gz: e9ac55598115c0ded4f4212a2a27b21cc2809ad56a98bd1e3d0ba933845d9d9e372cf0e1f52744419525f36f929a8b4c721b9504f7e467c4d66da9744e822e4e
7
+ data.tar.gz: 9d69b719ca4432dad3b7709ec872ad949d720f21030434a8959077c05b966ae64fc1126c454067d317bcb7d25f0b7a9a9fe4beb1f8a80e4aef86d75be655471a
data/README.md CHANGED
@@ -99,7 +99,34 @@ The code will strip namespaces from the input in order to greatly simplify worki
99
99
  The class {::Libis::Tools::Parameter} and the {::Libis::Tools::ParameterContainer} module provide a simple framework for
100
100
  instance variables that are type-safe and can easily be documented and provide defaults.
101
101
 
102
- ### {::Libis::Tools::XmlDocument}
102
+ ## {::Libis::Tools::TempFile}
103
+
104
+ A small and simple module that provides some convenience methods to deal with temp files. Random file names are generated
105
+ in a similar way as the standard Ruby Tempfile class does. It has the form:
106
+ ```
107
+ <Optional prefix with '_' appended>_<YYYYMMDD>_<process id>_<random base36 number><optional suffix>
108
+ ```
109
+
110
+ The #name method creates a random file name. Optional parameters are the prefix and suffix (including '.' character if
111
+ needed) for the temp file name and the directory part of the file path. Without directory option the file path will be
112
+ located in the standard folder for temporary files (e.g. /tmp on Linux).
113
+
114
+ The #file method creates a random file name as above, but immediately opens it for writing. If a block is given, the open
115
+ file pointer (IO object) will be passed as argument to the block and the file will automatically be closed and deleted
116
+ when the block ends. In that case the return value will be whatever the block returns.
117
+
118
+ Without a block, the method still creates and opens the file, but it will return the open file pointer to the caller. The
119
+ caller is responsible to #close and #unlink or #delete the file. The #unlink and #delete methods are injected into the
120
+ returned IO object for your convenience, but it calling the corresponding File methods instead is equally valid.
121
+
122
+ ## {::Libis::Tools::ThreadSafe}
123
+
124
+ A convenience method that embeds the mutex implementation. Just include this module whenever you need a thread-save
125
+ implementation and use the mutex instance variable without any concerns regarding initialization. Your class will have
126
+ access to an instance variable 'mutex' as well as a class variable 'class_mutex'. The mutexes (Montor instance) are created
127
+ in a thread-safe way.
128
+
129
+ ## {::Libis::Tools::XmlDocument}
103
130
 
104
131
  Class that embodies most used features of Nokogiri, Nori and Gyoku in one convenience class. The Nokogiri document is
105
132
  stored in the class variable 'document' and can be accessed and manipulated directly - if required. The class supports
data/lib/libis/tools.rb CHANGED
@@ -13,6 +13,7 @@ module Libis
13
13
  autoload :MetsFile, 'libis/tools/mets_file'
14
14
  autoload :Parameter, 'libis/tools/parameter'
15
15
  autoload :Spreadsheet, 'libis/tools/spreadsheet'
16
+ autoload :TempFile, 'libis/tools/temp_file'
16
17
  autoload :ThreadSafe, 'libis/tools/thread_safe'
17
18
  autoload :XmlDocument, 'libis/tools/xml_document'
18
19
 
@@ -0,0 +1,42 @@
1
+ require 'libis/tools/extend/empty'
2
+
3
+ module Libis
4
+ module Tools
5
+
6
+ module TempFile
7
+
8
+ def self.dir
9
+ Dir.tmpdir
10
+ end
11
+
12
+ def self.name(prefix = '', suffix = '', _dir = nil)
13
+ _dir ||= dir
14
+ t = Time.now.strftime('%Y%m%d')
15
+ t = '_' + t unless prefix.empty?
16
+ File.join(_dir, "#{prefix}#{t}_#{$$}_#{rand(0x100000000).to_s(36)}#{suffix}".freeze)
17
+ end
18
+
19
+ def self.file(prefix = '', suffix = '', dir = nil)
20
+ f = File.open(name(prefix, suffix, dir), 'w')
21
+
22
+ def f.unlink
23
+ File.unlink self
24
+ end
25
+
26
+ def f.delete
27
+ File.delete self
28
+ end
29
+
30
+ if block_given?
31
+ x = yield(f)
32
+ f.close
33
+ f.delete
34
+ return x
35
+ else
36
+ return f
37
+ end
38
+ end
39
+ end
40
+
41
+ end
42
+ end
@@ -1,5 +1,5 @@
1
1
  module Libis
2
2
  module Tools
3
- VERSION = '0.9.56'
3
+ VERSION = '0.9.57'
4
4
  end
5
5
  end
data/libis-tools.gemspec CHANGED
@@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency 'bundler', '~> 1.6'
32
32
  spec.add_development_dependency 'rake', '~> 10.3'
33
33
  spec.add_development_dependency 'rspec', '~> 3.1'
34
- spec.add_development_dependency 'simplecov', '~> 0.9'
34
+ spec.add_development_dependency 'term-ansicolor', '~> 1.6'
35
35
  spec.add_development_dependency 'equivalent-xml', '~> 0.5'
36
36
  spec.add_development_dependency 'awesome_print', '~> 1.6'
37
37
 
@@ -138,7 +138,7 @@ STR
138
138
  let(:xml) { Libis::Tools::XmlDocument.open(File.join(File.dirname(__FILE__), '123456789.marc')) }
139
139
 
140
140
  it 'load from xml' do
141
- puts record.marc_dump
141
+ # puts record.marc_dump
142
142
  expect(record.marc_dump).to eq <<-STR.align_left
143
143
  LDR:'01068nam 2200241u 4500'
144
144
  005:'20150701153710.0'
@@ -167,7 +167,7 @@ STR
167
167
  <dc:language>la</dc:language>
168
168
  </dc:record>
169
169
  STR
170
- puts puts record.to_dc.to_xml
170
+ # puts puts record.to_dc.to_xml
171
171
  record.to_dc.root.elements.each_with_index do |element, i|
172
172
  expect(element).to be_equivalent_to(xml_doc.root.elements[i])
173
173
  end
@@ -0,0 +1,76 @@
1
+ # encoding: utf-8
2
+ require_relative 'spec_helper'
3
+ require 'libis/tools/temp_file'
4
+
5
+ describe 'TempFile' do
6
+
7
+ context 'name' do
8
+
9
+ it 'without arguments' do
10
+ fname = Libis::Tools::TempFile.name()
11
+ expect(File.basename(fname)).to match(/^\d{8}_\d+_\w+$/)
12
+ end
13
+
14
+ it 'with prefix' do
15
+ fname = Libis::Tools::TempFile.name('abc')
16
+ expect(File.basename(fname)).to match(/^abc_\d{8}_\d+_\w+$/)
17
+ end
18
+
19
+ it 'with suffix' do
20
+ fname = Libis::Tools::TempFile.name(nil, '.xyz')
21
+ expect(File.basename(fname)).to match(/^\d{8}_\d+_\w+\.xyz$/)
22
+ end
23
+
24
+ it 'with prefix and suffix' do
25
+ fname = Libis::Tools::TempFile.name('abc', '.xyz')
26
+ expect(File.basename(fname)).to match(/^abc_\d{8}_\d+_\w+\.xyz$/)
27
+ end
28
+
29
+ it 'not in temp dir' do
30
+ fname = Libis::Tools::TempFile.name(nil, nil, '/abc/xyz/')
31
+ expect(File.dirname(fname)).to match('/abc/xyz')
32
+ end
33
+
34
+ end
35
+
36
+ context 'file' do
37
+
38
+ it 'created' do
39
+ f = Libis::Tools::TempFile.file
40
+ expect(File.exists?(f.path)).to be_truthy
41
+ f.close
42
+ f.delete
43
+ end
44
+
45
+ it 'is open' do
46
+ f = Libis::Tools::TempFile.file
47
+ expect(f.closed?).to be_falsey
48
+ f.close
49
+ f.delete
50
+ end
51
+
52
+ it 'can be closed' do
53
+ f = Libis::Tools::TempFile.file
54
+ expect(f.closed?).to be_falsey
55
+ f.close
56
+ expect(f.closed?).to be_truthy
57
+ f.delete
58
+ end
59
+
60
+ it 'can be unlinked' do
61
+ f = Libis::Tools::TempFile.file
62
+ f.close
63
+ f.unlink
64
+ expect(File.exists?(f.path)).to be_falsey
65
+ end
66
+
67
+ it 'can be deleted' do
68
+ f = Libis::Tools::TempFile.file
69
+ f.close
70
+ f.delete
71
+ expect(File.exists?(f.path)).to be_falsey
72
+ end
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,64 @@
1
+ require_relative 'spec_helper'
2
+ require 'awesome_print'
3
+ require 'libis/tools/thread_safe'
4
+
5
+ describe 'ThreadSafe' do
6
+
7
+ class Foo
8
+ include Libis::Tools::ThreadSafe
9
+
10
+ def self.bar
11
+ self.class_mutex.synchronize {
12
+ @bar ||= get_number
13
+ }
14
+ end
15
+
16
+ def self.get_number
17
+ nr = rand(1000000)
18
+ sleep 1
19
+ nr
20
+ end
21
+ end
22
+
23
+ it 'protects class variable' do
24
+ mutex = Monitor.new
25
+ result = []
26
+ 1000.times.map do
27
+ Thread.new do
28
+ a = Foo.bar
29
+ mutex.synchronize {result << a}
30
+ end
31
+ end.each(&:join)
32
+ result.uniq!
33
+ # ap result
34
+ expect(result.size).to be 1
35
+ end
36
+
37
+ class Bar
38
+
39
+ def self.bar
40
+ @bar ||= get_number
41
+ end
42
+
43
+ def self.get_number
44
+ nr = rand(1000000)
45
+ sleep 1
46
+ nr
47
+ end
48
+ end
49
+
50
+ it 'without not thread safe' do
51
+ mutex = Monitor.new
52
+ result = []
53
+ 1000.times.map do
54
+ Thread.new do
55
+ a = Bar.bar
56
+ mutex.synchronize {result << a}
57
+ end
58
+ end.each(&:join)
59
+ result.uniq!
60
+ # ap result
61
+ expect(result.size).not_to be 1
62
+ end
63
+
64
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libis-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.56
4
+ version: 0.9.57
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kris Dekeyser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-09 00:00:00.000000000 Z
11
+ date: 2017-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,19 +53,19 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.1'
55
55
  - !ruby/object:Gem::Dependency
56
- name: simplecov
56
+ name: term-ansicolor
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.9'
61
+ version: '1.6'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.9'
68
+ version: '1.6'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: equivalent-xml
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -320,6 +320,7 @@ files:
320
320
  - lib/libis/tools/mets_objects.rb
321
321
  - lib/libis/tools/parameter.rb
322
322
  - lib/libis/tools/spreadsheet.rb
323
+ - lib/libis/tools/temp_file.rb
323
324
  - lib/libis/tools/thread_safe.rb
324
325
  - lib/libis/tools/version.rb
325
326
  - lib/libis/tools/xml_document.rb
@@ -358,7 +359,9 @@ files:
358
359
  - spec/parameter_spec.rb
359
360
  - spec/spec_helper.rb
360
361
  - spec/spreadsheet_spec.rb
362
+ - spec/temp_file_spec.rb
361
363
  - spec/test.xsd
364
+ - spec/thread_safe_spec.rb
362
365
  - spec/xmldocument_spec.rb
363
366
  - test.rb
364
367
  - test/test_helper.rb
@@ -423,7 +426,9 @@ test_files:
423
426
  - spec/parameter_spec.rb
424
427
  - spec/spec_helper.rb
425
428
  - spec/spreadsheet_spec.rb
429
+ - spec/temp_file_spec.rb
426
430
  - spec/test.xsd
431
+ - spec/thread_safe_spec.rb
427
432
  - spec/xmldocument_spec.rb
428
433
  - test/test_helper.rb
429
434
  - test/webservices/test_ca_item_info.rb