tzispa_utils 0.3.1 → 0.3.2

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: 03d766019c0461759e3125672a0a7b29de48292e
4
- data.tar.gz: 7e038be0727063d8744efa11654b7309ffa1dacf
3
+ metadata.gz: ca4b92f6f0b683404de7f1cc52994ddf93999cf4
4
+ data.tar.gz: 83300a6e5917346f27cda41d471ff7f4f1e719c6
5
5
  SHA512:
6
- metadata.gz: 3cb6386b173141f9d580a77af4f05f4ee9d6f3377d1529fecc3954d0bcfe449b82499bd58ca27568fd923f587d542640227278098bf2ceffa200d17c293c36d8
7
- data.tar.gz: 119ca9321417f2378f4b5fcfa5b8c61267b0f1f7d50292cb68ee43edd512df462bb0d30f250a0fe75064d0fed27c3e808f62aa81889d0be9332ac6bbb4f51d73
6
+ metadata.gz: 2968a00cffa40a2afb2ac0b7155753b10eb1edc999afd1aacb6331b889cc5b1a6bda01a40064507688c32631fac820aec44e017fe37e7554b9340b0aff157886
7
+ data.tar.gz: 830b03732abc7ceabca1dd9c3006e427015cd96cfd04422b28fa182e395fcf3d4ae45f5cae913351f8a6a25374231759c10491512db4fe72087d4ccadf15e6af
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  Tzispa Utils
2
2
 
3
+ ## v0.3.2
4
+ - bug fixes
5
+ - added tests for hash refinements
6
+ - added mail class utility
7
+
3
8
  ## v0.3.1
4
9
  - added Hash refinement
5
10
 
@@ -1,16 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Tzispa
2
4
  module Utils
3
5
 
4
6
  class Decorator < SimpleDelegator
7
+ attr_reader :component
5
8
 
6
- def component
7
- @component ||= __getobj__
9
+ def initialize(component)
10
+ @component = component
11
+ super
8
12
  end
9
13
 
10
14
  def cclass
11
- component.class
15
+ @component.class
12
16
  end
13
-
14
17
  end
15
18
 
16
19
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Tzispa
2
4
  module Utils
3
5
  module Duplicable
@@ -12,7 +14,7 @@ module Tzispa
12
14
  value.dup
13
15
  end
14
16
  end
15
-
17
+
16
18
  end
17
19
  end
18
- end
20
+ end
@@ -1,57 +1,90 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'tzispa/utils/duplicable'
2
4
 
3
5
  module Tzispa
4
6
  module Utils
7
+ module TzHash
5
8
 
6
- DUPLICATE_LOGIC = proc do |value|
7
- case value
8
- when Hash
9
- value.deep_dup
10
- when ::Hash
11
- Hash.new(value).deep_dup.to_h
12
- end
13
- end.freeze
9
+ DUPLICATE_LOGIC = proc do |value|
10
+ case value
11
+ when Hash
12
+ value.deep_dup
13
+ when ::Hash
14
+ Hash.new(value).deep_dup.to_h
15
+ end
16
+ end.freeze
17
+
18
+ refine ::Hash do
19
+ def symbolize
20
+ dup.tap(&:symbolize!)
21
+ end
14
22
 
15
- refine Hash do
23
+ def symbolize!
24
+ keys.each do |k|
25
+ v = delete(k)
26
+ self[k.to_sym] = v
27
+ end
16
28
 
17
- def symbolize!
18
- keys.each do |k|
19
- v = delete(k)
20
- self[k.to_sym] = v
29
+ self
21
30
  end
22
31
 
23
- self
24
- end
32
+ def deep_symbolize
33
+ dup.tap do |hsh|
34
+ hsh.keys.each do |k|
35
+ v = hsh.delete(k)
36
+ v = v.dup.deep_symbolize if v.respond_to?(:to_hash)
37
+
38
+ hsh[k.to_sym] = v
39
+ end
40
+ end
41
+ end
25
42
 
26
- def deep_symbolize!
27
- keys.each do |k|
28
- v = delete(k)
29
- v = self.class.new(v).deep_symbolize! if v.respond_to?(:to_hash)
43
+ def deep_symbolize!
44
+ keys.each do |k|
45
+ v = delete(k)
46
+ v = v.deep_symbolize! if v.respond_to?(:to_hash)
30
47
 
31
- self[k.to_sym] = v
48
+ self[k.to_sym] = v
49
+ end
50
+
51
+ self
32
52
  end
