tzispa_utils 0.2.3 → 0.2.4

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: 635771a5c6d6022ba305f5df8315e4bf52b0294b
4
- data.tar.gz: 063e4e4035d8953d3a9d6e9117dc9811626174a6
3
+ metadata.gz: 77cdf8877650d45bb79b04666e53592548ac3f73
4
+ data.tar.gz: eaddb0eb6ec243f2c12e457800c52da144908fb7
5
5
  SHA512:
6
- metadata.gz: cd69e2e1282f476fc70b021b4d66679d8d0d0bfe414a2e434394a72b1c6d0c41481d62c8d13320aa76ca42b898228a86e69dd4bd710c6e0deb7232b2f0a2b37e
7
- data.tar.gz: 2f5f3b86a9e4e3a3e73b9ea96d201e0cd169348b29ea6ed8b22bc2ad2966935509f7c4ac5c662cdee98e6f8d5e70bc6a70dc2da40065567758c02277e87c6d86
6
+ metadata.gz: 8707b3881ded8b189a52f08099044243245ab45a4a5bd8692a5ca4d1abe6ba98b433675ec42a5be7eefd13c5223015e7379ea4de510b77a113229ae0eef99972
7
+ data.tar.gz: 1096ba2208612c0ae5d8605bf7d4f19cd949ab609872f71f7b077289bf80763d557623f38b1b55add77e44d414aaa81c95bcca43a0ea29e07c5bc72f95c39d75
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  Tzispa Utils
2
2
 
3
+ ## v0.2.4
4
+ - Remove obsolete / unused code
5
+ - Optimization and bug fixes
6
+ - Added test support with minitest
7
+
3
8
  ## v0.2.3
4
9
  - add dottize methods in string refinement
5
10
  - replace TzString class with String refinement
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Juan Antonio Piñero
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # file: Rakefile
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |task|
5
+ task.libs << %w(test)
6
+ task.pattern = 'test/*_test.rb'
7
+ end
@@ -1,7 +1,10 @@
1
+ require 'tzispa/utils/string'
2
+
1
3
  module Tzispa
2
4
  module Utils
3
5
 
4
6
  class Indenter
7
+ using Tzispa::Utils
5
8
 
6
9
  DEFAULT_INDENT_CHAR = ' '
7
10
 
@@ -15,7 +18,7 @@ module Tzispa
15
18
  end
16
19
 
17
20
  def <<(str)
18
- @instr << self.class.indentize(str, @indent_current, @indent_char)
21
+ @instr << String.indentize(str, @indent_current, @indent_char)
19
22
  self
20
23
  end
21
24
 
@@ -34,17 +37,6 @@ module Tzispa
34
37
  self
35
38
  end
36
39
 
37
- # Indent a string by count chars
38
- def self.indentize(str, count, char = ' ')
39
- str.gsub(/([^\n]*)(\n|$)/) do |match|
40
- last_iteration = ($1 == "" && $2 == "")
41
- line = ""
42
- line << (char * count) unless last_iteration
43
- line << $1
44
- line << $2
45
- line
46
- end
47
- end
48
40
 
49
41
  end
50
42
 
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  require 'i18n'
4
2
 
5
3
  module Tzispa
@@ -33,11 +31,7 @@ module Tzispa
33
31
 
34
32
  def dottize
35
33
  dup.tap { |s|
36
- s.gsub!(NAMESPACE_SEPARATOR, DOT_SEPARATOR)
37
- s.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET)
38
- s.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET)
39
- s.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET)
40
- s.downcase!
34
+ s.dottize!
41
35
  }
42
36
  end
43
37
 
@@ -53,11 +47,7 @@ module Tzispa
53
47
 
54
48
  def underscore
55
49
  dup.tap { |s|
56
- s.gsub!(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR)
57
- s.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET)
58
- s.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET)
59
- s.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET)
60
- s.downcase!
50
+ s.underscore!
61
51
  }
62
52
  end
63
53
 
@@ -71,6 +61,27 @@ module Tzispa
71
61
  }
72
62
  end
73
63
 
