safe_intern 1.1.0 → 1.1.1

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.
@@ -1,4 +1,4 @@
1
- script: rake spec
1
+ script: "rake spec && rake test_string"
2
2
 
3
3
  rvm:
4
4
  - 1.9.3
data/History.md CHANGED
@@ -1,3 +1,12 @@
1
+ 1.1.1 (2014-04-05)
2
+ ------------------
3
+ * Fixing bug in patched String class for Ruby 1.9.3
4
+ * Optimizing code in patched String for Ruby >2.0.0
5
+
6
+ 1.1.0 (2014-04-04)
7
+ ------------------
8
+ * Adding support for Ruby 1.9.3
9
+
1
10
  1.0.1 (2014-03-25)
2
11
  ------------------
3
12
  * Fixed Rakefile by adding dependencies
data/README.md CHANGED
@@ -41,6 +41,10 @@ much more efficient than doing something like
41
41
 
42
42
  Symbol.all_symbols.map(&:to_s).include?(untrusted_string)
43
43
 
44
+ This gem is also compatible with Ruby 1.9.3, where it falls back to enumerating
45
+ all Symbols and caching the results. It is slightly more efficient than Ruby
46
+ implementation, but still much slower than the implementation for Ruby 2.0.
47
+
44
48
  With the ability to query for known symbols, `intern` and `to_sym` methods can
45
49
  be patched. There are two possible behaviours when called on unknown string:
46
50
 
@@ -104,7 +108,7 @@ This gem provides implementation of both in `SafeIntern::ExceptionPatch` and
104
108
  trusted_string.intern(:allow_unsafe)
105
109
 
106
110
  ## Requirements
107
- * [Ruby] >= 2.0.0
111
+ * [Ruby] >= 1.9.3
108
112
 
109
113
  ## See also
110
114
 
data/Rakefile CHANGED
@@ -13,6 +13,11 @@ Rake::ExtensionTask.new "symbol_defined"
13
13
  RSpec::Core::RakeTask.new
14
14
  task :spec => :compile
15
15
 
16
+ desc 'Test patched String provided by SafeIntern'
17
+ task :test_string => :compile do
18
+ system 'rspec spec/safe_intern/string_spec_man.rb'
19
+ end
20
+
16
21
  # :rubocop task
17
22
  Rubocop::RakeTask.new
18
23
 
@@ -61,8 +61,4 @@ void Init_symbol_defined()
61
61
  {
62
62
  cSafeIntern = rb_define_module("SafeIntern");
63
63
  rb_define_module_function(cSafeIntern, "symbol_defined?", symbol_defined, 1);
64
-
65
- #ifndef RUBY2
66
- st_init_table_with_size(&cached_symbols, 1000);
67
- #endif
68
64
  }
@@ -6,19 +6,13 @@
6
6
 
7
7
  module SafeIntern
8
8
  module ExceptionPatch
9
- def intern(allow_unsafe = nil)
10
- if allow_unsafe == :allow_unsafe || SafeIntern.symbol_defined?(self)
11
- super()
12
- else
13
- fail UnsafeInternException
14
- end
15
- end
16
-
17
- def to_sym(allow_unsafe = nil)
18
- if allow_unsafe == :allow_unsafe || SafeIntern.symbol_defined?(self)
19
- super()
20
- else
21
- fail UnsafeInternException
9
+ %w(intern to_sym).each do |method|
10
+ define_method(method) do |allow_unsafe = nil|
11
+ if allow_unsafe == :allow_unsafe || SafeIntern.symbol_defined?(self)
12
+ super()
13
+ else
14
+ fail UnsafeInternException
15
+ end
22
16
  end
23
17
  end
24
18
  end
@@ -6,19 +6,13 @@
6
6
 
7
7
  module SafeIntern
8
8
  module NilPatch
9
- def intern(allow_unsafe = nil)
10
- if allow_unsafe == :allow_unsafe || SafeIntern.symbol_defined?(self)
11
- super()
12
- else
13
- nil
14
- end
15
- end
16
-
17
- def to_sym(allow_unsafe = nil)
18
- if allow_unsafe == :allow_unsafe || SafeIntern.symbol_defined?(self)
19
- super()
20
- else
21
- nil
9
+ %w(intern to_sym).each do |method|
10
+ define_method(method) do |allow_unsafe = nil|
11
+ if allow_unsafe == :allow_unsafe || SafeIntern.symbol_defined?(self)
12
+ super()
13
+ else
14
+ nil
15
+ end
22
16
  end