33
53
 
34
- self
35
- end
54
+ def stringify
55
+ dup.tap(&:stringify!)
56
+ end
57
+
58
+ def stringify!
59
+ keys.each do |k|
60
+ v = delete(k)
61
+ v = self.class.new(v).stringify! if v.respond_to?(:to_hash)
36
62
 
37
- def stringify!
38
- keys.each do |k|
39
- v = delete(k)
40
- v = self.class.new(v).stringify! if v.respond_to?(:to_hash)
63
+ self[k.to_s] = v
64
+ end
41
65
 
42
- self[k.to_s] = v
66
+ self
43
67
  end
44
68
 
45
- self
46
- end
69
+ def tuplify
70
+ [].tap do |tp|
71
+ each do |k, v|
72
+ if v.respond_to? :each
73
+ v.each { |w| tp << [k, w] }
74
+ else
75
+ tp << [k, v]
76
+ end
77
+ end
78
+ end
79
+ end
47
80
 
48
- def deep_dup
49
- self.class.new.tap do |result|
50
- @hash.each { |k, v| result[k] = Duplicable.dup(v, &DUPLICATE_LOGIC) }
81
+ def deep_dup
82
+ self.class.new.tap do |result|
83
+ @hash.each { |k, v| result[k] = Duplicable.dup(v, &DUPLICATE_LOGIC) }
84
+ end
51
85
  end
52
86
  end
53
87
 
54
88
  end
55
-
56
89
  end
57
90
  end
@@ -1,24 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'tzispa/utils/string'
2
4
 
3
5
  module Tzispa
4
6
  module Utils
5
7
 
6
8
  class Indenter
7
- using Tzispa::Utils
9
+ using Tzispa::Utils::TzString
8
10
 
9
11
  DEFAULT_INDENT_CHAR = ' '
12
+ DEFAULT_INDENT_SIZE = 2
10
13
 
11
14
  attr_reader :sbuff, :indent_char, :instr
12
15
 
13
- def initialize(indent_size, indent_char=DEFAULT_INDENT_CHAR)
14
- @indent_size = indent_size
16
+ def initialize(indent_size = nil, indent_char = nil)
17
+ @indent_size = indent_size || DEFAULT_INDENT_SIZE
15
18
  @indent_current = 0
16
- @indent_char = indent_char
19
+ @indent_char = indent_char || DEFAULT_INDENT_CHAR
17
20
  @instr = String.new
18
21
  end
19
22
 
20
- def <<(str)
21
- @instr << String.indentize(str, @indent_current, @indent_char)
23
+ def <<(item)
24
+ ss = item.to_s.indentize(@indent_current, @indent_char)
25
+ @instr.concat ss
26
+
22
27
  self
23
28
  end
24
29
 
@@ -28,16 +33,19 @@ module Tzispa
28
33
 
29
34
  def indent
30
35
  @indent_current += @indent_size
36
+
31
37
  self
32
38
  end
33
39
 
34
40
  def unindent
35
- @indent_current -= @indent_size if @indent_current-@indent_size >= 0
36
- @indent_current = 0 if @indent_current-@indent_size < 0
41
+ if (@indent_current - @indent_size).positive?
42
+ @indent_current -= @indent_size
43
+ else
44
+ @indent_current = 0
45
+ end
46
+
37
47
  self
38
48
  end
39
-
40
-
41
49
  end
42
50
 
43
51
  end
@@ -1,30 +1,38 @@
1
- require 'i18n'
2
- require 'sanitize'
3
- require 'escape_utils'
1
+ # frozen_string_literal: true
2
+
4
3
  require 'i18n'
5
4
 
6
5
  module Tzispa
7
6
  module Utils
7
+ module TzInteger
8
+
9
+ refine ::Integer do
10
+ def to_filesize(precission = 1, sep = nil)
11
+ units, qty = __i18n_filesize
12
+ ns = (to_f / (qty / 1024)).round(precission).to_s.split('.')
13
+ ns.pop if ns.last.to_i.zero?
14
+ __nu_format(ns.join(sep || I18n.t('number.format.separator', default: '.')),
15
+ units)
16
+ end
8
17
 
9
- refine Integer do
18
+ def __nu_format(number, units)
19
+ I18n.t('number.human.storage_units.format', default: '%n %u')
20
+ .sub('%n', number)
21
+ .sub('%u', units)
22
+ end
10
23
 
