bblib 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,7 +32,7 @@ module BBLib
32
32
 
33
33
  # Used to move the position of the articles 'the', 'a' and 'an' in strings for normalization.
34
34
  def self.move_articles str, position = :front, capitalize: true
35
- return str unless [:front, :back, :none].include? position
35
+ return str unless [:front, :back, :none].include?(position)
36
36
  articles = ["the", "a", "an"]
37
37
  articles.each do |a|
38
38
  starts, ends = str.downcase.start_with?(a + ' '), str.downcase.end_with?(' ' + a)
@@ -52,8 +52,8 @@ module BBLib
52
52
  end
53
53
  end
54
54
  while str.strip.end_with?(',')
55
- str.strip!
56
- str.chop!
55
+ str = str.strip
56
+ str = str.chop
57
57
  end
58
58
  str
59
59
  end
@@ -101,7 +101,7 @@ class String
101
101
  end
102
102
 
103
103
  def to_clean_sym
104
- self.strip.downcase.gsub('_', ' ').drop_symbols.gsub(' ', '_').to_sym
104
+ self.snake_case.to_sym
105
105
  end
106
106
 
107
107
  # Simple method to convert a string into an array containing only itself
@@ -110,13 +110,43 @@ class String
110
110
  end
111
111
 
112
112
  def encap_by? str
113
- self.start_with?(str) && self.end_with?(str)
113
+ case str
114
+ when '('
115
+ self.start_with?(str) && self.end_with?(')')
116
+ when '['
117
+ self.start_with?(str) && self.end_with?(']')
118
+ when '{'
119
+ self.start_with?(str) && self.end_with?('}')
120
+ when '<'
121
+ self.start_with?(str) && self.end_with?('>')
122
+ else
123
+ self.start_with?(str) && self.end_with?(str)
124
+ end
125
+ end
126
+
127
+ def uncapsulate char = '"'
128
+ case char
129
+ when '('
130
+ back = ')'
131
+ when '['
132
+ back = ']'
133
+ when '{'
134
+ back = '}'
135
+ when '<'
136
+ back = '>'
137
+ else
138
+ back = char
139
+ end
140
+ temp = self.dup
141
+ temp = temp[(char.size)..-1] while temp.start_with?(char) && temp != char
142
+ temp = temp[0..-(char.size + 1)] while temp.end_with?(back) && temp != char
143
+ temp
114
144
  end
115
145
 
116
146
  end
117
147
 
118
148
  class Symbol
119
149
  def to_clean_sym
120
- self.to_s.strip.downcase.gsub('_', ' ').drop_symbols.gsub(' ', '_').to_sym
150
+ self.to_s.to_clean_sym
121
151
  end
122
152
  end
@@ -45,7 +45,7 @@ module BBLib
45
45
  end
46
46
 
47
47
  def self.delimited_case str, delimiter = '_'
48
- regx = /[[:space:]]+|[^[[:alnum:]]]+|#{delimiter}+/
48
+ regx = /[[:space:]]+|[^[[:alnum:]]]+|\#{delimiter}+/
49
49
  words = str.split(regx).join(delimiter)
50
50
  end
51
51
 
@@ -1,15 +1,10 @@
1
1
 
2
2
  module BBLib
3
3
 
4
- class FuzzyMatcher
5
- attr_reader :threshold, :algorithms
6
- attr_accessor :case_sensitive, :remove_symbols, :move_articles, :convert_roman, :a, :b
7
-
8
- def initialize threshold: 75, case_sensitive: true, remove_symbols: false, move_articles: false, convert_roman: true
9
- self.threshold = threshold
10
- setup_algorithms
11
- @case_sensitive, @remove_symbols, @move_articles, @convert_roman = case_sensitive, remove_symbols, move_articles, convert_roman
12
- end
4
+ class FuzzyMatcher < LazyClass
5
+ attr_float_between 0, 100, :threshold, default: 75
6
+ attr_bool :case_sensitive, default: true
7
+ attr_bool :remove_symbols, :move_articles, :convert_roman, default: false
13
8
 
14
9
  # Calculates a percentage match between string a and string b.
15
10
  def similarity a, b
@@ -40,10 +35,6 @@ module BBLib
40
35
  sort ? matches.sort_by{ |k, v| v }.reverse.to_h : matches
41
36
  end
42
37
 
43
- def threshold= threshold
44
- @threshold = BBLib.keep_between(threshold, 0, 100)
45
- end
46
-
47
38
  def set_weight algorithm, weight
