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/gem.rb CHANGED
@@ -1,36 +1,40 @@
1
1
  #!/usr/bin/ruby -KU
2
- #encoding:utf-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  require 'rbconfig'
5
5
 
6
6
  module Rdoba
7
- def self.gemroot gemname = nil, path = ''
8
- if !gem
9
- raise "Undefined gem name" ; end
10
- g = Gem::Specification.find_by_name( gemname )
11
- File.join g.full_gem_path, 'share', 'settings.yaml'
12
- end
7
+ def self.gemroot(gemname = nil, _path = '')
8
+ unless gem
9
+ raise 'Undefined gem name'
10
+ end
13
11
 
14
- def self.os
15
- @@os ||= (
16
- host_os = RbConfig::CONFIG['host_os']
17
- case host_os
18
- when /(mswin|msys|mingw|cygwin|bccwin|wince|emc)/
19
- plat = $1 == 'mswin' && 'native' || $1
20
- out = `ver`.encode( 'US-ASCII',
21
- :invalid => :replace, :undef => :replace )
22
- if out =~ /\[.* (\d+)\.([\d\.]+)\]/
23
- "windows-#{plat}-#{$1 == '5' && 'xp' || 'vista'}-#{$1}.#{$2}"
24
- else
25
- "windows-#{plat}" ; end
26
- when /darwin|mac os/
27
- 'macosx'
28
- when /linux/
29
- 'linux'
30
- when /(solaris|bsd)/
31
- "unix-#{$1}"
32
- else
33
- raise "unknown os: #{host_os.inspect}"
34
- end)
35
- end ; end
12
+ g = Gem::Specification.find_by_name(gemname)
13
+ File.join g.full_gem_path, 'share', 'settings.yaml'
14
+ end
36
15
 
16
+ def self.os
17
+ @@os ||=
18
+ begin
19
+ host_os = RbConfig::CONFIG['host_os']
20
+ case host_os
21
+ when /(mswin|msys|mingw|cygwin|bccwin|wince|emc)/
22
+ plat = Regexp.last_match(1) == 'mswin' && 'native' || Regexp.last_match(1)
23
+ out = `ver`.encode('US-ASCII', invalid: :replace, undef: :replace)
24
+ if out =~ /\[.* (\d+)\.([\d.]+)\]/
25
+ "windows-#{plat}-#{Regexp.last_match(1) == '5' && 'xp' || 'vista'}-#{Regexp.last_match(1)}.#{Regexp.last_match(2)}"
26
+ else
27
+ "windows-#{plat}"
28
+ end
29
+ when /darwin|mac os/
30
+ 'macosx'
31
+ when /linux/
32
+ 'linux'
33
+ when /(solaris|bsd)/
34
+ "unix-#{Regexp.last_match(1)}"
35
+ else
36
+ raise "unknown os: #{host_os.inspect}"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,35 +1,36 @@
1
1
  #!/usr/bin/ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  class Hash
4
5
  attr_reader :order
5
6
 
6
7
  class Each
7
8
  General = 0
8
- Pair = 1
9
- Key = 2
10
- Value = 3
9
+ Pair = 1
10
+ Key = 2
11
+ Value = 3
11
12
  end
12
13
 
13
- private
14
+ private
14
15
 
15
16
  def each_special(spec)
16
- (@order | self.keys).each do |key|
17
- if self.has_key? key
18
- case spec
19
- when Hash::Each::General
20
- yield key, self[key]
21
- when Hash::Each::Pair
22
- yield key, self[key]
23
- when Hash::Each::Key
24
- yield key
25
- when Hash::Each::Value
26
- yield self[key]
27
- end
17
+ (@order | keys).each do |key|
18
+ next unless has_key? key
19
+
20
+ case spec
21
+ when Hash::Each::General
22
+ yield key, self[key]
23
+ when Hash::Each::Pair
24
+ yield key, self[key]
25
+ when Hash::Each::Key
26
+ yield key
27
+ when Hash::Each::Value
28
+ yield self[key]
28
29
  end
29
30
  end
