tzispa_utils 0.1.5 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 66c95b4a9c3e19277608569ec0e2b53323a22524
4
- data.tar.gz: d6918738e2d07d54f8a166d534b662b794069fd2
3
+ metadata.gz: 89d93a054b3512818bdea0de54bfea3c346a295d
4
+ data.tar.gz: 9640dd1294048ade258a8223d87f06924b162da4
5
5
  SHA512:
6
- metadata.gz: 6ca13fa636bd363a8c209e6e0536b36bd3726ff2ac37a739e0f203dc053004965b48f32d67f3a66d33842ed2ef6155a439202eb7b7eeb4c80062b648160b47f4
7
- data.tar.gz: cff0ee7f9e0322cea8ca116900c0d3c1670b141be48995ace7978fc64cd78b135c27cbb9bf956614b4b3efb34d8b4780917e8101ce3fc3a079f8599aaf2441db
6
+ metadata.gz: afa3236206dd25f1c799a65d54df1633384667869e376cf4883ecb44970b542871723b0b7f5c3034868221400e330f87caad480e3032a52be9944606702a7339
7
+ data.tar.gz: 5d12e36d97c7ac02c3cd11a0a6d3cea591d873b0257540be1127a2c16886e76ee7865ed10bc5be662a891653d774889b62b327293036b04bdaaf4e2ebfdc41c5
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  Tzispa Utils
2
2
 
3
+ ## v0.2.0
4
+ - Added urlize and transliterate to Tzispa::Utils::String
5
+ - Make Tzispa::Utils::String a subclass of ::String for better usability
6
+
3
7
  ## v0.1.4
4
8
  - Added CSVFixer utility class
5
9
 
@@ -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 = @string.split(NAMESPACE_SEPARATOR)
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.class.new( @string.split(CLASSIFY_SEPARATOR).collect{ |w| w.capitalize }.join)
35
+ self.split(CLASSIFY_SEPARATOR).collect{ |w| w.capitalize }.join
46
36
  end
47
37
 
48
38
  def camelize!
49
- @string = @string.split(CLASSIFY_SEPARATOR).collect!{ |w| w.capitalize }.join
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.class.new(@string.dup.tap { |s|
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
- @string = @string.tap { |s|
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
- private
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
- def gsub(pattern, replacement = nil, &blk)
79
- if block_given?
80
- @string.gsub(pattern, &blk)
81
- else
82
- @string.gsub(pattern, replacement)
83
- end
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
@@ -3,7 +3,7 @@
3
3
  module Tzispa
4
4
  module Utils
5
5
 
6
- VERSION = '0.1.5'
6
+ VERSION = '0.2.0'
7
7
  NAME = 'Tzispa Utils'
8
8
  GEM_NAME = 'tzispa_utils'
9
9
 
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.1.5
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-02-29 00:00:00.000000000 Z
11
+ date: 2016-03-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Utilities for Tzispa
14
14
  email: