rmtools 2.2.4 → 2.2.6

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.
data/README.md CHANGED
@@ -27,6 +27,17 @@ It's still randomly documented since it's just my working tool.
27
27
 
28
28
  ### CHANGES
29
29
 
30
+ ##### Version 2.2.6
31
+
32
+ * String
33
+ * Added #utf? and #find_compatible_encoding. Currently, latter one differentiates only utf-8 and ansi-1251.
34
+
35
+ * Range
36
+ * #include? never raises an exception
37
+
38
+ * Array/Set
39
+ * Added #is_subset_of?
40
+
30
41
  ##### Version 2.2.4
31
42
 
32
43
  * Hash
@@ -117,7 +117,7 @@ module RMTools
117
117
  cmd = "scp #{fullpath.inspect} #{[@host, fpath].join(':').inspect} 2>&1"
118
118
  print "`#{cmd}`: " if @debug
119
119
  if res = RMTools::tick_while {`#{cmd}`}.b
120
- puts "[ #{Painter.rb('Error')} ]: #{res}"
120
+ puts "[ #{Painter.r_b('Error')} ]: #{res}"
121
121
  else
122
122
  print "[ #{Painter.g('OK')} ]"
123
123
  end
@@ -73,5 +73,9 @@ end
73
73
  def truth_map
74
74
  map_values {|v| !!v}
75
75
  end
76
+
77
+ def key_value
78
+ to_a.first
79
+ end
76
80
 
77
81
  end
@@ -132,7 +132,9 @@ class Range
132
132
  elsif Range === number_or_range
133
133
  include_number? number_or_range.first and include_number? number_or_range.last
134
134
  else
135
- raise TypeError, "can not find #{number_or_range.class} in Range"
135
+ #raise TypeError, "can not find #{number_or_range.class} in Range"
136
+ # activerecord 4.0 tells it must not raise
137
+ false
136
138
  end
137
139
  end
138
140
 
@@ -14,7 +14,8 @@ module RMTools
14
14
  alias :coallition :+
15
15
  alias :subtraction :-
16
16
  alias :intersection :&
17
- protected :union, :coallition, :subtraction, :intersection
17
+ # :& won't work this way if :intersection will be protected
18
+ protected :union, :coallition, :subtraction
18
19
 
19
20
  def |(ary)
20
21
  return ary.uniq if empty?
@@ -55,12 +56,17 @@ module RMTools
55
56
  [self - common, ary - common]
56
57
  end
57
58
 
58
- alias diff ^
59
+ alias :diff :^
59
60
 
60
61
  def intersects?(ary)
61
62
  (self & ary).any?
62
63
  end
63
64
  alias :x? :intersects?
65
+
66
+ def =~(ary)
67
+ (self - ary).empty?
68
+ end
69
+ alias :is_subset_of? :=~
64
70
  }
65
71
  super
66
72
  end
@@ -3,7 +3,8 @@ require 'iconv'
3
3
  # Although ruby >= 1.9.3 would complain about not using String#encode, iconv is 2-4 times faster and still handles the ruby string encoding
4
4
 
5
5
  module RMTools
6
-
6
+ ENCODINGS_PATTERNS = {}
7
+
7
8
  module Cyrillic
8
9
  RU_LETTERS = "абвгдеёжзийклмнопрстуфхцчшщьыъэюя", "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЫЪЭЮЯ"
9
10
 
@@ -15,29 +16,52 @@ module RMTools
15
16
  ANSI_YOYE.force_encodings "Windows-1251"
16
17
  ANSI_ENCODING = ANSI_LETTERS_UC[0].encoding
17
18
  end
18
- end
19
19
 
20
+ ENCODINGS_PATTERNS["WINDOWS-1251"] = /[#{ANSI_LETTERS_UC.join}]/
21
+ end
22
+
23
+ ICONVS = {}
20
24
  ANSI2UTF = Cyrillic::ANSI2UTF = Iconv.new("UTF-8//IGNORE", "WINDOWS-1251//IGNORE").method(:iconv)
21
25
  UTF2ANSI = Cyrillic::UTF2ANSI = Iconv.new("WINDOWS-1251//IGNORE", "UTF-8//IGNORE").method(:iconv)
22
- ICONVS = {}
23
26
  end
24
27
 
25
28
  class String
26
29
 
27
- def utf(from_encoding='WINDOWS-1251//IGNORE')
30
+ def utf(from_encoding="#{encoding.name.upcase}//IGNORE")
28
31
  (ICONVS['UTF-8<'+from_encoding] ||= Iconv.new('UTF-8//IGNORE', from_encoding)).iconv(self)
29
32
  end
30
33
 
31
- def ansi(from_encoding='UTF-8//IGNORE')
34
+ def ansi(from_encoding="#{encoding.name.upcase}//IGNORE")
32
35
  (ICONVS['WINDOWS-1251<'+from_encoding] ||= Iconv.new('WINDOWS-1251//IGNORE', from_encoding)).iconv(self)
33
36
  end
34
37
 
35
- def utf!(from_encoding='WINDOWS-1251//IGNORE')
38
+ def utf!(from_encoding="#{encoding.name.upcase}//IGNORE")
36
39
  replace utf from_encoding
37
40
  end
38
41
 
39
- def ansi!(from_encoding='UTF-8//IGNORE')
42
+ def ansi!(from_encoding="#{encoding.name.upcase}//IGNORE")
40
43
  replace ansi from_encoding
41
44
  end
42
45
 
46
+
47
+ def utf?
48
+ begin
49
+ encoding == Encoding::UTF_8 and self =~ /./u
50
+ rescue Encoding::CompatibilityError
51
+ false
52
+ end
53
+ end
54
+
55
+ def find_compatible_encoding
56
+ # UTF-8 by default
57
+ return nil if utf?
58
+ for enc, pattern in ENCODINGS_PATTERNS
59
+ force_encoding(enc)
60
+ if self =~ pattern
61
+ return enc
62
+ end
63
+ end
64
+ false
65
+ end
66
+
43
67
  end
@@ -1,3 +1,3 @@
1
1
  module RMTools
2
- VERSION = '2.2.4'
2
+ VERSION = '2.2.6'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rmtools
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.4
4
+ version: 2.2.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-19 00:00:00.000000000 Z
12
+ date: 2013-08-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport