win32_service_manager 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest.txt +2 -0
- data/Rakefile +3 -0
- data/lib/win32_service_manager.rb +1 -1
- data/lib/win32_service_manager/core_ext/struct_to_hash.rb +108 -0
- data/spec/.bacon +0 -0
- metadata +3 -1
data/Manifest.txt
CHANGED
data/Rakefile
CHANGED
@@ -11,3 +11,6 @@ PROJ.email = 'raggi@rubyforge.org'
|
|
11
11
|
PROJ.url = 'http://github.com/raggi/win32_service_manager'
|
12
12
|
PROJ.rubyforge.name = 'libraggi'
|
13
13
|
PROJ.version = Win32ServiceManager.version
|
14
|
+
|
15
|
+
desc 'cleanup and rebuild everything'
|
16
|
+
task :full => %w(clean clobber manifest:create gem:spec gem doc)
|
@@ -0,0 +1,108 @@
|
|
1
|
+
class Struct
|
2
|
+
module ToHash
|
3
|
+
module Ruby187
|
4
|
+
def to_hash
|
5
|
+
hashable!
|
6
|
+
Hash[each_pair.to_a]
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module RubyOld
|
11
|
+
def to_hash
|
12
|
+
hashable!
|
13
|
+
vals = []
|
14
|
+
each_pair{|k,v| vals << k; vals << v}
|
15
|
+
Hash[*vals]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
if RUBY_VERSION >= '1.8.7'
|
20
|
+
include Ruby187
|
21
|
+
else
|
22
|
+
include RubyOld
|
23
|
+
end
|
24
|
+
|
25
|
+
def hashable?
|
26
|
+
self.class.members.uniq == self.class.members
|
27
|
+
end
|
28
|
+
|
29
|
+
def hashable!
|
30
|
+
raise ArgumentError, "#{self.inspect} has duplicate keys" unless hashable?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
include ToHash
|
35
|
+
end
|
36
|
+
|
37
|
+
if __FILE__ == $0
|
38
|
+
|
39
|
+
require "benchmark"
|
40
|
+
|
41
|
+
TESTS = 10_000
|
42
|
+
Benchmark.bmbm do |results|
|
43
|
+
ABC = Struct.new(:a,:b,:c)
|
44
|
+
if RUBY_VERSION >= '1.8.7'
|
45
|
+
a = ABC.new(1,2,3)
|
46
|
+
a.extend Struct::ToHash::Ruby187
|
47
|
+
results.report("new:") { TESTS.times { a.to_hash } }
|
48
|
+
end
|
49
|
+
b = ABC.new(1,2,3)
|
50
|
+
b.extend Struct::ToHash::RubyOld
|
51
|
+
results.report("old:") { TESTS.times { b.to_hash } }
|
52
|
+
end
|
53
|
+
|
54
|
+
puts
|
55
|
+
|
56
|
+
begin; require 'rubygems'; rescue LoadError; end
|
57
|
+
require 'bacon'
|
58
|
+
Bacon.summary_on_exit
|
59
|
+
|
60
|
+
|
61
|
+
describe "Struct#to_hash" do
|
62
|
+
should 'not raise an error when calling to_hash on a new struct' do
|
63
|
+
should.not.raise do
|
64
|
+
Struct.new(:a).new(1).to_hash
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "Struct#hashable?" do
|
70
|
+
should 'return true if #to_hash will not raise' do
|
71
|
+
Struct.new(:a).new(1).should.be.hashable
|
72
|
+
end
|
73
|
+
should 'return false if there are duplicate keys' do
|
74
|
+
Struct.new(:a, :a).new(1, 2).should.not.be.hashable
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "Struct#hashable!" do
|
79
|
+
should 'raise an ArgumentError when there are duplicate keys' do
|
80
|
+
should.raise(ArgumentError) do
|
81
|
+
Struct.new(:a, :a).new(1, 2).hashable!
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
supported_versions = [Struct::ToHash::RubyOld]
|
87
|
+
supported_versions << Struct::ToHash::Ruby187 if RUBY_VERSION >= '1.8.7'
|
88
|
+
supported_versions.each do |mod|
|
89
|
+
describe mod do
|
90
|
+
should 'raise an argument error when the struct has duplicate keys' do
|
91
|
+
should.raise(ArgumentError) do
|
92
|
+
s = Struct.new(:a, :a).new(1,2)
|
93
|
+
s.extend mod
|
94
|
+
s.to_hash
|
95
|
+
end
|
96
|
+
end
|
97
|
+
should 'return a hash with keys associated with their values' do
|
98
|
+
keys = %w(a b c d e).map { |k| k.to_sym }
|
99
|
+
values = [1,2,3,4,5]
|
100
|
+
result = Hash[*keys.zip(values).flatten]
|
101
|
+
s = Struct.new(*keys).new(*values)
|
102
|
+
s.extend mod
|
103
|
+
s.to_hash.should.== result
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
data/spec/.bacon
ADDED
File without changes
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: win32_service_manager
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Tucker
|
@@ -59,6 +59,8 @@ files:
|
|
59
59
|
- Rakefile
|
60
60
|
- bin/win32_service_manager
|
61
61
|
- lib/win32_service_manager.rb
|
62
|
+
- lib/win32_service_manager/core_ext/struct_to_hash.rb
|
63
|
+
- spec/.bacon
|
62
64
|
- spec/helper.rb
|
63
65
|
- spec/runner
|
64
66
|
- spec/spec_win32_service_manager.rb
|