fast_underscore 0.1.0 → 0.2.0

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
- SHA1:
3
- metadata.gz: 07fbe0b6f67b99fa5786e31ff487a444e1ac649b
4
- data.tar.gz: 467ac00f6bec4ef82f4ba862dff95751e3ad5063
2
+ SHA256:
3
+ metadata.gz: 6365c2190445bc7618c3fe443583205ae7fc08cdb0151811a6d088db7582ca9c
4
+ data.tar.gz: 351dc9c1c0b4c661bc2a909f94dabf4a018eae5cd0a2bb32e2c01916a9387c43
5
5
  SHA512:
6
- metadata.gz: a075571a7c8b3d84ace4442410bf11b3387a19cd1eb85de6207931426f568b48a7ca02eb8bd007a2e5e4238e4eca8b7eac96e38899a121173515e490aa012620
7
- data.tar.gz: 1e87466be79faddedb5f69fa3b554aecb708bb16eaee1e71d7a03b0c1a9fb880456ed3fe39557c05559e9dd5fc9e357edcdacec265777b9b1824cd1bcb8385a4
6
+ metadata.gz: aae5d5f8d9914d5acc70271c307e39b4e57427a7816b278bd3455f0cc1cf66ce928c844a295796beb7827a81642d511ed2ede47188ac75cef4567e398d8a34f3
7
+ data.tar.gz: 8fdd103f0920380ce462ca94b2d6f122a2465e4e793df9448d2c53d04a6f86c09ede6277689acab6958cf235cf02dae9a2a24b9ea1e9fe387ab5d7530ef44c35
data/.rubocop.yml CHANGED
@@ -1,11 +1,14 @@
1
1
  AllCops:
2
2
  DisplayCopNames: true
3
3
  DisplayStyleGuide: true
4
- TargetRubyVersion: 2.4
4
+ TargetRubyVersion: 2.5
5
5
  Exclude:
6
6
  - 'bin/**/*'
7
7
  - 'gemfiles/**/*'
8
8
  - 'tmp/**/*'
9
9
 
10
+ Style/Documentation:
11
+ Enabled: false
12
+
10
13
  Style/PerlBackrefs:
