rdoba 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rdoba'
2
4
  require 'tmpdir'
3
5
  require 'open3'
4
6
  require 'coveralls'
5
- require "codeclimate-test-reporter"
6
7
  require 'simplecov'
7
8
  require 'rspec/expectations'
8
9
 
@@ -10,176 +11,41 @@ require 'rspec/expectations'
10
11
  #
11
12
  #
12
13
  Coveralls.wear!
13
- CodeClimate::TestReporter.start
14
14
  SimpleCov.start
15
15
  ##############################################
16
16
 
17
- RdobaSimSimpleHead=<<HEAD
18
- #!/usr/bin/env ruby
19
-
20
- require 'rdoba'
21
- HEAD
22
-
23
- RdobaCodeClassDeclaration=<<HEAD
24
- class Cls
25
- def initialize
26
- log > {:variable=>"value"}
27
- end
28
- end
17
+ RdobaSimSimpleHead = <<~HEAD
18
+ #!/usr/bin/env ruby
19
+
20
+ require 'rdoba'
29
21
  HEAD
30
22
 
31
- RdobaSimClsHead=<<HEAD
32
- #!/usr/bin/env ruby
33
-
34
- require 'rdoba'
35
- class Cls
23
+ RdobaSimClsHead = <<~HEAD
24
+ #!/usr/bin/env ruby
25
+
26
+ require 'rdoba'
27
+ class Cls
36
28
  HEAD
37
29
 
38
- def format str
30
+ def format(str)
39
31
  puts str
40
- " " * @deep + str + "\n" ; end
32
+ ' ' * @deep + str + "\n"
33
+ end
41
34
 
42
35
  def store
43
- file = @inithead + @init + @echo + ( "end\n" * @deep )
36
+ file = @inithead + @init + @echo + ("end\n" * @deep)
44
37
  puts file
45
- File.open( @tmpfile, 'w+' ) do |f|
46
- f.puts file ; end ; end
47
-
48
- def match_keywords keystr
49
- keys = ( keystr || '' ).split( /[,:\s]/ ).map do |k|
50
- [ :basic, :extended, :enter, :leave, :compat, :timestamp, :pid,
51
- :function_name, :function_line, :function ].include?( k.to_sym ) && k.to_sym || nil
52
- end.compact ; end
53
-
54
- def rdoba_sim sub, cmd, *args
55
- echo = nil
56
- case sub
57
- when :log, :debug
58
- case cmd
59
- when :init
60
- $tmpdir ||= Dir.mktmpdir
61
- @tmpfile ||= File.join( $tmpdir, 'tmp.log' )
62
-
63
- if args[ 1 ] == 'class'
64
- @deep = 1
65
- @inithead = RdobaSimClsHead
66
- else
67
- @deep = 0
68
- @inithead = RdobaSimSimpleHead ; end
69
- @echo = ''
70
- @init = ''
71
- puts @inithead
72
-
73
- when :apply
74
- opts = match_keywords args[ 1 ]
75
- # opts = opts.size > 2 && opts || opts.size > 0 && opts[ 1 ] || nil
76
- param = case args[ 0 ]
77
- when 'io'
78
- { :io => "File.new( File.join( '#{@tmpfile}' ), 'w+' )", :functions => :basic }
79
- when 'as'
80
- { :as => ":self", :functions => :basic }
81
- when 'in'
82
- { :in => "Cls", :functions => :basic }
83
- # when /in.*as/
84
- # { :in => "Cls", :as => args[ 2 ].to_sym, :functions => :basic }
85
- when 'prefix'
86
- # basic|extended|enter|leave|compat
87
- { args[ 0 ].to_sym => opts, :functions => :basic }
88
- when 'functions'
89
- { args[ 0 ].to_sym => opts }
90
- else
91
- { :functions => :basic }
92
- end
93
-
94
- if args[ 2 ]
95
- param.merge!( :as => args[ 2 ].to_sym ) ; end
96
-
97
- param = "{" + ( param ).to_a.map do |v|
98
- "#{v[ 0 ].inspect} => #{v[ 1 ].is_a?( String ) &&
99
- v[ 1 ] || v[ 1 ].inspect}" end.join(',') + "}"
100
-
101
- @echo << " " * @deep + "rdoba :#{sub} => #{param}\n"
102
-
103
- when :func
104
- call = case args[ 0 ]
105
- when 'keyword'
106
- 'self'
107
- when 'invalid keyword'
108
- 'errlog'
109
- when /^db/
110
- @echo << "self.dbgl = 0xff\n"
111
- args[ 0 ]
112
- else
113
- ( args[ 0 ] || :log ).to_sym
114
- end
115
-
116
- sep = ( args[ 1 ] == :e ) && '.' || ' '
117
- param = []
118
- args[ 2..-1 ].each do |arg|
119
- case arg
120
- when NilClass
121
- when Symbol
122
- param << " #{arg}"
123
- else
124
- param << " #{arg.inspect}" ; end ; end
125
- str = "#{call}#{sep}#{args[ 1 ]}#{param.join(',')}"
126
- echo = format str
127
- @echo << echo
128
-
129
- when :close
130
- while @deep > 0
131
- @deep -= 1
132
- @echo << ( format "end" ) ; end
133
-
134
- when :declare
135
- @echo << ( format RdobaCodeClassDeclaration )
136
-
137
- when :create
138
- @echo << ( format "Cls.new" )
139
-
140
- when :call
141
- @echo << ( format "Cls.singleton" )
142
-
143
- when :def
144
- if @inithead == RdobaSimClsHead
145
- @deep = 1
146
- else
147
- @deep = 0 ; end
148
- echo = case args[ 0 ]
149
- when :init
150
- @deep += 1
151
- format 'def initialize'
152
- when :single
153
- @deep += 1
154
- format 'def self.singleton'
155
- else;
156
- '' ; end
157
- @echo << echo
158
-
159
- when :exec
160
- if !@echo.empty?
161
- store
162
- puts '-' * 15
163
-
164
- File.chmod 0755, @tmpfile
165
- Open3.popen3( @tmpfile ) do |stdin, stdout, stderr, wait_thr|
166
- @out = stdout.read
167
- @err = stderr.read
168
- end
169
- @echo = ''
170
- end
171
- res = case args[ 0 ]
172
- when :file
173
- File.new( @tmpfile, 'r' ).read
174
- when :stdout
175
- @out
176
- when :stderr
177
- @err;
178
- else return; end
179
- puts res
180
- res
181
- end
38
+ File.open(@tmpfile, 'w+') do |f|
39
+ f.puts file
182
40
  end
183
41
  end
184
42
 
185
- at_exit { $tmpdir && ( FileUtils.remove_entry_secure $tmpdir ) }
43
+ def match_keywords(keystr)
44
+ keys =
45
+ (keystr || '').split(/[,:\s]/).map do |k|
46
+ %i[basic extended enter leave compat timestamp pid function_name function_line function].include?(k.to_sym) &&
47
+ k.to_sym || nil
48
+ end.compact
49
+ end
50
+
51
+ at_exit { $tmpdir && (FileUtils.remove_entry_secure $tmpdir) }
@@ -1,13 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module MixinSupport
2
- def random_string count
3
- Random.new.bytes( ( count + 1 ) / 2 ).split( '' ).map do |b|
4
- b.ord.to_s( 16 )
5
- end.join[ 0...count ] ; end
4
+ def random_string(count)
5
+ Random.new.bytes((count + 1) / 2).split('').map do |b| b.ord.to_s(16)end.join[0...count]
6
+ end
6
7
 
7
- def tmpfile
8
- filename = File.join( Dir.mktmpdir, random_string( 20 ) )
9
- File.open( filename, 'w' ) do |f|
10
- f.print( random_string( 20 ) ) ; end
11
- filename ; end ; end
8
+ def tmpfile
9
+ filename = File.join(Dir.mktmpdir, random_string(20))
10
+ File.open(filename, 'w') do |f|
11
+ f.print(random_string(20))
12
+ end
13
+ filename
14
+ end
15
+ end
12
16
 
13
17
  World(MixinSupport)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rdoba
2
- VERSION = "0.9.3"
4
+ VERSION = '0.9.4'
3
5
  end
data/lib/rdoba/a.rb CHANGED
@@ -1,42 +1,42 @@
1
1
  #!/usr/bin/ruby -KU
2
- #coding:utf-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  require 'rdoba/debug'
5
5
 
6
6
  class Array
7
- def geta(index, options = {}) #TODO => [] + class Index
8
- dbp11 "[geta] <<< array = #{self.inspect}, index = #{index.inspect}, options = #{options.inspect}"
7
+ # TODO: => [] + class Index
8
+ def geta(index, options = {})
9
+ dbp11 "[geta] <<< array = #{inspect}, index = #{index.inspect}, options = #{options.inspect}"
9
10
  options[:сокр] ||= @сокр
10
11
 
11
- if index.class == Array
12
- return self if index == [] or index == ['']
12
+ if index.instance_of?(Array)
13
+ return self if [[], ['']].include?(index)
14
+
13
15
  index = index.clone
14
16
  value = self[index.shift]
15
- (value.class == Hash or value.class == Array) ? value.geta(index, options) : value
17
+ (value.instance_of?(Hash) or value.instance_of?(Array)) ? value.geta(index, options) : value
16
18
  else
17
19
  geta_value(index, options)
18
20
  end
19
21
  end
20
-
21
22
  end
22
23
 
23
-
24
24
  class Hash
25
-
26
- protected
25
+ protected
27
26
 
28
27
  def geta_value(cid, options = {})
29
- res = ((not cid) || cid.empty?) && self || self[cid] ||
28
+ res =
29
+ (!cid || cid.empty?) && self || self[cid] ||
30
30
  (options[:сокр] && (self[options[:сокр][cid]] || self[options[:сокр].reverse[cid]]))
31
31
 
32
- if not res and options[:try_regexp]
33
- self.keys.each do |key|
32
+ if !res and options[:try_regexp]
33
+ keys.each do |key|
34
34
  break res = self[key] if key.rmatch(cid)
35
- if options[:сокр]
36
- options[:сокр].each_pair do |val1, val2|
37
- break res = self[key] if key.rmatch(cid.gsub(/#{val1}/, val2)) or
38
- key.rmatch(cid.gsub(/#{val2}/, val1))
39
- end
35
+
36
+ next unless options[:сокр]
37
+
38
+ options[:сокр].each_pair do |val1, val2|
39
+ break res = self[key] if key.rmatch(cid.gsub(/#{val1}/, val2)) or key.rmatch(cid.gsub(/#{val2}/, val1))
40
40
  end
41
41
  end
42
42
  end
@@ -44,47 +44,51 @@ protected
44
44
  res
45
45
  end
46
46
 
47
- public
47
+ public
48
48
 
49
- def geta(index, options = {}) #TODO => [] + class Index
50
- dbp11 "[geta] <<< hash = #{self.inspect}, index = #{index.inspect}, options = #{options.inspect}"
49
+ # TODO: => [] + class Index
50
+ def geta(index, options = {})
51
+ dbp11 "[geta] <<< hash = #{inspect}, index = #{index.inspect}, options = #{options.inspect}"
51
52
  options[:сокр] ||= @сокр
52
53
 
53
- if index.class == Array
54
- return self if index == [] or index == ['']
54
+ if index.instance_of?(Array)
55
+ return self if [[], ['']].include?(index)
56
+
55
57
  index = index.clone
56
58
  value = geta_value(index.shift, options)
57
- (value.class == Hash or value.class == Array) ? value.geta(index, options) : value
59
+ (value.instance_of?(Hash) or value.instance_of?(Array)) ? value.geta(index, options) : value
58
60
  else
59
61
  geta_value(index, options)
60
62
  end
61
63
  end
62
64
 
63
- def seta(index, value, options = {}) #TODO => [] + class Index
65
+ # TODO: => [] + class Index
66
+ def seta(index, value, options = {})
64
67
  dbp11 "[seta] <<< index: #{index.inspect}, value: #{value.inspect}, options: #{options.inspect}"
65
68
  options[:сокр] ||= @сокр
66
69
 
67
- return self[index] = value if index.class != Array # TODO spec index
70
+ return self[index] = value if index.class != Array # TODO: spec index
68
71
 
69
72
  back = 0
70
- index = index.reverse.map do |x|
71
- if x.empty?
72
- back += 1
73
- nil
74
- elsif back > 0
75
- back -= 1
76
- nil
77
- else
78
- x
79
- end
80
- end.compact.reverse
73
+ index =
74
+ index.reverse.map do |x|
75
+ if x.empty?
76
+ back += 1
77
+ nil
78
+ elsif back > 0
79
+ back -= 1
80
+ nil
81
+ else
82
+ x
83
+ end
84
+ end.compact.reverse
81
85
  dbp12 "[seta]> result index: #{index.inspect}"
82
86
 
83
87
  obj = nil
84
88
  o = self
85
89
  dbp14 "[seta]>> self: #{o.inspect}"
86
90
  set_idx = index.pop
87
- par_class = set_idx =~ /^\d+$/ ? Array : Hash
91
+ par_class = /^\d+$/.match?(set_idx) ? Array : Hash
88
92
  par_idx = nil
89
93
  index.each do |idx|
90
94
  unless o
@@ -93,7 +97,7 @@ public
93
97
  obj[par_idx] = o
94
98
  end
95
99
  obj = o
96
- o = (obj.class == Hash) ? obj.geta_value(idx, options) : obj[idx]
100
+ o = obj.instance_of?(Hash) ? obj.geta_value(idx, options) : obj[idx]
97
101
  dbp14 "[seta]>> cur idx: #{idx.inspect}, parent obj: #{obj.inspect}, obj: #{o.inspect}"
98
102
  unless o
99
103
  if idx == index.last
@@ -105,10 +109,8 @@ public
105
109
  end
106
110
  end
107
111
 
108
- raise "Invalid path" unless o # TODO special exception
112
+ raise 'Invalid path' unless o # TODO: special exception
109
113
 
110
114
  o[set_idx] = value
111
115
  end
112
-
113
116
  end
114
-
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
-