the_string_to_slug 0.0.8 → 1.1

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: ee96cdf83aae4e27d147ffe28a082a53a304e497
4
- data.tar.gz: 670d91f17e1405c90528b0d0b939cdeaf4c4e468
3
+ metadata.gz: 4a4835e1fce9b86bfd6d2972a1962e2e49cd3fb2
4
+ data.tar.gz: bb2cc21692b00c62be219635b3481c5e23a878d2
5
5
  SHA512:
6
- metadata.gz: bd4ef70a8fc9734f82b611eed07feb210639d9172538e77e0da2e70c2449f50380876680aea18a95fc9414f93f01eea5a04b0107318663e8b86184b062903664
7
- data.tar.gz: 2fa8fb13d35d6df3be99ba52c818915afbfbec3dcfa6eb4b892fc60361cc6e36425a745cc2737a1241ab81ef442f9d2c36fa41132bd2d0bb275d310077c4ecff
6
+ metadata.gz: 0b2c161d669135b2a8859f5a434f2605d440d34f14b55ad22825034d5642389efe1088ff486b85afee2867922c71f5f03714a4d201c1fb9cea3f2fcb7ce63607
7
+ data.tar.gz: 85f45fe0904df42af44f4e277763c9a3b3b666a472735e1c2c9ad82118c6cf18a524ecb344c60786dc3bcc3f6879f0c9f7709c962839c1ab0ac3217d4d0d5af4
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .DS_store
data/README.md CHANGED
@@ -34,4 +34,26 @@ For filenames:
34
34
  ```ruby
35
35
  "/доки/dir/тест/документ.doc".slugged_filename #=> "dokument.doc"
36
36
  String.slugged_filename("/доки/dir/тест/документ.doc") #=> "dokument.doc"
37
+ ```
38
+
39
+ For full file path:
40
+
41
+ ```ruby
42
+ "/доки/dir/тест/документ.doc".slugged_file #=> "/доки/dir/тест/dokument.doc"
43
+ String.slugged_filename("/доки/dir/тест/документ.doc") #=> "/доки/dir/тест/dokument.doc"
44
+ ```
45
+
46
+ Params
47
+
48
+ ```ruby
49
+ "Документ.doc".to_slug_param(locale: :en)
50
+ # => "doc"
51
+
52
+ "Документ.doc".to_slug_param(locale: :en)
53
+ # => "dokument-doc"
54
+ ```
55
+
56
+ ```ruby
57
+ "Документ.doc".to_slug_param(delimiter: '_')
58
+ # => "dokument_doc"
37
59
  ```
@@ -4,48 +4,54 @@ require "the_string_to_slug/version"
4
4
  module TheStringToSlug; end
5
5
 
6
6
  class String
7
- def to_slug_param
8
- self.class.to_slug_param(self)
7
+ def to_slug_param opts = {}
8
+ self.class.to_slug_param(self, opts)
9
9
  end
10
10
 
11
- def slugged_filename
12
- self.class.slugged_filename(self)
11
+ def slugged_filename opts = {}
12
+ self.class.slugged_filename(self, opts)
13
13
  end
14
14
 
15
- def slugged_file
16
- self.class.slugged_file(self)
15
+ def slugged_file opts = {}
16
+ self.class.slugged_file(self, opts)
17
17
  end
18
18
 
19
19
  # -----------------------------------
20
20
  # Self methods
21
21
  # -----------------------------------
22
22
  class << self
23
- def to_slug_param str
24
- I18n::transliterate(str).gsub('_','-').parameterize('-').downcase
23
+ def to_slug_param str, opts = {}
24
+ delimiter = opts.delete(:delimiter) || '-'
25
+ str = str.gsub(/\-{2,}/, '-').mb_chars
26
+ str = I18n::transliterate(str, opts)
27
+ .gsub('_', delimiter)
28
+ .gsub('-', delimiter)
29
+ .parameterize(delimiter)
30
+ .downcase.to_s
25
31
  end
26
32
 
27
- def file_ext file_name
28
- File.extname(file_name)[1..-1].to_s.to_slug_param
33
+ def file_ext file_name, opts = {}
34
+ File.extname(file_name)[1..-1].to_s.to_slug_param opts
29
35
  end
30
36
 