11
- def to_filesize(precission=1)
12
- fsz = {
13
- I18n.t('number.human.storage_units.byte.other', default: 'Bytes') => 1024,
14
- I18n.t('number.human.storage_units.kb', default: 'KB') => 1024 * 1024,
15
- I18n.t('number.human.storage_units.mb', default: 'MB') => 1024 * 1024 * 1024,
16
- I18n.t('number.human.storage_units.gb', default: 'GB') => 1024 * 1024 * 1024 * 1024,
17
- I18n.t('number.human.storage_units.tb', default: 'TB') => 1024 * 1024 * 1024 * 1024 * 1024
18
- }.select { |k, v| self < v }.first
19
- ns = (self.to_f / (fsz[1] / 1024)).round(precission).to_s.split('.')
20
- n = ns[1].to_i == 0 ? ns[0] : ns.join( I18n.t('number.format.separator', default: '.') )
21
- u = fsz[0]
22
- fmt = I18n.t('number.human.storage_units.format', default: '%n %u')
23
- fmt.sub('%n', n).sub('%u', u)
24
+ def __i18n_filesize
25
+ {
26
+ I18n.t('number.human.storage_units.byte.other', default: 'Bytes') => 1024,
27
+ I18n.t('number.human.storage_units.kb', default: 'KB') => 1024**2,
28
+ I18n.t('number.human.storage_units.mb', default: 'MB') => 1024**3,
29
+ I18n.t('number.human.storage_units.gb', default: 'GB') => 1024**4,
30
+ I18n.t('number.human.storage_units.tb', default: 'TB') => 1024**5,
31
+ I18n.t('number.human.storage_units.pb', default: 'PB') => 1024**6
32
+ }.select { |_, v| self < v }.first
33
+ end
24
34
  end
25
35
 
26
36
  end
27
-
28
-
29
37
  end
30
- end
38
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mail'
4
+
5
+ module Tzispa
6
+ module Utils
7
+
8
+ class Mail
9
+ attr_accessor :from, :to, :subject, :content, :cc,
10
+ :html, :charset, :debug
11
+
12
+ attr_reader :config, :mail, :logger
13
+
14
+ DEFAULT_CHARSET = 'UTF-8'
15
+
16
+ def initialize(config, logger = nil)
17
+ @hmtl = false
18
+ @debug = false
19
+ @charset = DEFAULT_CHARSET
20
+ @logger = logger
21
+ @config = config
22
+ smtp_configuration(config)
23
+ @mail = ::Mail.new
24
+ end
25
+
26
+ def send_mail
27
+ recipients
28
+ body
29
+ mail.subject = subject
30
+ mail.charset = charset
31
+ mail.deliver
32
+ rescue
33
+ raise if debug
34
+ end
35
+
36
+ def add_file(path, attach_name)
37
+ mail.attachments[attach_name] = File.read(path)
38
+ end
39
+
40
+ private
41
+
42
+ def recipients
43
+ mail.from = from
44
+ mail.to to.split(';')
45
+ return unless cc
46
+ mail.cc cc.split(';')
47
+ end
48
+
49
+ def body
50
+ if html
51
+ mail.html_part do
52
+ content_type "text/html; charset=#{charset}"
53
+ body content
54
+ end
55
+ else
56
+ mail.body = content
57
+ end
58
+ end
59
+
60
+ def smtp_configuration(config)
61
+ if config.smtp_auth
62
+ auth_delivery(config)
63
+ else
64
+ delivery(config)
65
+ end
66
+ end
67
+
68
+ def delivery(config)
69
+ ::Mail.defaults do
70
+ delivery_method :smtp, address: config.host, domain: config.domain,
71
+ port: config.port, openssl_verify_mode: config.openssl_verify,
72
+ enable_starttls_auto: config.starttls_auto
73
+ end
74
+ end
75
+
76
+ def auth_delivery(config)
77
+ ::Mail.defaults do
78
+ delivery_method :smtp, address: config.host, domain: config.domain,
79
+ port: config.port, authentication: config.authentication,
80
+ openssl_verify_mode: config.openssl_verify,
81
+ enable_starttls_auto: config.starttls_auto,
82
+ user_name: config.user_name, password: config.password
83
+ end
84
+ end
85
+ end
86
+
87
+ end
88
+ end
@@ -1,183 +1,181 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'i18n'
2
4
  require 'sanitize'
