tzispa_utils 0.1.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/tzispa/utils/string.rb +36 -29
- data/lib/tzispa/utils/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89d93a054b3512818bdea0de54bfea3c346a295d
|
4
|
+
data.tar.gz: 9640dd1294048ade258a8223d87f06924b162da4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: afa3236206dd25f1c799a65d54df1633384667869e376cf4883ecb44970b542871723b0b7f5c3034868221400e330f87caad480e3032a52be9944606702a7339
|
7
|
+
data.tar.gz: 5d12e36d97c7ac02c3cd11a0a6d3cea591d873b0257540be1127a2c16886e76ee7865ed10bc5be662a891653d774889b62b327293036b04bdaaf4e2ebfdc41c5
|
data/CHANGELOG.md
CHANGED
data/lib/tzispa/utils/string.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'i18n'
|
4
|
+
|
3
5
|
module Tzispa
|
4
6
|
module Utils
|
5
7
|
|
6
|
-
class String
|
8
|
+
class String < ::String
|
7
9
|
|
8
10
|
NAMESPACE_SEPARATOR = '::'
|
9
11
|
CLASSIFY_SEPARATOR = '_'
|
@@ -11,24 +13,12 @@ module Tzispa
|
|
11
13
|
|
12
14
|
UNDERSCORE_DIVISION_TARGET = '\1_\2'
|
13
15
|
|
14
|
-
def initialize(str)
|
15
|
-
@string = str.to_s
|
16
|
-
end
|
17
|
-
|
18
|
-
def to_s
|
19
|
-
@string
|
20
|
-
end
|
21
|
-
|
22
|
-
def ==(other)
|
23
|
-
to_s == other
|
24
|
-
end
|
25
|
-
|
26
16
|
def self.constantize(str)
|
27
|
-
self.new(str).constantize
|
17
|
+
self.new(str.to_s).constantize
|
28
18
|
end
|
29
19
|
|
30
20
|
def constantize
|
31
|
-
names =
|
21
|
+
names = self.split(NAMESPACE_SEPARATOR)
|
32
22
|
names.shift if names.empty? || names.first.empty?
|
33
23
|
constant = Object
|
34
24
|
names.each do |name|
|
@@ -38,33 +28,33 @@ module Tzispa
|
|
38
28
|
end
|
39
29
|
|
40
30
|
def self.camelize(str)
|
41
|
-
self.new(str).camelize
|
31
|
+
self.new(str.to_s).camelize
|
42
32
|
end
|
43
33
|
|
44
34
|
def camelize
|
45
|
-
self.
|
35
|
+
self.split(CLASSIFY_SEPARATOR).collect{ |w| w.capitalize }.join
|
46
36
|
end
|
47
37
|
|
48
38
|
def camelize!
|
49
|
-
|
39
|
+
self.split(CLASSIFY_SEPARATOR).collect!{ |w| w.capitalize }.join
|
50
40
|
end
|
51
41
|
|
52
42
|
def self.underscore(str)
|
53
|
-
self.new(str).underscore
|
43
|
+
self.new(str.to_s).underscore
|
54
44
|
end
|
55
45
|
|
56
46
|
def underscore
|
57
|
-
self.
|
47
|
+
self.dup.tap { |s|
|
58
48
|
s.gsub!(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR)
|
59
49
|
s.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET)
|
60
50
|
s.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET)
|
61
51
|
s.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET)
|
62
52
|
s.downcase!
|
63
|
-
}
|
53
|
+
}
|
64
54
|
end
|
65
55
|
|
66
56
|
def underscore!
|
67
|
-
|
57
|
+
self.tap { |s|
|
68
58
|
s.gsub!(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR)
|
69
59
|
s.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET)
|
70
60
|
s.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET)
|
@@ -73,17 +63,34 @@ module Tzispa
|
|
73
63
|
}
|
74
64
|
end
|
75
65
|
|
76
|
-
|
66
|
+
# Replace accents in the string using I18n.transliterate
|
67
|
+
def transliterate(locale=nil)
|
68
|
+
I18n.transliterate(self, ({locale: locale} if locale))
|
69
|
+
end
|
77
70
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
71
|
+
# Convert a string to a format suitable for a URL without ever using escaped characters.
|
72
|
+
# It calls strip, transliterate, downcase (optional) then removes the spaces (optional)
|
73
|
+
# and finally removes any characters matching the default regexp (/[^-_A-Za-z0-9]/).
|
74
|
+
#
|
75
|
+
# Options
|
76
|
+
#
|
77
|
+
# * :downcase => call downcase on the string (defaults to true)
|
78
|
+
# * :convert_spaces => Convert space to underscore (defaults to false)
|
79
|
+
# * :regexp => The regexp matching characters that will be removed (defaults to /[^-_A-Za-z0-9]/)
|
80
|
+
def urlize(options = {})
|
81
|
+
options[:downcase] ||= true
|
82
|
+
options[:convert_spaces] ||= true
|
83
|
+
options[:regexp] ||= /[^-_A-Za-z0-9]/
|
84
|
+
|
85
|
+
self.transliterate(options[:locale]).strip.tap { |str|
|
86
|
+
str.downcase! if options[:downcase]
|
87
|
+
str.gsub!(/\ /,'_') if options[:convert_spaces]
|
88
|
+
str.gsub!(options[:regexp], '')
|
89
|
+
}
|
84
90
|
end
|
85
91
|
|
86
92
|
|
93
|
+
|
87
94
|
end
|
88
95
|
end
|
89
96
|
end
|
data/lib/tzispa/utils/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tzispa_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Antonio Piñero
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Utilities for Tzispa
|
14
14
|
email:
|