rdoba 0.9.1 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +7 -7
  2. data/.gitignore +4 -0
  3. data/.travis.yml +28 -0
  4. data/CHANGES.md +6 -0
  5. data/Gemfile +5 -0
  6. data/README.md +87 -108
  7. data/Rakefile +62 -54
  8. data/TODO +6 -0
  9. data/features/mixin.feature +85 -0
  10. data/features/step_definitions/mixin_steps.rb +305 -0
  11. data/features/support/env.rb +35 -145
  12. data/features/support/mixin_support.rb +17 -0
  13. data/html/.keep +0 -0
  14. data/lib/rdoba/_version_.rb +3 -1
  15. data/lib/rdoba/a.rb +44 -42
  16. data/lib/rdoba/bcd.rb +43 -26
  17. data/lib/rdoba/blank.rb +14 -0
  18. data/lib/rdoba/combinations.rb +17 -15
  19. data/lib/rdoba/common.rb +53 -68
  20. data/lib/rdoba/debug.rb +9 -3
  21. data/lib/rdoba/deploy.rb +55 -50
  22. data/lib/rdoba/dup.rb +31 -31
  23. data/lib/rdoba/fe.rb +6 -5
  24. data/lib/rdoba/gem.rb +33 -29
  25. data/lib/rdoba/hashorder.rb +24 -24
  26. data/lib/rdoba/io.rb +81 -74
  27. data/lib/rdoba/merge.rb +21 -0
  28. data/lib/rdoba/mixin/time.rb +17 -0
  29. data/lib/rdoba/mixin/try.rb +11 -0
  30. data/lib/rdoba/mixin/try_1_9_0.rb +9 -0
  31. data/lib/rdoba/mixin/wait_if.rb +27 -0
  32. data/lib/rdoba/mixin.rb +373 -52
  33. data/lib/rdoba/numeric.rb +19 -17
  34. data/lib/rdoba/os.rb +127 -0
  35. data/lib/rdoba/re.rb +4 -4
  36. data/lib/rdoba/require.rb +24 -19
  37. data/lib/rdoba/roman.rb +32 -22
  38. data/lib/rdoba/strings.rb +6 -144
  39. data/lib/rdoba/yaml.rb +20 -18
  40. data/lib/rdoba.rb +50 -47
  41. data/rdoba.gemspec +33 -26
  42. data/tddium.yml +11 -0
  43. metadata +184 -77
  44. data/features/bcd.feature +0 -29
  45. data/features/log.feature +0 -206
  46. data/features/step_definitions/bcd_steps.rb +0 -69
  47. data/features/step_definitions/log_steps.rb +0 -164
  48. data/lib/rdoba/log.rb +0 -248
  49. data/test/helper.rb +0 -18
  50. data/test/rdoba_test.rb.stub +0 -59
  51. data/test/test_rdoba.rb +0 -7
data/lib/rdoba/bcd.rb CHANGED
@@ -1,61 +1,78 @@
1
- #encoding: utf-8
2
- #
1
+ # frozen_string_literal: true
3
2
 
4
3
  module BCD
5
- class ParseError < Exception
6
- def initialize msg = "Invalid positive integer value"; end; end
4
+ class ParseError < RuntimeError
5
+ def initialize(msg = 'Invalid positive integer value'); end
6
+ end
7
7
 
8
- class ConvertError < Exception
9
- def initialize msg = "Invalid number has given"; end; end
8
+ class ConvertError < RuntimeError
9
+ def initialize(msg = 'Invalid number has given'); end
10
+ end
10
11
 
11
12
  def to_i
12
13
  res = 0
13
14
  mul = 1
14
- self.each_byte do |c|
15
- def pow value, mul
15
+ each_byte do |c|
16
+ def pow(value, mul)
16
17
  if value >= 10
17
- raise ConvertError; end
18
+ raise ConvertError
19
+ end
20
+
18
21
  value * mul
19
22
  end
20
- res += pow( c.ord & 0xF, mul )
23
+ res += pow(c.ord & 0xF, mul)
21
24
  mul *= 10
22
- res += pow( c.ord >> 4, mul )
25
+ res += pow(c.ord >> 4, mul)
23
26
  mul *= 10