23
17
  end
24
18
  end
@@ -12,5 +12,20 @@ require 'safe_intern'
12
12
  # Usage: require 'safe_intern/string'
13
13
  #
14
14
  class ::String
15
- prepend SafeIntern::ExceptionPatch
15
+ if RUBY_VERSION.start_with?('2')
16
+ prepend SafeIntern::ExceptionPatch
17
+
18
+ else
19
+ old_intern = instance_method(:intern)
20
+
21
+ %w(intern to_sym).each do |method|
22
+ define_method(method) do |allow_unsafe = nil|
23
+ if allow_unsafe == :allow_unsafe || SafeIntern.symbol_defined?(self)
24
+ old_intern.bind(self).call
25
+ else
26
+ fail UnsafeInternException
27
+ end
28
+ end
29
+ end
30
+ end
16
31
  end
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = 'safe_intern'
3
- gem.version = '1.1.0'
4
- gem.date = '2014-03-25'
3
+ gem.version = '1.1.1'
4
+ gem.date = Date.today.to_s
5
5
  gem.summary = 'Safe String#intern'
6
6
  gem.description = 'Safe implementation of String#intern'
7
7
  gem.authors = ["Jan Rusnacko"]
@@ -12,8 +12,8 @@ Gem::Specification.new do |gem|
12
12
  gem.homepage = 'https://github.com/jrusnack/safe_intern'
13
13
  gem.license = 'MIT'
14
14
  gem.cert_chain = ['certs/gem-jrusnack.pem']
15
- gem.add_development_dependency 'rake'
16
- gem.add_development_dependency 'rake-compiler'
17
- gem.add_development_dependency 'rspec'
18
- gem.add_development_dependency 'rubocop'
15
+ gem.add_development_dependency 'rake', '>= 10.0.0'
16
+ gem.add_development_dependency 'rake-compiler', '>= 0.9'
17
+ gem.add_development_dependency 'rspec', '>= 2.14'
18
+ gem.add_development_dependency 'rubocop', '>= 0'
19
19
  end
@@ -8,10 +8,12 @@ require 'spec_helper'
8
8
 
9
9
  describe SafeIntern::ExceptionPatch do
10
10
  context 'intern' do
11
- it_behaves_like 'safe-intern', SafeIntern::ExceptionPatch, :intern
11
+ rnd = rand(100_000).to_s.extend(SafeIntern::ExceptionPatch)
12
+ it_behaves_like 'safe-intern', SafeIntern::ExceptionPatch, :intern, rnd
12
13
  end
13
14
 
14
15
  context 'to_sym' do
15
- it_behaves_like 'safe-intern', SafeIntern::ExceptionPatch, :to_sym
16
+ rnd = rand(100_000).to_s.extend(SafeIntern::ExceptionPatch)
17
+ it_behaves_like 'safe-intern', SafeIntern::ExceptionPatch, :to_sym, rnd
16
18
  end
17
19
  end
@@ -8,10 +8,12 @@ require 'spec_helper'
8
8
 
9
9
  describe SafeIntern::NilPatch do
10
10
  context '#intern' do
11
- it_behaves_like 'safe-intern', SafeIntern::NilPatch, :intern
11
+ rnd = rand(100_000).to_s.extend(SafeIntern::NilPatch)
12
+ it_behaves_like 'safe-intern', SafeIntern::NilPatch, :intern, rnd
12
13
  end
13
14
 
14
15
  context '#to_sym' do
15
- it_behaves_like 'safe-intern', SafeIntern::NilPatch, :to_sym
16
+ rnd = rand(100_000).to_s.extend(SafeIntern::NilPatch)
17
+ it_behaves_like 'safe-intern', SafeIntern::NilPatch, :to_sym, rnd
16
18
  end
17
19
  end