3
5
  require 'escape_utils'
4
6
 
5
7
  module Tzispa
6
8
  module Utils
7
-
8
- NAMESPACE_SEPARATOR = '::'
9
- CLASSIFY_SEPARATOR = '_'
10
- UNDERSCORE_SEPARATOR = '/'
11
- DOT_SEPARATOR = '.'
12
- UNDERSCORE_DIVISION_TARGET = '\1_\2'
13
-
14
-
15
- refine String do
16
-
17
- def constantize
18
- names = self.split(NAMESPACE_SEPARATOR)
19
- names.shift if names.empty? || names.first.empty?
20
- constant = Object
21
- names.each do |name|
22
- constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
9
+ module TzString
10
+
11
+ NAMESPACE_SEPARATOR = '::'
12
+ CLASSIFY_SEPARATOR = '_'
13
+ UNDERSCORE_SEPARATOR = '/'
14
+ DOT_SEPARATOR = '.'
15
+ UNDERSCORE_DIVISION_TARGET = '\1_\2'
16
+
17
+ refine String do
18
+ def constantize
19
+ names = split(NAMESPACE_SEPARATOR)
20
+ names.shift if names.empty? || names.first.empty?
21
+ constant = Object
22
+ names.each do |name|
23
+ constant = if constant.const_defined?(name)
24
+ constant.const_get(name)
25
+ else
26
+ constant.const_missing(name)
27
+ end
28
+ end
29
+ constant
23
30
  end
24
- constant
25
- end
26
31
 
27
- def camelize
28
- split(CLASSIFY_SEPARATOR).collect{ |w| w.capitalize }.join
29
- end
32
+ def camelize
33
+ split(CLASSIFY_SEPARATOR).collect(&:capitalize).join
34
+ end
30
35
 
31
- def camelize!
32
- split(CLASSIFY_SEPARATOR).collect!{ |w| w.capitalize }.join
33
- end
36
+ def camelize!
37
+ split(CLASSIFY_SEPARATOR).collect!(&:capitalize).join
38
+ end
34
39
 
35
- def dottize
36
- dup.tap { |s|
37
- s.dottize!
38
- }
39
- end
40
+ def dottize
41
+ dup.tap do |s|
42
+ s.gsub!(NAMESPACE_SEPARATOR, DOT_SEPARATOR)
43
+ s.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET)
44
+ s.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET)
45
+ s.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET)
46
+ s.downcase!
47
+ end
48
+ end
40
49
 
41
- def dottize!
42
- tap { |s|
43
- s.gsub!(NAMESPACE_SEPARATOR, DOT_SEPARATOR)
44
- s.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET)
45
- s.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET)
46
- s.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET)
47
- s.downcase!
48
- }
49
- end
50
+ def dottize!
51
+ tap do |s|
52
+ s.gsub!(NAMESPACE_SEPARATOR, DOT_SEPARATOR)
53
+ s.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET)
54
+ s.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET)
55
+ s.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET)
56
+ s.downcase!
57
+ end
58
+ end
50
59
 
51
- def underscore
52
- dup.tap { |s|
53
- s.underscore!
54
- }
55
- end
60
+ def underscore
61
+ dup.tap(&:underscore!)
62
+ end
56
63
 
57
- def underscore!
58
- tap { |s|
59
- s.gsub!(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR)
60
- s.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET)
61
- s.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET)
62
- s.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET)
63
- s.downcase!
64
- }
65
- end
64
+ def underscore!
65
+ tap do |s|
66
+ s.gsub!(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR)
67
+ s.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET)
68
+ s.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET)
69
+ s.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET)
70
+ s.downcase!
71
+ end
72
+ end
66
73
 
67
- def indentize(count, char = ' ')
68
- dup.tap { |s|
69
- s.indentize! count, char
70
- }
71
- end
74
+ def indentize(count, char = ' ')
75
+ dup.tap { |str| str.indentize! count, char }
76
+ end
72
77
 
73
- # Indent a string by count chars
74
- def indentize!(count, char = ' ')
75
- tap { |str|
76
- str.gsub!(/([^\n]*)(\n|$)/) do |match|
77
- last_iteration = ($1 == "" && $2 == "")
78
- line = ""
78
+ # Indent a string by count chars
79
+ def indentize!(count, char = ' ')
80
+ gsub!(/([^\n]*)(\n|$)/) do
81
+ s1 = Regexp.last_match(1)
82
+ s2 = Regexp.last_match(2)
83
+ last_iteration = (s1 == '' && s2 == '')
84
+ line = String.new
79
85
  line << (char * count) unless last_iteration
80
- line << $1
81
- line << $2
86
+ line << s1 << s2
82
87
  line
83
88
  end
84
- }
85
- end
86
-
89
+ end
87
90
 