24
27
  end
25
- res; end
28
+ res
29
+ end
26
30
 
27
- def self.parse value
31
+ def self.parse(value)
28
32
  if value < 0
29
- raise ParseError; end
33
+ raise ParseError
34
+ end
35
+
30
36
  res = BCDString.new
31
37
  if res.respond_to? :force_encoding
32
- res.force_encoding( 'ASCII-8BIT' ); end
38
+ res.force_encoding('ASCII-8BIT')
39
+ end
33
40
  if value > 0
34
41
  part = nil
35
42
  while value > 0
36
- ( value, mod ) = value.divmod 10
43
+ value, mod = value.divmod 10
37
44
  if part
38
- res << ( ( mod << 4 ) | part ).chr
45
+ res << ((mod << 4) | part).chr
39
46
  part = nil
40
47
  else
41
- part = mod; end; end
48
+ part = mod
49
+ end
50
+ end
42
51
  if part
43
- res << part.chr; end
52
+ res << part.chr
53
+ end
44
54
  else
45
- res << 0.chr; end
46
- res; end; end
55
+ res << 0.chr
56
+ end
57
+ res
58
+ end
59
+ end
47
60
 
48
61
  class BCDString < String
49
62
  include BCD
50
63
 
51
- def initialize value = nil
64
+ def initialize(value = nil)
52
65
  if value.is_a? Numeric
53
- self.replace BCD.parse( value )
66
+ replace BCD.parse(value)
54
67
  else
55
- super value.to_s; end; end; end
68
+ super value.to_s
69
+ end
70
+ end
71
+ end
56
72
 
57
73
  class Numeric
58
74
  def to_bcd
59
75
  # NOTE: return as plain LSB string
60
- BCD.parse value; end; end
61
-
76
+ BCD.parse value
77
+ end
78
+ end
@@ -0,0 +1,14 @@
1
+ class Object
2
+ def blank?
3
+ case self
4
+ when NilClass, FalseClass
5
+ true
6
+ when TrueClass
7
+ false
8
+ when Hash, Array
9
+ !self.any?
10
+ else
11
+ self.to_s == ""
12
+ end
13
+ end
14
+ end
@@ -1,12 +1,12 @@
1
1
  #!/usr/bin/ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  class Array
4
-
5
- private
5
+ private
6
6
 
7
7
  def __comby(i, size)
8
- s = "0#{sprintf("%.*b", size, i)}0"
9
- v = { :res => [], :c0 => 0, :c1 => 0, :j => 0}
8
+ s = "0#{format('%.*b', size, i)}0"
9
+ v = { res: [], c0: 0, c1: 0, j: 0 }
10
10
 
11
11
  def up1(v)
12
12
  sub_j = v[:j] + v[:c1] + 1
@@ -17,7 +17,9 @@ private
17
17
 
18
18
  def up0(v)
19
19
  sub_j = v[:j] + v[:c0] - 1
20
- self[v[:j]...sub_j].each do |x| v[:res] << [x] end
20
+ self[v[:j]...sub_j].each do |x|
21
+ v[:res] << [x]
22
+ end
21
23
  v[:j] = sub_j
22
24
  end
23
25
 
@@ -35,25 +37,25 @@ private
35
37
  v[:res]
36
38
  end
37
39
 
38
- public
40
+ public
39
41
 
40
42
  def each_comby(*args)
41
- return self if self.empty? or not block_given?
43
+ return self if empty? or !block_given?
44
+
42
45
  if args.include?(:backward)
43
- yield [ self.dup ]
44
- ((1 << (self.size - 1)) - 2).downto(0) do |i|
45
- c = __comby(i, self.size - 1)
46
+ yield [dup]
47
+ ((1 << (size - 1)) - 2).downto(0) do |i|
48
+ c = __comby(i, size - 1)
46
49
  yield c
47
50
  end
48
51
  else
49
- 0.upto((1 << (self.size - 1)) - 2) do |i|
50
- c = __comby(i, self.size - 1)
52
+ 0.upto((1 << (size - 1)) - 2) do |i|
53
+ c = __comby(i, size - 1)
51
54
  yield c
52
55
  end
