tzispa_utils 0.2.1 → 0.2.2

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: 1c5bb436155051a840c18c851e169692afdec5b6
4
- data.tar.gz: d2822691fc761083cfa2db17d34afd3e0f8e4af1
3
+ metadata.gz: 77125d8627c9bdb1de850ec93e48233a377558f2
4
+ data.tar.gz: ad37d8e675926bbf0015b120f6748fb50a8e34c2
5
5
  SHA512:
6
- metadata.gz: 56a2687e8869a0cce036ba60d5770ed6d2509a21994bd7061e4abfd1211a2e10aad7ed2218a1630158c03383557d1b86e2e8cd99b3f6e394ee5ea53301a43f28
7
- data.tar.gz: 5324ff5d7a1a09b7017f81f77f6db11c2ce4935da50b993211054cc2a8da576c0c48eeb032dfcf730bf8861755a97b3c55abfeb5107b74fc1b1f028265324156
6
+ metadata.gz: 00c5eae659271cd1b087548ee5a0083c8ee9949587e8cc9e809eeaaa12810d2e3f83809ffb8d30e9ae030f12e774f012cc787337210a2a12653fc1b38196f545
7
+ data.tar.gz: b0545451b6a61d4232f13ae6cb1d1891783fb838f6adb6639929e26e3bd725a93fd92f81601ad248efa406e668d7a7c62ab41601072ac852df884e61cde25614
data/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  Tzispa Utils
2
2
 
3
- ## v0.2.1
3
+ ## v0.2.2
4
4
  - Add string indentation utility
5
5
 
6
6
  ## v0.2.0
@@ -0,0 +1,97 @@
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
@@ -0,0 +1,52 @@
1
+ module Tzispa
2
+ module Utils
3
+
4
+ class Indenter
5
+
6
+ DEFAULT_INDENT_CHAR = ' '
7
+
8
+ attr_reader :sbuff, :indent_char, :instr
9
+
10
+ def initialize(indent_size, indent_char=DEFAULT_INDENT_CHAR)
11
+ @indent_size = indent_size
12
+ @indent_current = 0
13
+ @indent_char = indent_char
14
+ @instr = String.new
15
+ end
16
+
17
+ def <<(str)
18
+ @instr << self.class.indentize(str, @indent_current, @indent_char)
19
+ self
20
+ end
21
+
22
+ def to_s
23
+ @instr
24
+ end
25
+
26
+ def indent
27
+ @indent_current += @indent_size
28
+ self
29
+ end
30
+
31
+ def unindent
32
+ @indent_current -= @indent_size if @indent_current-@indent_size >= 0
33
+ @indent_current = 0 if @indent_current-@indent_size < 0
34
+ self
35
+ end
36
+
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
+
49
+ end
50
+
51
+ end
52
+ end
@@ -8,16 +8,8 @@ module Tzispa
8
8
  NAMESPACE_SEPARATOR = '::'
9
9
  CLASSIFY_SEPARATOR = '_'
10
10
  UNDERSCORE_SEPARATOR = '/'
11
- DEFAULT_INDENT_CHAR = ' '
12
11
  UNDERSCORE_DIVISION_TARGET = '\1_\2'
13
12
 
14
- attr_accessor :indent_char
15
-
16
- def initialize(s='')
17
- super(s)
18
- @indent = 0
19
- @indent_char = DEFAULT_INDENT_CHAR
20
- end
21
13
 
22
14
  def self.constantize(str)
23
15
  self.new(str.to_s).constantize
@@ -95,33 +87,6 @@ module Tzispa
95
87
  }
96
88
  end
97
89
 
98
- def indenter(str=nil, count=0)
99
- @indent += count if count > 0
100
- self.class.indentize(str&.to_s || self, @indent, @indent_char)
101
- end
102
-
103
- def unindenter(str=nil, count=0)
104
- @indent -= count if count > 0 && @indent-count >= 0
105
- @indent = 0 if count > 0 && @indent-count < 0
106
- self.class.indentize(str&.to_s || self, @indent, @indent_char)
107
- end
108
-
109
- # Indent a string by count chars
110
- def indentize(count, char = ' ')
111
- gsub(/([^\n]*)(\n|$)/) do |match|
112
- last_iteration = ($1 == "" && $2 == "")
113
- line = ""
114
- line << (char * count) unless last_iteration
115
- line << $1
116
- line << $2
117
- line
118
- end
119
- end
120
-
121
- def self.indentize(str, count, char = ' ')
122
- self.new(str).indentize(count, char)
123
- end
124
-
125
90
  end
126
91
  end
127
92
  end
@@ -3,7 +3,7 @@
3
3
  module Tzispa
4
4
  module Utils
5
5
 
6
- VERSION = '0.2.1'
6
+ VERSION = '0.2.2'
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.2.1
4
+ version: 0.2.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: 2016-03-23 00:00:00.000000000 Z
11
+ date: 2016-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -34,8 +34,10 @@ files:
34
34
  - CHANGELOG.md
35
35
  - README.md
36
36
  - lib/tzispa/utils.rb
37
+ - lib/tzispa/utils/cache.rb
37
38
  - lib/tzispa/utils/csv_fixer.rb
38
39
  - lib/tzispa/utils/decorator.rb
40
+ - lib/tzispa/utils/indenter.rb
39
41
  - lib/tzispa/utils/lax.rb
40
42
  - lib/tzispa/utils/string.rb
41
43
  - lib/tzispa/utils/version.rb