48
39
  return nil unless @algorithms.include? algorithm
49
40
  @algorithms[algorithm][:weight] = BBLib.keep_between(weight, 0, nil)
@@ -55,7 +46,7 @@ module BBLib
55
46
 
56
47
  private
57
48
 
58
- def setup_algorithms
49
+ def lazy_setup
59
50
  @algorithms = {
60
51
  levenshtein: {weight: 10, signature: :levenshtein_similarity},
61
52
  composition: {weight: 5, signature: :composition_similarity},
@@ -66,11 +57,15 @@ module BBLib
66
57
  end
67
58
 
68
59
  def prep_strings a, b
69
- @a, @b = a.to_s.dup.strip, b.to_s.dup.strip
70
- if !@case_sensitive then @a.downcase!; @b.downcase! end
71
- if @remove_symbols then @a.drop_symbols!; @b.drop_symbols! end
72
- if @convert_roman then @a.from_roman!; @b.from_roman! end
73
- if @move_articles then @a.move_articles!(:front, @case_sensitive); @b.move_articles! :front, @case_sensitive end
60
+ @a, @b = a.to_s.dup, b.to_s.dup
61
+ methods = [
62
+ @case_sensitive ? nil : :downcase,
63
+ @remove_symbols ? :drop_symbols : nil,
64
+ @convert_roman ? :from_roman : nil,
65
+ @move_articles ? :move_articles : nil
66
+ ].reject(&:nil?).each do |method|
67
+ @a, @b = @a.send(method), @b.send(method)
68
+ end
74
69
  end
75
70
 
76
71
  end
@@ -30,7 +30,7 @@ module BBLib
30
30
  a.chars.each do |c|
31
31
  if temp.chars.include? c
32
32
  matches+=1
33
- temp.sub! c, ''
33
+ temp = temp.sub(c, '')
34
34
  end
35
35
  end
36
36
  (matches / [a.length, b.length].max.to_f )* 100.0
@@ -1,7 +1,7 @@
1
1
 
2
2
  module BBLib
3
3
 
4
- # Converts any integer up to 1000 to a roman numeral string_a
4
+ # Converts any integer up to 1000 to a roman numeral
5
5
  def self.to_roman num
6
6
  return num.to_s if num > 1000
7
7
  roman = {1000 => 'M', 900 => 'CM', 500 => 'D', 400 => 'CD', 100 => 'C', 90 => 'XC', 50 => 'L',
@@ -18,14 +18,13 @@ module BBLib
18
18
 
19
19
  def self.string_to_roman str
20
20
  sp = str.split ' '
21
- sp.map! do |s|
21
+ sp.map do |s|
22
22
  if s.drop_symbols.to_i.to_s == s.drop_symbols && !(s =~ /\d+\.\d+/)
23
- s.sub!(s.scan(/\d+/).first.to_s, BBLib.to_roman(s.to_i))
23
+ s = s.sub(s.scan(/\d+/).first.to_s, BBLib.to_roman(s.to_i))
24
24
  else
25
25
  s
26
26
  end
27
- end
28
- sp.join ' '
27
+ end.join ' '
29
28
  end
30
29
 
31
30
 
@@ -36,7 +35,7 @@ module BBLib
36
35
  if !sp.select{ |i| i[/#{num}/i]}.empty?
37
36
  for i in 0..(sp.length-1)
38
37
  if sp[i].drop_symbols.upcase == num
39
- sp[i].sub!(num ,n.to_s)
38
+ sp[i] = sp[i].sub(num ,n.to_s)
40
39
  end
41
40
  end
42
41
  end
@@ -3,8 +3,8 @@ module BBLib
3
3
  class Cron
4
4
  attr_reader :exp, :parts, :time
5
5
 
6
- def initialize exp
7
- @parts = Hash.new
6
+ def initialize exp = '* * * * * *'
7
+ @parts = Hash.new
8
8
  self.exp = exp
9
9
  end
10
10
 
@@ -61,7 +61,7 @@ module BBLib
61
61
  exp = exp.to_s.downcase
62
62
  REPLACE.each do |k, v|
63
63
  v.each do |r|
64
- exp.gsub!(r.to_s, k.to_s)
64
+ exp = exp.gsub(r.to_s, k.to_s)
65
65
  end
66
66
  end
67
67
  exp
@@ -69,8 +69,8 @@ module BBLib
69
69
 
70
70
  def parse_cron_numbers exp, min, max, qmark
71
71
  numbers = Array.new
72
- exp = Cron.numeralize(exp)
73
- exp.gsub!('?', qmark.to_s)
72
+ exp = Cron.numeralize(exp)
73
+ exp = exp.gsub('?', qmark.to_s)
74
74
  exp.scan(/\*\/\d+|\d+\/\d+|\d+-\d+\/\d+/).each do |s|
75
75
  range, divisor = s.split('/').first, s.split('/').last.to_i
76
76
  if range == '*'
@@ -87,15 +87,14 @@ module BBLib
87
87
  end
88
88
  index+=1
89
89
  end
90
- exp.sub!(s, '')
90
+ exp = exp.sub(s, '')
91
91
  end
92
92
  numbers.push exp.scan(/\d+/).map{ |m| m.to_i }
93
93
  exp.strip.scan(/\d+\-\d+/).each do |e|
94
94
  nums = e.scan(/\d+/).map{ |n| n.to_i }
95
95
  numbers.push (nums.min..nums.max).map{ |n| n }
96
96
  end
97
- numbers.flatten!.sort!
98
- numbers.uniq.reject{ |r| r < min || r > max }
97
+ numbers.flatten.sort.uniq.reject{ |r| r < min || r > max }
99
98
  end
100
99
 
101
100
  def next_day time, direction
@@ -134,36 +133,37 @@ module BBLib
134
133
  end
135
134
 
136
135
  PARTS = {
137
- minute: {send: :min, min:0, max:59, size: 60},
138
- hour: {send: :hour, min:0, max:23, size: 60*60},
139
- day: {send: :day, min:1, max:31, size: 60*60*24},
140
- month: {send: :month, min:1, max:12},
136
+ minute: {send: :min, min:0, max:59, size: 60},
137
+ hour: {send: :hour, min:0, max:23, size: 60*60},
138
+ day: {send: :day, min:1, max:31, size: 60*60*24},
139
+ month: {send: :month, min:1, max:12},
141
140
  weekday: {send: :wday, min:0, max:6},
142
- year: {send: :year, min:0, max:90000}
141
+ year: {send: :year, min:0, max:90000}
143
142
  }
144
143
 
145
144
  REPLACE = {
146
- 1 => [:sunday, :sun, :january, :jan],
147
- 2 => [:monday, :mon, :february, :feb],
148
- 3 => [:tuesday, :tues, :tue, :march, :mar],
149
- 4 => [:wednesday, :wednes, :wed, :april, :apr],
150
- 5 => [:thursday, :thurs, :thu, :may],
151
- 6 => [:friday, :fri, :june, :jun],
152
- 7 => [:saturday, :sat, :july, :jul],
153
- 8 => [:august, :aug],
154
- 9 => [:september, :sept, :sep],
145
+ 0 => [:sunday, :sun],
146
+ 1 => [:monday, :mon, :january, :jan],
147
+ 2 => [:tuesday, :tues, :february, :feb],
148
+ 3 => [:wednesday, :wednes, :tue, :march, :mar],
149
+ 4 => [:thursday, :thurs, :wed, :april, :apr],
150
+ 5 => [:friday, :fri, :thu, :may],
151
+ 6 => [:saturday, :sat, :june, :jun],
152
+ 7 => [:july, :jul],
153
+ 8 => [:august, :aug],
154
+ 9 => [:september, :sept, :sep],
155
155
  10 => [:october, :oct],
156
156
  11 => [:november, :nov],
157
157
  12 => [:december, :dec]
158
158
  }
159
159
 
160
160
  SPECIAL_EXP = {
161
- '0 0 * * * *' => ['@daily', '@midnight', 'daily', 'midnight'],
161
+ '0 0 * * * *' => ['@daily', '@midnight', 'daily', 'midnight'],
162
162
  '0 12 * * * *' => ['@noon', 'noon'],
163
- '0 0 * * 0 *' => ['@weekly', 'weekly'],
164
- '0 0 1 * * *' => ['@monthly', 'monthly'],
165
- '0 0 1 1 * *' => ['@yearly', '@annually', 'yearly', 'annually'],
166
- '? ? ? ? ? ?' => ['@reboot', '@restart', 'reboot', 'restart']
163
+ '0 0 * * 0 *' => ['@weekly', 'weekly'],
164
+ '0 0 1 * * *' => ['@monthly', 'monthly'],
165
+ '0 0 1 1 * *' => ['@yearly', '@annually', 'yearly', 'annually'],
166
+ '? ? ? ? ? ?' => ['@reboot', '@restart', 'reboot', 'restart']
167
167
  }
168
168
 
169
169
  end
@@ -1,17 +1,8 @@
1
1
  module BBLib
2
2
 
3
- class TaskTimer
4
- attr_reader :tasks, :save, :retention
5
-
6
- def initialize task:nil, retention:100
7
- @tasks = {}
8
- self.retention = retention
9
- if task then start task end
10
- end
11
-
12
- def retention= num
13
- @retention = num.nil? ? nil : BBLib.keep_between(num, -1, nil)
14
- end
3
+ class TaskTimer < LazyClass
4
+ attr_hash :tasks, default: Hash.new
5
+ attr_int_between -1, nil, :retention, default: 100
15
6
 
16
7
  def time task = :default, type = :current
17
8
  return nil unless @tasks.keys.include? task
@@ -65,37 +56,52 @@ module BBLib
65
56
  start(task) unless stop(task).nil?
66
57
  end
67
58
 
68
- def save= save
69
- @save = save
70
- end
71
-
72
59
  def active? task
73
60
  return false unless @tasks.keys.include? task
74
61
  !@tasks[task][:current].nil?
75
62
  end
76
63
 
77
- def method_missing *args
78
- temp = args.first.to_s.sub('p_','').to_sym
64
+ def stats task, pretty: false
65
+ return nil unless @tasks.include?(task)
66
+ stats = "#{task}" + "\n" + '-'*30 + "\n"
67
+ TIMER_TYPES.each do |k,v|
68
+ next if STATS_IGNORE.include?(k)
69
+ stats+= k.to_s.capitalize.ljust(10) + "#{self.send(k, task, pretty:pretty)}\n"
70
+ end
71
+ stats
72
+ end
73
+
74
+ def method_missing *args, **named
75
+ temp = args.first.to_sym
76
+ pretty = named.delete :pretty
79
77
  type, task = TIMER_TYPES.keys.find{ |k| k == temp || TIMER_TYPES[k].include?(temp) }, args[1] ||= :default
80
- raise NoMethodError unless type
78
+ return super unless type
81
79
  t = time task, type
82
- args.first.to_s.start_with?('p_') && type != :count ? t.to_duration : t
80
+ pretty && type != :count && t ? (t.is_a?(Array) ? t.map{|m| m.to_duration} : t.to_duration) : t
83
81
  end
84
82
 
85
83
  private
86
84
 
85
+ STATS_IGNORE = [:current, :all]
86
+
87
87
  TIMER_TYPES = {
88
88
  current: [],
89
- avg: [:average, :av],
90
- all: [:times],
91
- max: [:maximum, :largest],
92
- min: [:minimum, :smallest],
93
- sum: [],
94
- last: [:latest],
95
- first: [:initial],
96
- count: [:total]
89
+ count: [:total],
90
+ first: [:initial],
91
+ last: [:latest],
92
+ min: [:minimum, :smallest],
93
+ max: [:maximum, :largest],
94
+ avg: [:average, :av],
95
+ sum: [],
96
+ all: [:times]
97
97
  }
98
98
 
99
+ def lazy_init *args
100
+ if args.first.is_a?(Symbol)
101
+ start(args.first)
102
+ end
103
+ end
104
+
99
105
  end
100
106
 
101
107
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bblib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Black
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-04 00:00:00.000000000 Z
11
+ date: 2016-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: A library containing many reusable, basic functions.
56
70
  email:
57
71
  - d2sm10@hotmail.com
@@ -74,11 +88,19 @@ files:
74
88
  - lib/bblib.rb
75
89
  - lib/bblib/version.rb
76
90
  - lib/file/bbfile.rb
91
+ - lib/gem/bbgem.rb
77
92
  - lib/hash/bbhash.rb
78
93
  - lib/hash/hash_path.rb
79
94
  - lib/hash/hash_path_proc.rb
95
+ - lib/hash/path_hash.rb
80
96
  - lib/number/bbnumber.rb
97
+ - lib/object/attr.rb
81
98
  - lib/object/bbobject.rb
99
+ - lib/object/hooks.rb
100
+ - lib/object/lazy_class.rb
101
+ - lib/opal/bbopal.rb
102
+ - lib/os/bbos.rb
103
+ - lib/os/bbsys.rb
82
104
  - lib/string/bbstring.rb
83
105
  - lib/string/cases.rb
84
106
  - lib/string/fuzzy_matcher.rb