53
- yield [ self.dup ]
56
+ yield [dup]
54
57
  end
55
58
 
56
- return self
59
+ self
57
60
  end
58
61
  end
59
-
data/lib/rdoba/common.rb CHANGED
@@ -1,13 +1,14 @@
1
1
  #!/usr/bin/ruby -KU
2
- #coding:utf-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  class Object
5
5
  def xor(val1)
6
- val0 = (not not self)
7
- ((val0) and (not val1)) or ((not val0) and (val1))
6
+ val0 = !!self
7
+ (val0 and !val1) or (!val0 and val1)
8
8
  end
9
9
 
10
- def co(method, *args) #calls any method
10
+ # calls any method
11
+ def co(method, *_args)
11
12
  eval "#{method}(*args)"
12
13
  end
13
14
 
@@ -20,49 +21,36 @@ class Object
20
21
  opts.each do |opt|
21
22
  case opt.class.to_s.to_sym
22
23
  when :Hash
23
- opt.each do |x,y| v[x] = y end
24
+ opt.each do |x, y|
25
+ v[x] = y
26
+ end
24
27
  when :Array
25
- opt.each do |x| v[x] = true end
28
+ opt.each do |x|
29
+ v[x] = true
30
+ end
26
31
  when :Symbol
27
- v[opt] = true
32
+ v[opt] = true
28
33
  end
29
34
  end
30
35
  v
31
36
  end
32
37
 
33
38
  def apply_opts(opts)
34
- parse_opts(opts).each do |x,y|
35
- self.instance_variable_set("@#{x}".to_sym, y)
36
- end
37
- end
38
- end
39
-
40
- module Kernel
41
- def wait_if(timeout = 30)
42
- require 'timeout'
43
-
44
- begin
45
- Timeout::timeout(timeout) do
46
- while yield(); end
47
- end
48
- true
49
- rescue Timeout::Error
50
- false
51
- end
39
+ parse_opts(opts).each do |x, y| instance_variable_set("@#{x}".to_sym, y)end
52
40
  end
53
41
  end
54
42
 
55
43
  class NilClass
56
44
  def =~(value)
57
- value == nil
45
+ value.nil?
58
46
  end
59
47
 
60
- def +(value)
61
- value
48
+ def +(other)
49
+ other
62
50
  end
63
51
 
64
52
  def <<(value)
65
- [ value ]
53
+ [value]
66
54
  end
67
55
 
68
56
  def empty?
@@ -72,59 +60,58 @@ class NilClass
72
60
  def to_i
73
61
  0
74
62
  end
75
- alias :ord :to_i
63
+ alias ord to_i
76
64
 
77
65
  def size
78
66
  0
79
67
  end
80
68
 
81
- def <=>(value)
69
+ def <=>(_other)
82
70
  -1
83
71
  end
84
72
  end
85
73
 
86
-
87
74
  class Array
88
75
  def purge
89
- self.compact.delete_if {|x| x.empty? }
76
+ compact.delete_if { |x| x.empty? }
90
77
  end
91
78
 
92
79
  def >>(value = nil)
93
80
  value ? delete(value) : shift
94
81
  end
95
82
 
96
- alias :__get__ :[]
83
+ alias __get__ []
97
84
  def [](index, *args)
98
- return __get__(index.to_i, *args) if index.class == String and index =~ /^\d+$/
85
+ return __get__(index.to_i, *args) if index.instance_of?(String) and index =~ /^\d+$/
86
+
99
87
  __get__(index, *args)
100
88
  end
101
89
 
102
- alias :__set__ :[]=
90
+ alias __set__ []=
103
91
  def []=(index, value, *args)
104
- return __set__(index.to_i, value, *args) if index.class == String and index =~ /^\d+$/
92
+ return __set__(index.to_i, value, *args) if index.instance_of?(String) and index =~ /^\d+$/
93
+
105
94
  __set__(index, value, *args)
106
95
  end
107
96
  end
108
97
 
109
98
  class String
110
- def -(str)
111
- #TODO make smart search for match in the 'str', when only last subpart matched to 'self'
112
- len = self.size
99
+ def -(other)
100
+ # TODO: make smart search for match in the 'str', when only last subpart matched to 'self'
101
+ len = size
113
102
  bc = ec = nil