64
+ def indentize(count, char = ' ')
65
+ dup.tap { |s|
66
+ s.indentize! count, char
67
+ }
68
+ end
69
+
70
+ # Indent a string by count chars
71
+ def indentize!(count, char = ' ')
72
+ tap { |str|
73
+ str.gsub!(/([^\n]*)(\n|$)/) do |match|
74
+ last_iteration = ($1 == "" && $2 == "")
75
+ line = ""
76
+ line << (char * count) unless last_iteration
77
+ line << $1
78
+ line << $2
79
+ line
80
+ end
81
+ }
82
+ end
83
+
84
+
74
85
  # Replace accents in the string using I18n.transliterate
75
86
  def transliterate(locale=nil)
76
87
  I18n.transliterate(self, ({locale: locale} if locale))
@@ -83,7 +94,7 @@ module Tzispa
83
94
  # Options
84
95
  #
85
96
  # * :downcase => call downcase on the string (defaults to true)
86
- # * :convert_spaces => Convert space to underscore (defaults to false)
97
+ # * :convert_spaces => Convert space to underscore (defaults to true)
87
98
  # * :regexp => The regexp matching characters that will be removed (defaults to /[^-_A-Za-z0-9]/)
88
99
  def urlize(options = {})
89
100
  options[:downcase] ||= true
@@ -102,25 +113,35 @@ module Tzispa
102
113
  split(word_splitter).take_while { |s| (ml += s.length + 1) <= max }.join(word_splitter)
103
114
  end
104
115
 
116
+
117
+
105
118
  end
106
119
 
107
120
 
108
121
  refine String.singleton_class do
109
122
 
110
123
  def underscore(str)
111
- String.new(str&.to_s)&.underscore
124
+ String.new(str).underscore
112
125
  end
113
126
 
114
127
  def camelize(str)
115
- String.new(str&.to_s)&.camelize
128
+ String.new(str).camelize
116
129
  end
117
130
 
118
131
  def dottize(str)
119
- String.new(str&.to_s)&.dottize
132
+ String.new(str).dottize
120
133
  end
121
134
 
122
135
  def constantize(str)
123
- String.new(str&.to_s)&.constantize
136
+ String.new(str).constantize
137
+ end
138
+
139
+ def urlize(str)
140
+ String.new(str).urlize
141
+ end
142
+
143
+ def indentize(str, count, char = ' ')
144
+ String.new(str).indentize count, char
124
145
  end
125
146
 
126
147
  end
@@ -3,7 +3,7 @@
3
3
  module Tzispa
4
4
  module Utils
5
5
 
6
- VERSION = '0.2.3'
6
+ VERSION = '0.2.4'
7
7
  NAME = 'Tzispa Utils'
8
8
  GEM_NAME = 'tzispa_utils'
9
9
 
data/lib/tzispa/utils.rb CHANGED
@@ -1,13 +1,10 @@
1
1
  module Tzispa
2
- module Utility
2
+ module Utils
3
3
 
4
4
  require 'tzispa/utils/version'
5
- require 'tzispa/utils/csv_fixer'
6
5
  require 'tzispa/utils/decorator'
7
- require 'tzispa/utils/lax'
6
+ require 'tzispa/utils/indenter'
8
7
  require 'tzispa/utils/string'
9
8
 
10
-
11
-
12
9
  end
13
10
  end
data/lib/tzispa_utils.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Tzispa
2
- module Utility
2
+ module Utils
3
3
 
4
4
  require 'tzispa/utils'