31
- def file_name file_name
32
- file_name = File.basename(file_name)
33
- ext = File.extname(file_name)
34
- File.basename(file_name, ext).to_s.to_slug_param
37
+ def file_name name, opts = {}
38
+ name = File.basename name
39
+ ext = File.extname name
40
+ File.basename(name, ext).to_s.to_slug_param opts
35
41
  end
36
42
 
37
- def slugged_filename file_name
38
- file_name = File.basename(file_name)
39
- fname = self.file_name(file_name)
40
- ext = self.file_ext(file_name)
43
+ def slugged_filename name, opts = {}
44
+ name = File.basename name
45
+ fname = self.file_name name, opts
46
+ ext = self.file_ext name, opts
41
47
 
42
48
  return fname if ext.blank?
43
49
  [fname, ext].join('.')
44
50
  end
45
51
 
46
- def slugged_file file_full_path
47
- file_name = slugged_filename file_full_path
48
- file_full_path.split('/')[0...-1].push(file_name).join '/'
52
+ def slugged_file file_full_path, opts = {}
53
+ fname = slugged_filename file_full_path, opts
54
+ file_full_path.split('/')[0...-1].push(fname).join '/'
49
55
  end
50
56
  end
51
57
  end
@@ -1,3 +1,3 @@
1
1
  module TheStringToSlug
2
- VERSION = "0.0.8"
2
+ VERSION = "1.1"
3
3
  end
@@ -6,6 +6,8 @@ require 'rails/all'
6
6
  # you've limited to :test, :development, or :production.
7
7
  Bundler.require(:default, Rails.env)
8
8
 
9
+ I18n.enforce_available_locales = true
10
+
9
11
  module DummyApp
10
12
  class Application < Rails::Application
11
13
  # Settings in config/environments/* take precedence over those specified here.
@@ -7,6 +7,11 @@ describe 'StringToSlug' do
7
7
  "Привет Мир! Hello world!".to_slug_param.should eq "privet-mir-hello-world"
8
8
  "Документ.doc".to_slug_param.should eq "dokument-doc"
9
9
  end
10
+
11
+ it 'When wrong translation' do
12
+ "Привет Мир! Hello world!".to_slug_param(locale: :en).should eq "hello-world"
13
+ "Документ.doc".to_slug_param(locale: :en).should eq "doc"
14
+ end
10
15
  end
11
16
 
12
17
  context 'Filename test' do
@@ -19,6 +24,17 @@ describe 'StringToSlug' do
19
24
 
20
25
  String.slugged_filename("/доки/dir/тест/доку мент").should eq "doku-ment"
21
26
  end
27
+
28
+ it 'When wrong translation' do
29
+ "/doc/dir/test/document.doc".to_slug_param(locale: :ru).should eq "doc-dir-test-document-doc"
30
+ "/doc/dir/test/document.doc".slugged_filename(locale: :ru).should eq "document.doc"
31
+
32
+ "/доки/dir/тест/документ.doc".slugged_filename(locale: :en).should eq ".doc"
33
+ "/доки/dir/тест/документ".slugged_filename(locale: :en).should eq ""
34
+ "/доки/dir/тест/доку мент".slugged_filename(locale: :en).should eq ""
35
+
36
+ String.slugged_filename("/доки/dir/тест/доку мент", locale: :en).should eq ""
37
+ end
22
38
  end
23
39
 
24
40
  context 'Full path to file' do
@@ -30,5 +46,77 @@ describe 'StringToSlug' do
30
46
 
31
47
  String.slugged_file("/доки/dir/тест/доку мент").should eq "/доки/dir/тест/doku-ment"
32
48
  end