114
103
  (0...len).each do |idx|
115
- break bc = idx if self[idx] == str[0]
104
+ break bc = idx if self[idx] == other[0]
116
105
  end
117
- ((bc + 1)...len).each do |idx|
118
- break ec = idx if self[idx] != str[idx - bc]
119
- end if bc
120
- (not bc) ? self.clone : (not ec) ? self[0, bc] : self[0, bc] + self[ec, len - ec]
106
+ ((bc + 1)...len).each do |idx| break ec = idx if self[idx] != other[idx - bc]end if bc
107
+ bc ? ec ? self[0, bc] + self[ec, len - ec] : self[0, bc] : clone
121
108
  end
122
109
 
123
- alias :__match__ :=~
110
+ alias __match__ =~
124
111
  def =~(value)
125
- if value.class == String
112
+ if value.instance_of?(String)
126
113
  self == value
127
- elsif value.class == Regexp
114
+ elsif value.instance_of?(Regexp)
128
115
  value =~ self
129
116
  else
130
117
  __match__(value)
@@ -132,14 +119,14 @@ class String
132
119
  end
133
120
 
134
121
  def rmatch(value)
135
- self == value || self =~ /^\/([^\/]+)/ && value =~ /#{$1}/
122
+ self == value || self =~ %r{^/([^/]+)} && value =~ /#{Regexp.last_match(1)}/
136
123
  end
137
124
 
138
125
  def hexdump
139
- res= ''
126
+ res = ''
140
127
  i = 0
141
- self.each_byte do |byte|
142
- res << sprintf("%.2X ", byte)
128
+ each_byte do |byte|
129
+ res << format('%.2X ', byte)
143
130
  i += 1
144
131
  res << "\n" if i % 16 == 0
145
132
  end
@@ -148,13 +135,13 @@ class String
148
135
  end
149
136
 
150
137
  class Hash
151
- def |(inval)
152
- res = self.dup
153
- inval.each_pair do |key, val|
154
- if val.class == res[key].class
155
- if val.class == Hash
156
- res[key] |= inval[key]
157
- elsif val.class == Array
138
+ def |(other)
139
+ res = dup
140
+ other.each_pair do |key, val|
141
+ if val.instance_of?(res[key].class)
142
+ if val.instance_of?(Hash)
143
+ res[key] |= other[key]
144
+ elsif val.instance_of?(Array)
158
145
  res[key].concat val
159
146
  else
160
147
  res[key] = val
@@ -172,19 +159,17 @@ class Hash
172
159
 
173
160
  def reverse
174
161
  h = {}
175
- self.each_pair do |key, value|
162
+ each_pair do |key, value|
176
163
  if h.key? value
177
- if h[value].class == Array
178
- h[value] << key
179
- else
180
- h[value] = [ h[value], key ]
181
- end
164
+ if h[value].instance_of?(Array)
165
+ h[value] << key
166
+ else
167
+ h[value] = [h[value], key]
168
+ end
182
169
  else
183
- h[value] = key
170
+ h[value] = key
184
171
  end
185
172
  end
186
173
  h
187
174
  end
188
175
  end
189
-
190
-
data/lib/rdoba/debug.rb CHANGED
@@ -1,6 +1,12 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
3
+ warn "Warning: the module has kept only for backward compatibility\n" \
4
+ "Please use 'rdoba :log' form instead"
2
5
 
3
- STDERR.puts "Warning: the module has kept only for backward compatibility\n" +
4
- "Please use 'rdoba :log' form instead"
5
6
  require 'rdoba/log'
6
7
 
8
+ module Rdoba
9
+ def self.debug(options = {})
10
+ Rdoba.log options
11
+ end
12
+ end
data/lib/rdoba/deploy.rb CHANGED
@@ -1,74 +1,80 @@
1
1
  #!/usr/bin/ruby -KU
2
- #coding:utf-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  require 'rdoba/common'
5
5
 
6
6
  class Hash
7
-
8
7
  def deploy!(vars = {})
9
- self.replace deploy(vars)
10
- # TODO add variable copy
8
+ replace deploy(vars)
9
+ # TODO: add variable copy
11
10
  end