30
31
  end
31
32
 
32
- public
33
+ public
33
34
 
34
35
  def order=(order)
35
36
  return nil if order.class != Array
@@ -38,13 +39,14 @@ public
38
39
  end
39
40
 
40
41
  def disorder
41
- @order = nil; self
42
+ @order = nil
43
+ self
42
44
  end
43
45
 
44
- alias :__each__ :each
45
- alias :__each_pair__ :each_pair
46
- alias :__each_key__ :each_key
47
- alias :__each_value__ :each_value
46
+ alias __each__ each
47
+ alias __each_pair__ each_pair
48
+ alias __each_key__ each_key
49
+ alias __each_value__ each_value
48
50
 
49
51
  def each(&block)
50
52
  @order ? each_special(Hash::Each::General, &block) : __each__(&block)
@@ -61,6 +63,4 @@ public
61
63
  def each_value(&block)
62
64
  @order ? each_special(Hash::Each::Value, &block) : __each_value__(&block)
63
65
  end
64
-
65
66
  end
66
-
data/lib/rdoba/io.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/ruby -KU
2
- #coding:utf-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  require 'strscan'
5
5
  require 'rdoba/re'
@@ -7,7 +7,7 @@ require 'rdoba/roman'
7
7
  require 'rdoba/numeric'
8
8
 
9
9
  module Kernel
10
- alias :__sprintf__ :sprintf
10
+ alias __sprintf__ sprintf
11
11
  def sprintf(format, *args)
12
12
  nargs = []
13
13
  nformat = ''
@@ -15,26 +15,31 @@ module Kernel
15
15
  fmt = format.split('%')
16
16
  nformat = fmt.shift
17
17
 
18
- while (not fmt.empty?)
18
+ until fmt.empty?
19
19
  part = fmt.shift