49
+
50
+ it 'When wrong translation' do
51
+ "/doc/dir/test/document.doc".slugged_file(locale: :en).should eq "/doc/dir/test/document.doc"
52
+ "/доки/dir/тест/документ.doc".slugged_file(locale: :en).should eq "/доки/dir/тест/.doc"
53
+
54
+ "/доки/dir/тест/документ".slugged_file(locale: :en).should eq "/доки/dir/тест/"
55
+ "/доки/dir/тест/доку мент".slugged_file(locale: :en).should eq "/доки/dir/тест/"
56
+
57
+ String.slugged_file("/доки/dir/тест/доку мент", locale: :en).should eq "/доки/dir/тест/"
58
+ end
59
+ end
60
+
61
+ context 'Delimiter' do
62
+ it 'should be true' do
63
+ "Привет Мир! Hello world!".to_slug_param(delimiter: '_').should eq "privet_mir_hello_world"
64
+ "Документ.doc".to_slug_param(delimiter: '_').should eq "dokument_doc"
65
+
66
+ "/доки/dir/тест/документ".slugged_file(delimiter: '_').should eq "/доки/dir/тест/dokument"
67
+ "/доки/dir/тест/доку мент".slugged_file(delimiter: '_').should eq "/доки/dir/тест/doku_ment"
68
+ "/доки/dir/тест/доку мент".slugged_filename(delimiter: '_').should eq "doku_ment"
69
+ end
70
+ end
71
+
72
+ context "spec chars" do
73
+ it "should works" do
74
+ "[$&+,:;=?@Hello world!#|'<>.^*()%!-]".to_slug_param.should eq 'hello-world'
75
+ end
76
+
77
+ it "should works" do
78
+ "教師 — the-teacher".to_slug_param.should eq 'the-teacher'
79
+ "Hello ^, I'm here!".to_slug_param.should eq 'hello-i-m-here'
80
+ end
81
+
82
+ it "should works" do
83
+ "HELLO---WorlD".to_slug_param(delimiter: '_').should eq 'hello_world'
84
+ end
85
+
86
+ it "should works" do
87
+ str = "__...HELLO-___--+++--WorlD----__&&***...__.---"
88
+
89
+ str.to_slug_param.should eq 'hello-world'
90
+ str.to_slug_param(delimiter: '_').should eq 'hello_world'
91
+ str.to_slug_param(delimiter: '+').should eq 'hello+world'
92
+ end
93
+
94
+ it "should works" do
95
+ str = "Ilya zykin aka Killich, $$$ aka the-teacher"
96
+ str.to_slug_param(delimiter: '+').should eq "ilya+zykin+aka+killich+aka+the+teacher"
97
+ end
98
+ end
99
+
100
+ context 'Scopes' do
101
+ it "should work with Nested Controller Name" do
102
+ class PagesController < ApplicationController; end
103
+ ctrl = PagesController.new
104
+ ctrl.controller_path.to_slug_param.should eq 'pages'
105
+ end
106
+
107
+ it "should work with Nested Controller Name" do
108
+ module Admin; end
109
+ class Admin::PagesController < ApplicationController; end
110
+ ctrl = Admin::PagesController.new
111
+ ctrl.controller_path.to_slug_param.should eq 'admin-pages'
112
+ end
113
+
114
+ it "should work with Nested Controller Name" do
115
+ module Admin; end
116
+ class Admin::PagesController < ApplicationController; end
117
+ ctrl = Admin::PagesController.new
118
+ params = { delimiter: '_' }
119
+ ctrl.controller_path.to_slug_param(params).should eq 'admin_pages'
120
+ end
33
121
  end
34
122
  end
metadata CHANGED
@@ -1,65 +1,65 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: the_string_to_slug
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: '1.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya N. Zykin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-26 00:00:00.000000000 Z
11
+ date: 2014-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails-i18n
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: 4.0.0.pre
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.0.0.pre
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ! '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: ! ' Convert text string to slug param '
55
+ description: " Convert text string to slug param "
56
56
  email:
57
57
  - zykin-ilya@ya.ru
58
58
  executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - .gitignore
62
+ - ".gitignore"
63
63
  - Gemfile
64
64
  - LICENSE.txt
65
65
  - README.md
@@ -133,17 +133,17 @@ require_paths:
133
133
  - lib
134
134
  required_ruby_version: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - ! '>='
136
+ - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  required_rubygems_version: !ruby/object:Gem::Requirement
140
140
  requirements:
141
- - - ! '>='
141
+ - - ">="
142
142
  - !ruby/object:Gem::Version
143
143
  version: '0'
144
144
  requirements: []
145
145
  rubyforge_project:
146
- rubygems_version: 2.0.5
146
+ rubygems_version: 2.1.11
147
147
  signing_key:
148
148
  specification_version: 4
149
149
  summary: Translite, downcase, parameterize