12
11
 
13
12
  def deploy(vars = {})
14
13
  res = {}
15
14
 
16
- self.keys.sort do |x,y|
17
- if x =~ /=$/
18
- y =~ /=$/ ? x <=> y : -1
15
+ keys.sort do |x, y|
16
+ if /=$/.match?(x)
17
+ /=$/.match?(y) ? x <=> y : -1
19
18
  else
20
- y !~ /=$/ ? x <=> y : 1
19
+ /=$/.match?(y) ? 1 : x <=> y
21
20
  end
22
21
  end.each do |key|
23
-
24
22
  if key =~ /(.*)=$/
25
- vars[$1] = self[key]
26
- next
27
-
23
+ vars[Regexp.last_match(1)] = self[key]
24
+ next
28
25
  elsif key =~ /(.*)@$/
29
- sym = $1
30
- eval "res.class.co( :attr_accessor, :#{sym})"
31
- eval "res.#{sym} = self[key]"
32
- next
33
-
26
+ sym = Regexp.last_match(1)
27
+ eval "res.class.co( :attr_accessor, :#{sym})"
28
+ eval "res.#{sym} = self[key]"
29
+ next
34
30
  elsif key =~ /^%([^%].*)/
35
- next $stderr.puts "Warning: undefined variable " +
36
- "#{$1.inspect} found. Ignoring..." unless vars.key?($1)
37
- var = vars[$1].dup
38
- if var.class == Hash
39
- res |= var.deploy(vars)
40
- elsif var.class == String
41
- res[var] = nil
42
- else
43
- raise "Undeployable hash #{$1} value class #{var.class}"
44
- end
45
- next
31
+ unless vars.key?(Regexp.last_match(1))
32
+ next warn 'Warning: undefined variable ' + "#{Regexp.last_match(1).inspect} found. Ignoring..."
33
+ end
46
34
 
35
+ var = vars[Regexp.last_match(1)].dup
36
+ if var.instance_of?(Hash)
37
+ res |= var.deploy(vars)
38
+ elsif var.instance_of?(String)
39
+ res[var] = nil
40
+ else
41
+ raise "Undeployable hash #{Regexp.last_match(1)} value class #{var.class}"
42
+ end
43
+ next
47
44
  elsif key =~ /^%%(.*)/
48
- key.replace $1.to_s
45
+ key.replace Regexp.last_match(1).to_s
49
46
  end
50
47
 
51
48
  def deploy_value(value, vars)
52
- case value.class.to_sym
53
- when :String
54
- if value =~ /^%([^%].*)/
55
- begin; vars[$1].deploy(vars); rescue; nil end
56
- elsif value =~ /(.*)%([A-Za-z0-9_А-я]+)(.*)/
57
- a = [ $1.to_s, $2.to_s, $3.to_s ]
58
- a[1] = begin; vars[a[1]].deploy(vars).to_s; rescue; vars[a[1]] end
59
- a.join
60
- else
61
- value
62
- end
63
- when :Hash
64
- value.deploy(vars)
65
- when :Array
66
- value.map do |sub|
67
- deploy_value(sub, vars)
68
- end
69
- else
70
- value
71
- end
49
+ case value.class.to_sym
50
+ when :String
51
+ if value =~ /^%([^%].*)/
52
+ begin
53
+ vars[Regexp.last_match(1)].deploy(vars)
54
+ rescue StandardError
55
+ nil
56
+ end
57
+ elsif value =~ /(.*)%([A-Za-z0-9_А-я]+)(.*)/
58
+ a = [Regexp.last_match(1).to_s, Regexp.last_match(2).to_s, Regexp.last_match(3).to_s]
59
+ a[1] =
60
+ begin
61
+ vars[a[1]].deploy(vars).to_s
62
+ rescue StandardError
63
+ vars[a[1]]
64
+ end
65
+ a.join
66
+ else
67
+ value
68
+ end
69
+ when :Hash
70
+ value.deploy(vars)
71
+ when :Array
72
+ value.map do |sub|
73
+ deploy_value(sub, vars)
74
+ end
75
+ else
76
+ value
77
+ end
72
78
  end
73
79
 
74
80
  value = self[key]