88
- # Replace accents in the string using I18n.transliterate
89
- def transliterate(locale=nil)
90
- I18n.transliterate(self, ({locale: locale} if locale))
91
- end
91
+ # Replace accents in the string using I18n.transliterate
92
+ def transliterate(locale = nil)
93
+ I18n.transliterate(self, ({ locale: locale } if locale))
94
+ end
92
95
 
93
- # Convert a string to a format suitable for a URL without ever using escaped characters.
94
- # It calls strip, transliterate, downcase (optional) then removes the spaces (optional)
95
- # and finally removes any characters matching the default regexp (/[^-_A-Za-z0-9]/).
96
- #
97
- # Options
98
- #
99
- # * :downcase => call downcase on the string (defaults to true)
100
- # * :convert_spaces => Convert space to underscore (defaults to true)
101
- # * :regexp => The regexp matching characters that will be removed (defaults to /[^-_A-Za-z0-9]/)
102
- def urlize(options = {})
103
- options[:downcase] ||= true
104
- options[:convert_spaces] ||= true
105
- options[:regexp] ||= /[^-_A-Za-z0-9]/
106
-
107
- transliterate(options[:locale]).strip.tap { |str|
108
- str.downcase! if options[:downcase]
109
- str.gsub!(/\ /,'_') if options[:convert_spaces]
110
- str.gsub!(options[:regexp], '')
111
- }
112
- end
96
+ # Convert a string to a format suitable for a URL without ever using escaped characters.
97
+ # It calls strip, transliterate, downcase (optional) then removes the spaces (optional)
98
+ # and finally removes any characters matching the default regexp (/[^-_A-Za-z0-9]/).
99
+ #
100
+ # Options
101
+ #
102
+ # * :downcase => call downcase on the string (defaults to true)
103
+ # * :convert_spaces => Convert space to underscore (defaults to true)
104
+ # * :regexp => matching characters that will be removed (defaults to /[^-_A-Za-z0-9]/)
105
+ def urlize(options = {})
106
+ options[:downcase] ||= true
107
+ options[:convert_spaces] ||= true
108
+ options[:regexp] ||= /[^-_A-Za-z0-9]/
109
+
110
+ transliterate(options[:locale]).strip.tap do |str|
111
+ str.downcase! if options[:downcase]
112
+ str.tr!(' ', '_') if options[:convert_spaces]
113
+ str.gsub!(options[:regexp], '')
114
+ end
115
+ end
113
116
 
114
- def length_constraint_wordify(max, word_splitter=' ')
115
- ml = 0
116
- split(word_splitter).take_while { |s| (ml += s.length + 1) <= max }.join(word_splitter)
117
- end
117
+ def length_constraint_wordify(max, word_splitter = ' ')
118
+ ml = 0
119
+ split(word_splitter).take_while { |s| (ml += s.length + 1) <= max }.join(word_splitter)
120
+ end
118
121
 
119
- def sanitize_html(level = nil)
120
- level ? Sanitize.fragment(self, level) :
121
- Sanitize.fragment(self)
122
- end
123
- alias sanitize_htm sanitize_html
122
+ def sanitize_html(level = nil)
123
+ level ? Sanitize.fragment(self, level) : Sanitize.fragment(self)
124
+ end
125
+ alias_method :sanitize_htm, :sanitize_html
124
126
 
125
- def escape_html
126
- EscapeUtils.escape_html(self)
127
- end
128
- alias_method :escape_htm, :escape_html
127
+ def escape_html
128
+ EscapeUtils.escape_html(self)
129
+ end
130
+ alias_method :escape_htm, :escape_html
129
131
 
130
- def unescape_html
131
- EscapeUtils.unescape_html(self)
132
+ def unescape_html
133
+ EscapeUtils.unescape_html(self)
134
+ end
135
+ alias_method :unescape_htm, :unescape_html
132
136
  end
133
- alias_method :unescape_htm, :unescape_html
134
137
 
