to_regexp 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,5 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ doc/
6
+ .yardoc/
data/CHANGELOG ADDED
@@ -0,0 +1,6 @@
1
+ 0.1.2 / 2013-02-13
2
+
3
+ * Enhancements
4
+
5
+ * Start keeping CHANGELOG!
6
+ * add :detect option
data/Gemfile CHANGED
@@ -1,7 +1,4 @@
1
- source "http://rubygems.org"
1
+ source :rubygems
2
2
 
3
3
  # Specify your gem's dependencies in to_regexp.gemspec
4
4
  gemspec
5
-
6
- # development dependencies
7
- gem 'ensure-encoding'
data/README.rdoc CHANGED
@@ -11,7 +11,7 @@ You can also treat strings as literal regexps. These two are equivalent:
11
11
 
12
12
  '/foo/'.to_regexp #=> /foo/
13
13
  'foo'.to_regexp(:literal => true) #=> /foo/
14
-
14
+
15
15
  If you need case insensitivity and you're using <tt>:literal</tt>, pass options like <tt>:ignore_case</tt>. These two are equivalent:
16
16
 
17
17
  '/foo/i'.to_regexp #=> /foo/i
@@ -21,4 +21,11 @@ You can get the options passed to <tt>Regexp.new</tt> with <tt>#as_regexp</tt>:
21
21
 
22
22
  '/foo/'.to_regexp == Regexp.new('/foo/'.as_regexp) # true
23
23
 
24
+ Finally, you can be more lazy using <tt>:detect</tt>:
25
+
26
+ 'foo'.to_regexp(detect: true) #=> /foo/
27
+ 'foo\b'.to_regexp(detect: true) #=> %r{foo\\b}
28
+ '/foo\b/'.to_regexp(detect: true) #=> %r{foo\b}
29
+ 'foo\b/'.to_regexp(detect: true) #=> %r{foo\\b/}
30
+
24
31
  Copyright 2012 Seamus Abshere
data/Rakefile CHANGED
@@ -11,15 +11,7 @@ end
11
11
 
12
12
  task :default => :test
13
13
 
14
- begin
15
- require 'rake/rdoctask'
16
- Rake::RDocTask.new do |rdoc|
17
- rdoc.rdoc_dir = 'rdoc'
18
- rdoc.title = 'remote_table'
19
- rdoc.options << '--line-numbers' << '--inline-source'
20
- rdoc.rdoc_files.include('README*')
21
- rdoc.rdoc_files.include('lib/**/*.rb')
22
- end
23
- rescue LoadError
24
- puts "Rdoc is not available"
14
+ require 'yard'
15
+ YARD::Rake::YardocTask.new do |y|
16
+ y.options << '--no-private'
25
17
  end
data/lib/to_regexp.rb CHANGED
@@ -12,19 +12,32 @@ module ToRegexp
12
12
  '/' => '/',
13
13
  }
14
14
 
15
+ # Get a regexp back
16
+ #
17
+ # Without :literal or :detect, `"foo".to_regexp` will return nil.
18
+ #
19
+ # @param [optional, Hash] options
20
+ # @option options [true,false] :literal Treat meta characters and other regexp codes as just text; always return a regexp
21
+ # @option options [true,false] :detect If string starts and ends with valid regexp delimiters, treat it as a regexp; otherwise, interpret it literally
22
+ # @option options [true,false] :ignore_case /foo/i
23
+ # @option options [true,false] :multiline /foo/m
24
+ # @option options [true,false] :extended /foo/x
25
+ # @option options [true,false] :lang /foo/[nesu]
15
26
  def to_regexp(options = {})
16
27
  if args = as_regexp(options)
17
28
  ::Regexp.new *args
18
29
  end
19
30
  end
20
-
31
+
32
+ # Return arguments that can be passed to `Regexp.new`
33
+ # @see to_regexp
21
34
  def as_regexp(options = {})
22
35
  unless options.is_a?(::Hash)
23
36
  raise ::ArgumentError, "[to_regexp] Options must be a Hash"
24
37
  end
25
38
  str = self.strip
26
-
27
- if options[:literal] == true
39
+
40
+ if options[:literal] or (options[:detect] and REGEXP_DELIMITERS.none? { |k, v| str.start_with?(k) and str.end_with?(v) })
28
41
  content = ::Regexp.escape str
29
42
  elsif delim_set = REGEXP_DELIMITERS.detect { |k, v| str.start_with?(k) }
30
43
  delim_start, delim_end = delim_set.map { |delim| ::Regexp.escape delim }
@@ -1,3 +1,3 @@
1
1
  module ToRegexp
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -121,4 +121,11 @@ class TestToRegexp < Test::Unit::TestCase
121
121
  assert_equal /(?-mix:dogs)|(?i-mx:cats)/, Regexp.union(/dogs/, '/cats/i')
122
122
  assert_equal %r{&|<|>|'|"|\/}.inspect, Regexp.union(*ESCAPE_HTML_KEYS).inspect
123
123
  end
124
+
125
+ def test_016_detect
126
+ assert_equal /foo/, 'foo'.to_regexp(detect: true)
127
+ assert_equal %r{foo\\b}, 'foo\b'.to_regexp(detect: true)
128
+ assert_equal %r{foo\b}, '/foo\b/'.to_regexp(detect: true)
129
+ assert_equal %r{foo\\b/}, 'foo\b/'.to_regexp(detect: true)
130
+ end
124
131
  end
data/to_regexp.gemspec CHANGED
@@ -1,6 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "to_regexp/version"
2
+ require File.expand_path("../lib/to_regexp/version", __FILE__)
4
3
 
5
4
  Gem::Specification.new do |s|
6
5
  s.name = "to_regexp"
@@ -18,4 +17,7 @@ Gem::Specification.new do |s|
18
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
19
  s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency 'ensure-encoding'
22
+ s.add_development_dependency 'yard'
21
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_regexp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,40 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-22 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-02-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ensure-encoding
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: yard
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
14
46
  description: Provides String#to_regexp, for example if you want to make regexps out
15
47
  of a CSV you just imported.
16
48
  email:
@@ -20,6 +52,7 @@ extensions: []
20
52
  extra_rdoc_files: []
21
53
  files:
22
54
  - .gitignore
55
+ - CHANGELOG
23
56
  - Gemfile
24
57
  - History.txt
25
58
  - README.rdoc
@@ -49,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
82
  version: '0'
50
83
  requirements: []
51
84
  rubyforge_project: to_regexp
52
- rubygems_version: 1.8.15
85
+ rubygems_version: 1.8.25
53
86
  signing_key:
54
87
  specification_version: 3
55
88
  summary: Provides String#to_regexp