@@ -0,0 +1,37 @@
1
+ # Copyright (C) 2014 Jan Rusnacko
2
+ #
3
+ # This copyrighted material is made available to anyone wishing to use,
4
+ # modify, copy, or redistribute it subject to the terms and conditions of the
5
+ # MIT license.
6
+
7
+ require 'spec_helper'
8
+
9
+ # Run this file manually with:
10
+ #
11
+ # rspec spec/safe_intern/string_spec_man.rb
12
+ #
13
+ # or with rake:
14
+ #
15
+ # rake test_string
16
+ #
17
+ # This cannot be run by rspec along with the rest of the tests, because rspec
18
+ # loads this file before running any tests. Upon loading, require loads patched
19
+ # String class, which makes other testcases fail, as they extend singleton
20
+ # classes of strings, when String class is patched already (so super() calls
21
+ # patched methods of String class).
22
+ #
23
+ # For now, I don`t want to go into forking for just three testcases.
24
+
25
+ describe 'String patched' do
26
+ require 'safe_intern/string'
27
+
28
+ context 'intern' do
29
+ rnd = rand(100_000).to_s
30
+ it_behaves_like 'safe-intern', SafeIntern::ExceptionPatch, :intern, rnd
31
+ end
32
+
33
+ context 'to_sym' do
34
+ rnd = rand(100_000).to_s
35
+ it_behaves_like 'safe-intern', SafeIntern::ExceptionPatch, :to_sym, rnd
36
+ end
37
+ end
@@ -4,7 +4,9 @@
4
4
  # modify, copy, or redistribute it subject to the terms and conditions of the
5
5
  # MIT license.
6
6
 
7
- shared_examples 'safe-intern' do |patch, method|
7
+ # takes patch and method to test. nxstr is string that would result in new
8
+ # symbol defined when intern is called on it
9
+ shared_examples 'safe-intern' do |patch, method, nxstr|
8
10
  it 'should convert to Symbol if it already exists' do
9
11
  'Object'.extend(patch).send(method).should be_eql(:Object)
10
12
  end
@@ -12,18 +14,18 @@ shared_examples 'safe-intern' do |patch, method|
12
14
  it 'should not create new Symbol if it does not already exist' do
13
15
  case patch.name
14
16
  when 'SafeIntern::NilPatch'
15
- 'DoesNotExist'.extend(patch).send(method).should be_nil
17
+ nxstr.send(method).should be_nil
16
18
  when 'SafeIntern::ExceptionPatch'
17
- expect { 'DoesNotExist'.extend(patch).send(method) }.to raise_error
19
+ expect { nxstr.send(method) }.to raise_error
18
20
  else
19
21
  fail
20
22
  end
21
- Symbol.all_symbols.map(&:to_s).should_not include('DoesNotExist')
23
+ Symbol.all_symbols.map(&:to_s).should_not include(nxstr)
22
24
  end
23
25
 
24
26
  it 'should accept :allow_unsafe as optional parameter' do
25
- rnd = rand(100_000).to_s
26
- Symbol.all_symbols.map(&:to_s).should_not include(rnd)
27
- rnd.extend(patch).send(method, :allow_unsafe).should be_eql(rnd.to_sym)
27
+ Symbol.all_symbols.map(&:to_s).should_not include(nxstr)
28
+ nxstr.intern(:allow_unsafe)
29
+ Symbol.all_symbols.map(&:to_s).should include(nxstr)
28
30
  end
29
31
  end
metadata CHANGED
@@ -1,7 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: safe_intern
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Jan Rusnacko
@@ -9,62 +10,70 @@ autorequire:
9
10
  bindir: bin
10
11
  cert_chain:
11
12
  - certs/gem-jrusnack.pem
12
- date: 2014-03-25 00:00:00.000000000 Z
13
+ date: 2014-04-05 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: rake
16
17
  requirement: !ruby/object:Gem::Requirement
18
+ none: false
17
19
  requirements:
18
- - - ">="
20
+ - - ! '>='
19
21
  - !ruby/object:Gem::Version
20
- version: '0'
22
+ version: 10.0.0
21
23
  type: :development
22
24
  prerelease: false
23
25
  version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
24
27
  requirements:
25
- - - ">="
28
+ - - ! '>='
26
29
  - !ruby/object:Gem::Version
27
- version: '0'
30
+ version: 10.0.0
28
31
  - !ruby/object:Gem::Dependency