135
- end
136
-
137
-
138
- refine String.singleton_class do
139
-
140
- def underscore(str)
141
- String.new(str).underscore
142
- end
138
+ refine String.singleton_class do
139
+ def underscore(str)
140
+ String.new(str).underscore
141
+ end
143
142
 
144
- def camelize(str)
145
- String.new(str).camelize
146
- end
143
+ def camelize(str)
144
+ String.new(str).camelize
145
+ end
147
146
 
148
- def dottize(str)
149
- String.new(str).dottize
150
- end
147
+ def dottize(str)
148
+ String.new(str).dottize
149
+ end
151
150
 
152
- def constantize(str)
153
- String.new(str).constantize
154
- end
151
+ def constantize(str)
152
+ String.new(str).constantize
153
+ end
155
154
 
156
- def urlize(str)
157
- String.new(str).urlize
158
- end
155
+ def urlize(str)
156
+ String.new(str).urlize
157
+ end
159
158
 
160
- def indentize(str, count, char = ' ')
161
- String.new(str).indentize count, char
162
- end
159
+ def indentize(str, count, char = ' ')
160
+ String.new(str).indentize count, char
161
+ end
163
162
 
164
- def sanitize_html(str, level = nil)
165
- String.new(str).santize(level)
166
- end
167
- alias_method :sanitize_htm, :sanitize_html
163
+ def sanitize_html(str, level = nil)
164
+ String.new(str).santize(level)
165
+ end
166
+ alias_method :sanitize_htm, :sanitize_html
168
167
 
169
- def escape_html(str)
170
- String.new(str).escape_html
171
- end
172
- alias_method :escape_htm, :escape_html
168
+ def escape_html(str)
169
+ String.new(str).escape_html
170
+ end
171
+ alias_method :escape_htm, :escape_html
173
172
 
174
- def unescape_html(str)
175
- String.new(str).unescape_html
173
+ def unescape_html(str)
174
+ String.new(str).unescape_html
175
+ end
176
+ alias_method :unescape_htm, :unescape_html
176
177
  end
177
- alias_method :unescape_htm, :unescape_html
178
178
 
179
179
  end
180
-
181
-
182
180
  end
183
181
  end
@@ -3,7 +3,7 @@
3
3
  module Tzispa
4
4
  module Utils
5
5
 
6
- VERSION = '0.3.1'
6
+ VERSION = '0.3.2'
7
7
  NAME = 'Tzispa Utils'
8
8
  GEM_NAME = 'tzispa_utils'
9
9
 
@@ -1,26 +1,22 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  class BaseClass
4
-
5
6
  attr_accessor :a, :b
6
7
 
7
8
  def sum
8
- a+b
9
+ (a + b)
9
10
  end
10
-
11
11
  end
12
12
 
13
13
  class DecoratedClass < Tzispa::Utils::Decorator
14
-
15
14
  def substract
16
- b-a
15
+ (b - a)
17
16
  end
18
-
19
17
  end
20
18
 
21
-
22
19
  class DecoratorTest < Minitest::Test
23
-
24
20
  def setup
25
21
  @base = BaseClass.new
26
22
  @base.a = 12
data/test/hash_test.rb ADDED
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+ require 'tzispa/utils/hash'
5
+
6
+ class HashTest < Minitest::Test
7
+ using Tzispa::Utils::TzHash
8
+
9
+ def test_symbolize
10
+ s1 = { 'one' => 1, 'two' => 2 }
11
+ s1s = { one: 1, two: 2 }
12
+ assert_equal s1.symbolize.keys, [:one, :two]
13
+ assert_equal s1.symbolize, s1s
14
+ refute_equal s1, s1s
15
+ s1.symbolize!
16
+ assert_equal s1, s1s
17
+ end
18
+
19
+ def test_deep_symbolize
20
+ s2 = { 'one' => 1, 'two' => { 'one' => 1, 'two' => 2 } }
21
+ s2s = { one: 1, two: { 'one' => 1, 'two' => 2 } }
22
+ s2d = { one: 1, two: { one: 1, two: 2 } }
23
+ s2m = { 'one' => 1, 'two' => { one: 1, two: 2 } }
24
+ assert_equal s2.symbolize, s2s
25
+ assert_equal s2.deep_symbolize, s2d
26
+ refute_equal s2, s2d
27
+ refute_equal s2, s2m
28
+ s2.deep_symbolize!
29
+ assert_equal s2, s2d
30
+ end
31
+
32
+ def test_stringify
33
+ s1 = { 'one' => 1, 'two' => 2 }
34
+ s1s = { one: 1, two: 2 }
35
+ assert_equal s1s.stringify.keys, %w(one two)
36
+ assert_equal s1s.stringify, s1
37
+ refute_equal s1, s1s
38
+ s1s.stringify!
39
+ assert_equal s1, s1s
40
+ end
41
+
42
+ def test_tuplify
43
+ s1 = { 'one' => 1, 'two' => 2, 'three' => 3 }
44
+ s2 = { file: %w(read write delete), document: %w(read update), folder: 'read' }
45
+ assert_equal s1.tuplify,
46
+ [['one', 1], ['two', 2], ['three', 3]]
47
+ assert_equal s2.tuplify,
48
+ [[:file, 'read'], [:file, 'write'], [:file, 'delete'],
49
+ [:document, 'read'], [:document, 'update'],
50
+ [:folder, 'read']]
51
+ end
52
+
53
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  class IndenterTest < Minitest::Test
@@ -7,13 +9,15 @@ class IndenterTest < Minitest::Test
7
9
  end