11
14
  Enabled: false
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fast_underscore (0.1.0)
4
+ fast_underscore (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -23,7 +23,7 @@ GEM
23
23
  powerpack (0.1.1)
24
24
  rainbow (3.0.0)
25
25
  rake (12.3.0)
26
- rake-compiler (0.9.9)
26
+ rake-compiler (1.0.4)
27
27
  rake
28
28
  rubocop (0.52.0)
29
29
  parallel (~> 1.10)
@@ -48,8 +48,8 @@ DEPENDENCIES
48
48
  fast_underscore!
49
49
  minitest (~> 5)
50
50
  rake (~> 12)
51
- rake-compiler (~> 0)
51
+ rake-compiler (~> 1)
52
52
  rubocop (~> 0.52)
53
53
 
54
54
  BUNDLED WITH
55
- 1.16.1.pre1
55
+ 1.16.1
data/README.md CHANGED
@@ -11,19 +11,16 @@ Maybe! Run a stack profiler like [`ruby-prof`](https://github.com/ruby-prof/ruby
11
11
 
12
12
  ## Usage with Rails
13
13
 
14
- `ActiveSupport::Inflector#underscore` is actually used significantly throughout the Rails boot process, so it's best to hook into that process early to get the best results. Place the following lines above the `require 'rails'` line in your `application.rb` file:
14
+ `ActiveSupport::Inflector#underscore`, in addition to underscoring the input will additionally take into account known acronyms. Since this can't be done at compile time, `FastUnderscore` will detect when `ActiveSupport` is loaded and take advantage of its knowledge of acronyms while still using the native extension.
15
15
 
16
- ```ruby
17
- require 'active_support'
18
- require 'fast_underscore'
19
- ```
20
-
21
- This will allow Rails to use the faster underscore method while it is booting, which is used for autoloading dependencies, as well as determining table names.
16
+ Since the `#underscore` method is used so much throughout the Rails boot process (for autoloading dependencies, determining table names, determining inverse associations, etc.), it's best to hook into Rails as early as possible. As such, for the best results in your `Gemfile` add `require: false` to the `gem 'fast_underscore'` declaration and add `require 'fast_underscore'` to the bottom of `config/boot.rb`.
22
17
 
23
18
  ## Is it fast?
24
19
 
25
20
  At last check, these were the benchmarks (obtained by running `bin/benchmark`):
26
21
 
22
+ ### Rails 5.1.4
23
+
27
24
  ```
28
25
  Warming up --------------------------------------
29
26
  ActiveSupport 2.000 i/100ms
@@ -37,6 +34,21 @@ Comparison:
37
34
  ActiveSupport: 28.8 i/s - 22.54x slower
38
35
  ```
39
36
 
37
+ ### Rails 5.2.0
38
+
39
+ ```
40
+ Warming up --------------------------------------
41
+ ActiveSupport 5.000 i/100ms
42
+ FastUnderscore 66.000 i/100ms
43
+ Calculating -------------------------------------
44
+ ActiveSupport 50.055 (± 6.0%) i/s - 250.000 in 5.016223s
45
+ FastUnderscore 655.181 (± 2.1%) i/s - 3.300k in 5.038968s
46
+
47
+ Comparison:
48
+ FastUnderscore: 655.2 i/s
49
+ ActiveSupport: 50.1 i/s - 13.09x slower
50
+ ```
51
+
40
52
  ## Installation
41
53
 
42
54
  Add this line to your application's Gemfile:
data/bin/benchmark CHANGED
@@ -12,7 +12,7 @@ words = 500.times.map { Array.new(100) { source.sample }.join }
12
12
 
13
13
  Benchmark.ips do |x|
14
14
  x.report('ActiveSupport') do
15
- words.each { |word| ActiveSupport::Inflector.underscore(word) }
15
+ words.each { |word| ActiveSupport::Inflector.as_underscore(word) }
16
16
  end
17
17
 
18
18
  x.report('FastUnderscore') do
data/bin/console CHANGED
@@ -1,7 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'bundler/setup'
4
- require 'active_support'
5
4
  require 'fast_underscore'
6
5
 
7
6
  require 'irb'
@@ -350,7 +350,7 @@ builder_free(builder_t *builder) {
350
350
  * camelize(underscore('SSLError')) # => "SslError"
351
351
  */
352
352
  static VALUE
353
- rb_str_underscore(VALUE self, VALUE rb_string) {
353
+ str_underscore(VALUE rb_string) {
354
354
  VALUE resultant;
355
355
  rb_encoding *encoding;
356
356
 
@@ -381,6 +381,15 @@ rb_str_underscore(VALUE self, VALUE rb_string) {
381
381
  return resultant;
382
382
  }
383
383
 
384
+ /**
385
+ * A singleton method calls with a string that delegates to `str_underscore` to
386
+ * form an underscored, lowercase form from the expression in the string.
387
+ */
388
+ static VALUE
389
+ rb_str_underscore(VALUE self, VALUE rb_string) {
390
+ return str_underscore(rb_string);
391
+ }
392
+
384
393
  /**
385
394
  * Hook into Ruby and define the `FastUnderscore::underscore`.
386
395
  */
@@ -388,4 +397,5 @@ void
388
397
  Init_fast_underscore(void) {
389
398
  VALUE rb_cFastUnderscore = rb_define_module("FastUnderscore");
390
399
  rb_define_singleton_method(rb_cFastUnderscore, "underscore", rb_str_underscore, 1);
400
+ rb_define_method(rb_cString, "underscore", str_underscore, 0);
391
401
  }
@@ -27,6 +27,6 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency 'bundler', '~> 1'
28
28
  spec.add_development_dependency 'minitest', '~> 5'
29
29
  spec.add_development_dependency 'rake', '~> 12'
30
- spec.add_development_dependency 'rake-compiler', '~> 0'
30
+ spec.add_development_dependency 'rake-compiler', '~> 1'
31
31
  spec.add_development_dependency 'rubocop', '~> 0.52'
32
32
  end
@@ -11,19 +11,18 @@ module FastUnderscore
11
11
  @pattern ||= /(?:(?<=([A-Za-z\d]))|\b)(#{acronym_regex})(?=\b|[^a-z])/
12
12
  end
13
13
 
14
- def underscore
15
- return self unless /[A-Z-]|::/.match?(self)
14
+ def underscore(string)
15
+ return string unless /[A-Z-]|::/.match?(string)
16
16
 
17
- response = dup
17
+ response = string.dup
18
18
  response.gsub!(AcronymRegex.pattern) { "#{$1 && '_'}#{$2.downcase}" }
19
19
 
20
20
  FastUnderscore.underscore(response)
21
21
  end
22
22
  end
23
- end
24
-
25
- String.prepend(FastUnderscore::AcronymRegex)
26
23
 
27
- class << ActiveSupport::Inflector
28
- define_method(:underscore, &FastUnderscore.method(:underscore))
24
+ class << ActiveSupport::Inflector
25
+ alias as_underscore underscore
26
+ prepend AcronymRegex
27
+ end
29
28
  end
@@ -4,10 +4,10 @@ module FastUnderscore
4
4
  # Uses ActiveSupport's `acronym_underscore_regex` method for replacing
5
5
  # acronyms within strings that need to be underscored.
6
6
  module AcronymUnderscoreRegex
7
- def underscore
8
- return self unless /[A-Z-]|::/.match?(self)
7
+ def underscore(string)
8
+ return string unless /[A-Z-]|::/.match?(string)
9
9
 
10
- response = dup
10
+ response = string.dup
11
11
  acronyms = ActiveSupport::Inflector.inflections.acronyms_underscore_regex
12
12
 
13
13
  response.gsub!(acronyms) { "#{$1 && '_'}#{$2.downcase}" }
@@ -15,10 +15,9 @@ module FastUnderscore
15
15
  FastUnderscore.underscore(response)
16
16
  end
17
17
  end
18
- end
19
-
20
- String.prepend(FastUnderscore::AcronymUnderscoreRegex)
21
18
 
22
- class << ActiveSupport::Inflector
23
- define_method(:underscore, &FastUnderscore.method(:underscore))
19
+ class << ActiveSupport::Inflector
20
+ alias as_underscore underscore
21
+ prepend AcronymUnderscoreRegex
22
+ end
24
23
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Provides a C-optimized method for underscoring a string
4
3
  module FastUnderscore
5
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
6
5
  end
@@ -3,10 +3,29 @@
3
3
  require 'fast_underscore/version'
4
4
  require 'fast_underscore/fast_underscore'
5
5
 
6
- if !defined?(ActiveSupport)
7
- require 'fast_underscore/ext/plain_string'
8
- elsif ActiveSupport::VERSION::MAJOR == 5 && ActiveSupport::VERSION::MINOR >= 2
9
- require 'fast_underscore/ext/acronym_underscore_regex'
10
- else
11
- require 'fast_underscore/ext/acronym_regex'
6
+ module FastUnderscore
7
+ # Hooks into ActiveSupport::Inflector and waits for the #underscore method to
8
+ # be defined. When it is, it automatically redefines it by hooking into its
9
+ # acronyms and then still calling the native extension.
10
+ module ActiveSupportHook
11
+ def method_added(method)
12
+ return super if method != :underscore || !defined?(ActiveSupport)
13
+ require 'active_support/version'
14
+
15
+ if ActiveSupport::VERSION::MAJOR == 5 &&
16
+ ActiveSupport::VERSION::MINOR >= 2
17
+ require 'fast_underscore/ext/acronym_underscore_regex'
18
+ else
19
+ require 'fast_underscore/ext/acronym_regex'
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ module ActiveSupport
26
+ module Inflector
27
+ class << self
28
+ prepend FastUnderscore::ActiveSupportHook
29
+ end
30
+ end
12
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_underscore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Deisz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-17 00:00:00.000000000 Z
11
+ date: 2018-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: benchmark-ips
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '1'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: '1'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rubocop
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -146,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  version: '0'
147
147
  requirements: []
148
148
  rubyforge_project:
149
- rubygems_version: 2.6.13
149
+ rubygems_version: 2.7.4
150
150
  signing_key:
151
151
  specification_version: 4
152
152
  summary: Fast String#underscore implementation