29
32
  name: rake-compiler
30
33
  requirement: !ruby/object:Gem::Requirement
34
+ none: false
31
35
  requirements:
32
- - - ">="
36
+ - - ! '>='
33
37
  - !ruby/object:Gem::Version
34
- version: '0'
38
+ version: '0.9'
35
39
  type: :development
36
40
  prerelease: false
37
41
  version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
38
43
  requirements:
39
- - - ">="
44
+ - - ! '>='
40
45
  - !ruby/object:Gem::Version
41
- version: '0'
46
+ version: '0.9'
42
47
  - !ruby/object:Gem::Dependency
43
48
  name: rspec
44
49
  requirement: !ruby/object:Gem::Requirement
50
+ none: false
45
51
  requirements:
46
- - - ">="
52
+ - - ! '>='
47
53
  - !ruby/object:Gem::Version
48
- version: '0'
54
+ version: '2.14'
49
55
  type: :development
50
56
  prerelease: false
51
57
  version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
52
59
  requirements:
53
- - - ">="
60
+ - - ! '>='
54
61
  - !ruby/object:Gem::Version
55
- version: '0'
62
+ version: '2.14'
56
63
  - !ruby/object:Gem::Dependency
57
64
  name: rubocop
58
65
  requirement: !ruby/object:Gem::Requirement
66
+ none: false
59
67
  requirements:
60
- - - ">="
68
+ - - ! '>='
61
69
  - !ruby/object:Gem::Version
62
70
  version: '0'
63
71
  type: :development
64
72
  prerelease: false
65
73
  version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
66
75
  requirements:
67
- - - ">="
76
+ - - ! '>='
68
77
  - !ruby/object:Gem::Version
69
78
  version: '0'
70
79
  description: Safe implementation of String#intern
@@ -74,8 +83,8 @@ extensions:
74
83
  - ext/symbol_defined/extconf.rb
75
84
  extra_rdoc_files: []
76
85
  files:
77
- - ".gitignore"
78
- - ".travis.yml"
86
+ - .gitignore
87
+ - .travis.yml
79
88
  - Gemfile
80
89
  - History.md
81
90
  - LICENSE
@@ -92,30 +101,32 @@ files:
92
101
  - safe_intern.gemspec
93
102
  - spec/safe_intern/exception_patch_spec.rb
94
103
  - spec/safe_intern/nil_patch_spec.rb
104
+ - spec/safe_intern/string_spec_man.rb
95
105
  - spec/safe_intern_helper.rb
96
106
  - spec/spec_helper.rb
97
107
  homepage: https://github.com/jrusnack/safe_intern
98
108
  licenses:
99
109
  - MIT
100
- metadata: {}
101
110
  post_install_message:
102
111
  rdoc_options: []
103
112
  require_paths:
104
113
  - lib
105
114
  required_ruby_version: !ruby/object:Gem::Requirement
115
+ none: false
106
116
  requirements:
107
- - - ">="
117
+ - - ! '>='
108
118
  - !ruby/object:Gem::Version
109
119
  version: 1.9.3
110
120
  required_rubygems_version: !ruby/object:Gem::Requirement
121
+ none: false
111
122
  requirements:
112
- - - ">="
123
+ - - ! '>='
113
124
  - !ruby/object:Gem::Version
114
125
  version: '0'
115
126
  requirements: []
116
127
  rubyforge_project:
117
- rubygems_version: 2.2.2
128
+ rubygems_version: 1.8.25
118
129
  signing_key:
119
- specification_version: 4
130
+ specification_version: 3
120
131
  summary: Safe String#intern
121
132
  test_files: []
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: aee18090fdd063ac1c39176b72b319fb5afb83b0
4
- data.tar.gz: 56868cfedd38626f90c81c3fe8a8956b62d343a0
5
- SHA512:
6
- metadata.gz: c6da40ab4b1ee204193716b29277b00660400462761a2c70979387f0f651cee7e98714aaedc754b52980b3d8d0ff5df6f592d3378175e3846da119bb852493cf
7
- data.tar.gz: f38721903a27577dc326ad868c01624d715e99b669b65bde829f30203e301ca5fc74c6ccbe11f74cff94bb59a884291bc734247127fe66f696bc998e14e9a516