20
- part = '%' + fmt.shift unless part
21
- if part =~ /([0-9 #+\-*.]*)([bcdEefGgiopsuXxP])(.*)/ and $2 == 'P'
22
- keys = $1 || ''
23
- str = $3 || ''
24
- if keys =~ /(-)?([0-9*]*)\.?([0-9\*]*)(\+?)/
20
+ part ||= '%' + fmt.shift
21
+ if part =~ /([0-9 #+\-*.]*)([bcdEefGgiopsuXxP])(.*)/ and Regexp.last_match(2) == 'P'
22
+ keys = Regexp.last_match(1) || ''
23
+ str = Regexp.last_match(3) || ''
24
+ if keys =~ /(-)?([0-9*]*)\.?([0-9*]*)(\+?)/
25
25
  value = args.shift
26
- indent = ' ' * ($2 == '*' ? args.shift : $2).to_i
27
- plain = value && value.to_p(
28
- :padding => ($3 == '*' ? args.shift : $3.empty? ? 1 : $3).to_i,
29
- :be => $4.empty? ? nil : true) || ''
30
- nformat += ($1 ? plain + indent : indent + plain) + str
26
+ indent = ' ' * (Regexp.last_match(2) == '*' ? args.shift : Regexp.last_match(2)).to_i
27
+ plain =
28
+ value &&
29
+ value.to_p(
30
+ padding:
31
+ (Regexp.last_match(3) == '*' ? args.shift : Regexp.last_match(3).empty? ? 1 : Regexp.last_match(3))
32
+ .to_i,
33
+ be: Regexp.last_match(4).empty? ? nil : true
34
+ ) || ''
35
+ nformat += (Regexp.last_match(1) ? plain + indent : indent + plain) + str
31
36
  else
32
37
  nformat += '%' + keys + 'c' + str
33
38
  nargs.push args.shift
34
39
  end
35
40
  else
36
41
  nformat += '%' + part
37
- l = $1 =~ /\*/ ? 2 : 1
42
+ l = Regexp.last_match(1) =~ /\*/ ? 2 : 1
38
43
  while l > 0
39
44
  nargs.push args.shift
40
45
  l -= 1
@@ -43,57 +48,59 @@ module Kernel
43
48
  end
44
49
  __sprintf__(nformat, *nargs).to_p
45
50
  end
46
-
47
51
  end
48
52
 
49
53
  class String
50
54
  def scanf_re(format)
51
- fss = StringScanner.new(format) # TODO remove scanner in favor of match
55
+ fss = StringScanner.new(format) # TODO: remove scanner in favor of match
52
56
  nformat = ''
53
57
 
54
58
  pos = 0
55
59
  argfs = []
56
60
  while fss.scan_until(/%([0-9 #+\-*]*)(?:\.([0-9]+)(\+)?)?([bcdefgiosuxr])/)
57
- argfs << [ fss[1], fss[2], fss[3], fss[4] ]
58
- # TODO add performing the special case in fss[1]
59
- nformat += fss.pre_match[pos..-1].to_res + case fss[4]
60
- when 'x'
61
- '(?:0[xX])?([a-fA-F0-9]+)'
62
- when 'i'
63
- '([+\-]?[0-9]+)'
64
- when 'u'
65
- '([0-9]+)'
66
- when 'e'
67
- '([+\-]?[0-9]+[eE][+\-]?[0-9]+)'
68
- when 'f'
69
- '([+\-]?[0-9]+\.[0-9]*)'
70
- when 'g'
71
- '([+\-]?[0-9]+(?:[eE][+\-]?[0-9]+|\.[0-9]*))'
72
- when 'c'
73
- fss[2] ? "(.{1,#{fss[2]}})" : "(.)"
74
- when 'b'
75
- '([01]+)b?'
76
- when 'o'
77
- '0([0-9]+)'
78
- when 'd'
79
- '([+\-]?(?:0X)?[A-F0-9.+]+)'
80
- when 's'
81
- '(.+)'
82
- when 'r'
83
- '([IVXLCDMivxlcdm]+)'
84
- end
61
+ argfs << [fss[1], fss[2], fss[3], fss[4]]
62
+
63
+ #  TODO add performing the special case in fss[1]
64
+ nformat +=
65
+ fss.pre_match[pos..-1].to_res +
66
+ case fss[4]
67
+ when 'x'
68
+ '(?:0[xX])?([a-fA-F0-9]+)'
69
+ when 'i'
70
+ '([+\-]?[0-9]+)'
71
+ when 'u'
72
+ '([0-9]+)'
73
+ when 'e'
74
+ '([+\-]?[0-9]+[eE][+\-]?[0-9]+)'
75
+ when 'f'
76
+ '([+\-]?[0-9]+\.[0-9]*)'
77
+ when 'g'
78
+ '([+\-]?[0-9]+(?:[eE][+\-]?[0-9]+|\.[0-9]*))'
79
+ when 'c'
80
+ fss[2] ? "(.{1,#{fss[2]}})" : '(.)'
81
+ when 'b'
82
+ '([01]+)b?'
83
+ when 'o'
84
+ '0([0-9]+)'
85
+ when 'd'
86
+ '([+\-]?(?:0X)?[A-F0-9.+]+)'
87
+ when 's'
88
+ '(.+)'
89
+ when 'r'
90
+ '([IVXLCDMivxlcdm]+)'
91
+ end
85
92
 
86
93
  pos = fss.pos
87
94
  end
88
95
 
89
96
  nformat += fss.rest
90
97
 
91
- [ /#{nformat}/, argfs ]
98
+ [/#{nformat}/, argfs]
92
99
  end
93
100
 
94
- (alias :__scanf__ :scanf) if self.instance_methods(false).include?(:scanf)
101
+ (alias __scanf__ scanf) if instance_methods(false).include?(:scanf)
95
102
  def scanf(format, &block)
96
- (re, argfs) = scanf_re(format)
103
+ re, argfs = scanf_re(format)
97
104
 
98
105
  ss = StringScanner.new(self)
99
106
  res = []
@@ -102,29 +109,32 @@ class String
102
109
  argfs.each_index do |i|
103
110
  argf = argfs[i]
104
111
  value = ss[i + 1]
105
- rline << case argf[3]
106
- when 'x'
107
- value.to_i(16)
108
- when /[diu]/
109
- value.to_i
110
- when /[efg]/
111
- value.to_f
112
- when 'c'
113
- argf[2] ? value.to_i(:be) : value.to_i
114
- when 'b'
115
- value.to_i(2)
116
- when 'o'
117
- value.to_i(8)
118
- when 's'
119
- value
120
- when 'r'
121
- value.rom
122
- end
112
+ rline <<
113
+ case argf[3]
114
+ when 'x'
115
+ value.to_i(16)
116
+ when /[diu]/
117
+ value.to_i
118
+ when /[efg]/
119
+ value.to_f
120
+ when 'c'
121
+ argf[2] ? value.to_i(:be) : value.to_i
122
+ when 'b'
123
+ value.to_i(2)
124
+ when 'o'
125
+ value.to_i(8)
126
+ when 's'
127
+ value
128
+ when 'r'
129
+ value.rom
130
+ end
123
131
  end
124
132
 
125
- if block_given?
133
+ if block
126
134
  pass = []
127
- (1..block.arity).each do |i| pass << "rline[#{i}]" end
135
+ (1..block.arity).each do |i|
136
+ pass << "rline[#{i}]"
137
+ end
128
138
  eval "yield(#{pass.join(', ')})"
129
139
  end
130
140
 
@@ -144,17 +154,14 @@ class String
144
154
  ostr[0...ss.pre_match.size - pos] = ss.pre_match[pos..-1]
145
155
  pos = ss.pos
146
156
 
147
- if ss.post_match[0] == "\n"[0]
148
- res = ostr
149
- pos += 1
150
- ostr = ''
151
- end
157
+ next unless ss.post_match[0] == "\n"[0]
152
158
 
159
+ res = ostr
160
+ pos += 1
161
+ ostr = ''
153
162
  end
154
163
 
155
164
  ostr[0...ss.rest.size] = ss.rest
156
165
  res + ostr
157
166
  end
158
167
  end
159
-
160
-
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rdoba::Merge
4
+ TARGET = :Hash
5
+
6
+ def deep_merge(source, dest)
7
+ dup = dest.dup
8
+ source.each do |key, value|
9
+ newvalue = dup.delete key
10
+ case newvalue
11
+ when Hash
12
+ value.deep_merge newvalue
13
+ when Array
14
+ value |= newvalue
15
+ when NilClass
16
+ else
17
+ raise
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rdoba::Mixin::Time
4
+ require 'ffi/stat'
5
+
6
+ def mtime(file)
7
+ FFI::Stat.stat(file)[:st_mtimespec].to_time
8
+ end
9
+
10
+ def atime(file)
11
+ FFI::Stat.stat(file)[:st_atimespec].to_time
12
+ end
13
+
14
+ def ctime(file)
15
+ FFI::Stat.stat(file)[:st_ctimespec].to_time
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rdoba::Mixin::TryObject
4
+ def try(method, *args, default: nil)
5
+ if respond_to?(method)
6
+ send(method, *args)
7
+ else
8
+ default
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rdoba::Mixin::TryObject
4
+ def try(method, *args)
5
+ if respond_to?(method)
6
+ send(method, *args)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'timeout'
4
+
5
+ module Rdoba::Mixin::Wait_ifKernel
6
+ ##
7
+ # +wait_if+ waits for +timeout+ second to the condition passed via block,
8
+ # and in case if it failed, returns false, otherwise true. +timeout+ can
9
+ # be a float or integer number of seconds, but if passed 0 or nil it waits
10
+ # forever. Default value is 6 seconds. Example:
11
+ #
12
+ # wait_if(5) { sleep 2; true } # => true
13
+ # wait_if(5) { sleep 10; true } # => false
14
+ #
15
+ def wait_if(timeout = 6)
16
+ begin
17
+ Timeout.timeout(timeout) do
18
+ while yield()
19
+ sleep(0.1)
20
+ end
21
+ end
22
+ true
23
+ rescue Timeout::Error
24
+ false
25
+ end
26
+ end
27
+ end