8
10
 
9
11
  def test_indenter
10
- str_t = "indenter test"
11
- assert (@ind.indent << str_t).to_s == " indenter test"
12
- assert (@ind.indent << str_t).to_s == " indenter test indenter test"
13
- assert (@ind.unindent << str_t).to_s == " indenter test indenter test indenter test"
14
- assert (@ind.unindent << str_t).to_s == " indenter test indenter test indenter testindenter test"
12
+ str_t = 'indenter test'
13
+ assert_equal (@ind.indent << str_t).to_s,
14
+ ' indenter test'
15
+ assert_equal (@ind.indent << str_t).to_s,
16
+ ' indenter test indenter test'
17
+ assert_equal (@ind.unindent << str_t).to_s,
18
+ ' indenter test indenter test indenter test'
19
+ assert_equal (@ind.unindent << str_t).to_s,
20
+ ' indenter test indenter test indenter testindenter test'
15
21
  end
16
22
 
17
-
18
-
19
23
  end
data/test/integer_test.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class IntegerTest < Minitest::Test
4
- using Tzispa::Utils
4
+ using Tzispa::Utils::TzInteger
5
5
 
6
6
  def setup
7
7
  I18n.enforce_available_locales = false
@@ -12,6 +12,8 @@ class IntegerTest < Minitest::Test
12
12
  end
13
13
 
14
14
  def test_filesize
15
+ I18n.load_path = nil
16
+ I18n.default_locale = nil
15
17
  assert_equal 8192.to_filesize, '8 KB'
16
18
  assert_equal 8192000.to_filesize, '7.8 MB'
17
19
  assert_equal 8388608.to_filesize, '8 MB'
data/test/string_test.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class StringTest < Minitest::Test
4
- using Tzispa::Utils
4
+ using Tzispa::Utils::TzString
5
5
 
6
6
  @@locales = (I18n.load_path += Dir["test/res/locales/*.yml"])
7
7
 
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.3.1
4
+ version: 0.3.2
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: 2017-02-20 00:00:00.000000000 Z
11
+ date: 2017-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mail
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.6'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.6'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: minitest
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +80,7 @@ dependencies:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: '5.0'
69
- description: Utility classes used in Tzispa framework
83
+ description: Utility classes used by Tzispa framework
70
84
  email:
71
85
  - japinero@area-integral.com
72
86
  executables: []
@@ -83,10 +97,12 @@ files:
83
97
  - lib/tzispa/utils/hash.rb
84
98
  - lib/tzispa/utils/indenter.rb
85
99
  - lib/tzispa/utils/integer.rb
100
+ - lib/tzispa/utils/mail.rb
86
101
  - lib/tzispa/utils/string.rb
87
102
  - lib/tzispa/utils/version.rb
88
103
  - lib/tzispa_utils.rb
89
104
  - test/decorator_test.rb
105
+ - test/hash_test.rb
90
106
  - test/indenter_test.rb
91
107
  - test/integer_test.rb
92
108
  - test/res/locales/es.yml
@@ -100,6 +116,7 @@ post_install_message:
100
116
  rdoc_options: []
101
117
  require_paths:
102
118
  - lib
119
+ - test
103
120
  required_ruby_version: !ruby/object:Gem::Requirement
104
121
  requirements:
105
122
  - - "~>"