@@ -77,4 +83,3 @@ class Hash
77
83
  res
78
84
  end
79
85
  end
80
-
data/lib/rdoba/dup.rb CHANGED
@@ -1,64 +1,64 @@
1
1
  #!/usr/bin/ruby -KU
2
- #coding:utf-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  class Array
5
- alias :__dup__ :dup
5
+ alias __dup__ dup
6
6
  def dup(opts = {})
7
- if (opts.class == Hash ? opts.key?(:recursive) : opts.to_s.to_sym == :recursive)
7
+ if opts.instance_of?(Hash) ? opts.key?(:recursive) : opts.to_s.to_sym == :recursive
8
8
  res = []
9
9
 
10
10
  def sub_dup(value)
11
- if value.class.to_s =~ /(Hash|Array)/
12
- value.dup(:recursive)
13
- else
14
- begin
15
- value.dup
16
- rescue
17
- new = value
18
- end
19
- end
11
+ if /(Hash|Array)/.match?(value.class.to_s)
12
+ value.dup(:recursive)
13
+ else
14
+ begin
15
+ value.dup
16
+ rescue StandardError
17
+ new = value
18
+ end
19
+ end
20
20
  end
21
21
 
22
- self.each do |value| res << sub_dup(value) end
22
+ each do |value|
23
+ res << sub_dup(value)
24
+ end
23
25
 
24
26
  res
25
27
  elsif opts.empty?
26
28
  __dup__
27
29
  else
28
- raise "Unsupported option(s): #{opts.class == Hash ? opts.keys.join(', ') : opts}"
30
+ raise "Unsupported option(s): #{opts.instance_of?(Hash) ? opts.keys.join(', ') : opts}"
29
31
  end
30
32
  end
31
-
32
33
  end
33
34
 
34
35
  class Hash
35
- alias :__dup__ :dup
36
+ alias __dup__ dup
36
37
  def dup(opts = {})
37
- if (opts.class == Hash ? opts.key?(:recursive) : opts.to_s.to_sym == :recursive)
38
+ if opts.instance_of?(Hash) ? opts.key?(:recursive) : opts.to_s.to_sym == :recursive
38
39
  res = {}
39
40
 
40
41
  def sub_dup(value)
41
- if value.class.to_s =~ /(Hash|Array)/
42
- value.dup(:recursive)
43
- else
44
- begin
45
- value.dup
46
- rescue
47
- new = value
48
- end
49
- end
42
+ if /(Hash|Array)/.match?(value.class.to_s)
43
+ value.dup(:recursive)
44
+ else
45
+ begin
46
+ value.dup
47
+ rescue StandardError
48
+ new = value
49
+ end
50
+ end
50
51
  end
51
52
 
52
- self.each do |key, value| res[ sub_dup(key) ] = sub_dup(value) end
53
+ each do |key, value|
54
+ res[sub_dup(key)] = sub_dup(value)
55
+ end
53
56
 
54
57
  res
55
58
  elsif opts.empty?
56
59
  __dup__
57
60
  else
58
- raise "Unsupported option(s): #{opts.class == Hash ? opts.keys.join(', ') : opts}"
61
+ raise "Unsupported option(s): #{opts.instance_of?(Hash) ? opts.keys.join(', ') : opts}"
59
62
  end
60
63
  end
61
-
62
64
  end
63
-
64
-
data/lib/rdoba/fe.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/ruby -KU
2
+ # frozen_string_literal: true
2
3
 
3
4
  class String
4
5
  def fe
@@ -6,13 +7,13 @@ class String
6
7
  self
7
8
  else
8
9
  return self unless Encoding.default_internal
9
- if self.frozen?
10
- self.dup.force_encoding(Encoding.default_internal || 'UTF-8').freeze
10
+
11
+ if frozen?
12
+ dup.force_encoding(Encoding.default_internal || 'UTF-8').freeze
11
13
  else
12
- self.force_encoding(Encoding.default_internal || 'UTF-8')
14
+ force_encoding(Encoding.default_internal || 'UTF-8')
13
15
  end
14
16
  end
15
17
  end
16
- alias :fenc :fe
18
+ alias fenc fe
17
19
  end
18
-