5
5
 
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+
3
+ class BaseClass
4
+
5
+ attr_accessor :a, :b
6
+
7
+ def sum
8
+ a+b
9
+ end
10
+
11
+ end
12
+
13
+ class DecoratedClass < Tzispa::Utils::Decorator
14
+
15
+ def substract
16
+ b-a
17
+ end
18
+
19
+ end
20
+
21
+
22
+ class DecoratorTest < Minitest::Test
23
+
24
+ def setup
25
+ @base = BaseClass.new
26
+ @base.a = 12
27
+ @base.b = 25
28
+ @deco = DecoratedClass.new @base
29
+ end
30
+
31
+ def test_decorator
32
+ assert_respond_to @deco, :sum
33
+ assert_respond_to @deco, :a
34
+ assert_respond_to @deco, :b
35
+ refute_respond_to @deco.component, :substract
36
+ assert (@base.a == @deco.a) && (@deco.a == 12) &&
37
+ (@base.b == @deco.b) && (@deco.b == 25) &&
38
+ (@base.sum == @deco.sum) && (@deco.sum == 37) &&
39
+ (@deco.substract == 13)
40
+ end
41
+
42
+
43
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class IndenterTest < Minitest::Test
4
+
5
+ def setup
6
+ @ind = Tzispa::Utils::Indenter.new 2
7
+ end
8
+
9
+ 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"
15
+ end
16
+
17
+
18
+
19
+ end
@@ -0,0 +1,28 @@
1
+ ---
2
+ es:
3
+ transliterate:
4
+ rule:
5
+ á: a
6
+ é: e
7
+ í: i
8
+ ó: o
9
+ ú: u
10
+ Á: A
11
+ É: E
12
+ Í: I
13
+ Ó: O
14
+ Ú: U
15
+ ñ: n
16
+ Ñ: n
17
+ ä: a
18
+ ë: e
19
+ ï: i
20
+ ö: o
21
+ ü: u
22
+ Ä: A
23
+ Ë: E
24
+ Ï: I
25
+ Ö: O
26
+ Ü: U
27
+ ç: c
28
+ Ç: c
@@ -0,0 +1,55 @@
1
+ require 'test_helper'
2
+
3
+ class StringTest < Minitest::Test
4
+ using Tzispa::Utils
5
+
6
+ @@locales = (I18n.load_path += Dir["test/res/locales/*.yml"])
7
+
8
+
9
+ def test_that_it_has_a_version_number
10
+ refute_nil ::Tzispa::Utils::VERSION
11
+ end
12
+
13
+ def test_camelize
14
+ assert "my_test".camelize == "MyTest" &&
15
+ "mytest".camelize == "Mytest" &&
16
+ "MY_TEST".camelize == "MyTest"
17
+ end
18
+
19
+ def test_constantize
20
+ assert "Tzispa::Utils::Decorator".constantize == Tzispa::Utils::Decorator &&
21
+ "Hash".constantize == Hash
22
+ end
23
+
24
+ def test_dottize
25
+ assert "Tzispa::Utils::SimpleStringParser".dottize == "tzispa.utils.simple_string_parser" &&
26
+ "SimpleStringParser".dottize == "simple_string_parser"
27
+ end
28
+
29
+ def test_underscore
30
+ assert "SimpleStringParser".underscore == "simple_string_parser" &&
31
+ "Tzispa::Utils::SimpleStringParser".underscore == "tzispa/utils/simple_string_parser"
32
+ end
33
+
34
+ def test_transliterate
35
+ refute_nil @@locales
36
+ refute_empty @@locales
37
+ assert "áéíóúÁÉÍÓÚñÑ".transliterate(:es) == "aeiouAEIOUnN" &&
38
+ "aeiouAEIOUnN".transliterate(:es) == "aeiouAEIOUnN"
39
+ end
40
+
41
+ def test_urlize
42
+ refute_nil @@locales
43
+ refute_empty @@locales
44
+ assert "El ciprés es un árbol con leña".urlize(locale: :es) == "el_cipres_es_un_arbol_con_lena"
45
+ end
46
+
47
+ def test_length_constraint_wordify
48
+ str_t = "en un lugar de la mancha de cuyo nombre no quiero acordarme"
49
+ assert str_t.length_constraint_wordify(25) == "en un lugar de la mancha" &&
50
+ str_t.length_constraint_wordify(64) == str_t &&
51
+ str_t.length_constraint_wordify(0) == ""
52
+ end
53
+
54
+
55
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'tzispa_utils'
3
+ require 'minitest/autorun'
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.2.3
4
+ version: 0.2.4
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-21 00:00:00.000000000 Z
11
+ date: 2016-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -24,7 +24,21 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.7'
27
- description: Utilities for Tzispa
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ description: Utility classes used in Tzispa framework
28
42
  email:
29
43
  - japinero@area-integral.com
30
44
  executables: []
@@ -32,17 +46,21 @@ extensions: []
32
46
  extra_rdoc_files: []
33
47
  files:
34
48
  - CHANGELOG.md
49
+ - LICENSE
35
50
  - README.md
51
+ - Rakefile
36
52
  - lib/tzispa/utils.rb
37
- - lib/tzispa/utils/cache.rb
38
- - lib/tzispa/utils/csv_fixer.rb
39
53
  - lib/tzispa/utils/decorator.rb
40
54
  - lib/tzispa/utils/indenter.rb
41
- - lib/tzispa/utils/lax.rb
42
55
  - lib/tzispa/utils/string.rb
43
56
  - lib/tzispa/utils/version.rb
44
57
  - lib/tzispa_utils.rb
45
- homepage: https://github.com/japiber/tzispa_utils.git
58
+ - test/decorator_test.rb
59
+ - test/indenter_test.rb
60
+ - test/res/locales/es.yml
61
+ - test/string_test.rb
62
+ - test/test_helper.rb
63
+ homepage: https://github.com/japiber/tzispa_utilspec.git
46
64
  licenses:
47
65
  - MIT
48
66
  metadata: {}
@@ -1,97 +0,0 @@
1
- module Tzispa
2
- module Utils
3
-
4
- class LRUCache
5
-
6
- attr_reader :size, :hash
7
-
8
- class DListQueue
9
-
10
- def initialize(size)
11
- @size = size
12
- @head = nil
13
- @tail = nil
14
- @counter = 0
15
- end
16
-
17
- def enqueue(node)
18
- @counter += 1
19
- if @head && @tail
20
- node.next = @head
21
- @head.prev = node
22
- @head = node
23
- else
24
- @head = @tail = node
25
- end
26
- end
27
-
28
- def dequeue()
29
- temp = @tail
30
- @tail.prev.next = nil
31
- @tail = @tail.prev
32
- @counter -= 1
33
- temp
34
- end
35
-
36
- def move_back(node)
37
- cur = node
38
- cur.prev&.next = cur.next
39
- cur.next&.prev = cur.prev
40
- node.next = @head
41
- @head.prev = node
42
- @head = node
43
- end
44
-
45
- end
46
-
47
-
48
- Node = Struct.new(:value, :next, :prev)
49
-
50
- def initialize(size)
51
- @size = size
52
- @hash = Hash.new
53
- @queue = DListQueue.new(size)
54
- end
55
-
56
- def get(key)
57
- @hash[key]&.tap { |value|
58
- update_access_table(value[1])
59
- }
60
- end
61
-
62
- def set(key, value)
63
- remove_least_accesed if @hash.length > @size-1
64
- if @hash[key]
65
- @hash[key][0] = value
66
- else
67
- Node.new(key).tap { |node|
68
- @hash[key] = [value, node]
69
- @queue.enqueue(node)
70
- }
71
- end
72
- end
73
-
74
- def [](key)
75
- get(key)&.slice(0)
76
- end
77
-
78
- def []=(key, value)
79
- set(key, value)
80
- end
81
-
82
- private
83
-
84
- def remove_least_accesed
85
- item = @queue.dequeue()
86
- @hash.delete(item.value)
87
- end
88
-
89
- def update_access_table(node)
90
- @queue.move_back(node)
91
- end
92
-
93
- end
94
-
95
-
96
- end
97
- end
@@ -1,26 +0,0 @@
1
- module Tzispa
2
- module Utils
3
-
4
- class CsvFixer < File
5
-
6
- def initialize(filename, mode, separator)
7
- @separator = separator
8
- super(filename, mode)
9
- end
10
-
11
- def gets(sep, limit=nil)
12
- line = limit ? super(sep) : super(sep, limit)
13
- line&.split(@separator)&.map { |field|
14
- field = field.strip
15
- if /\A\".*\"\Z/ =~ field
16
- "\"#{field.gsub(/\A\"(.*)\"\Z/, '\1').gsub(/([^\"])\"([^\"])/,'\1""\2')}\""
17
- else
18
- "\"#{field.gsub(/([^\"])\"([^\"])/, '\1""\2')}\""
19
- end
20
- }&.join(@separator)
21
- end
22
-
23
- end
24
-
25
- end
26
- end
@@ -1,42 +0,0 @@
1
- module Enumerable
2
- def lax
3
- Lax.new(self)
4
- end
5
- end
6
-
7
- class Lax < Enumerator
8
-
9
- def initialize(receiver)
10
- super() do |yielder|
11
- begin
12
- receiver.each do |val|
13
- if block_given?
14
- yield(yielder, val)
15
- else
16
- yielder << val
17
- end
18
- end
19
- rescue StopIteration
20
- end
21
- end
22
- end
23
-
24
- def map(&block)
25
- Lax.new(self) do |yielder, val|
26
- yielder << block.call(val)
27
- end
28
- end
29
-
30
- def take(n)
31
- taken = 0
32
- Lax.new(self) do |yielder, val|
33
- if taken < n
34
- yielder << val
35
- taken += 1
36
- else
37
- raise StopIteration
38
- end
39
- end
40
- end
41
-
42
- end