rdoba 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rdoba/numeric.rb CHANGED
@@ -1,20 +1,20 @@
1
1
  #!/usr/bin/ruby -KU
2
- #coding:utf-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  require 'rdoba/common'
5
5
  require 'rdoba/strings'
6
6
 
7
7
  class String
8
- alias :_rdoba_to_i :to_i
8
+ alias _rdoba_to_i to_i
9
9
  def to_i(base = 10, *opts)
10
10
  v = parse_opts(opts)
11
11
  if v[:be]
12
- (str, sign, num) = (self.match /\s*(-?)([0-9a-fx]+)/u).to_a
12
+ str, sign, num = (match /\s*(-?)([0-9a-fx]+)/u).to_a
13
13
  if str
14
- n = num.reverse._rdoba_to_i(base)
15
- sign.empty? && n || -n
14
+ n = num.reverse._rdoba_to_i(base)
15
+ sign.empty? && n || -n
16
16
  else
17
- 0
17
+ 0
18
18
  end
19
19
  else
20
20
  _rdoba_to_i(base)
@@ -22,8 +22,8 @@ class String
22
22
  end
23
23
  end
24
24
 
25
- class Fixnum
26
- alias :_rdoba_to_s :to_s
25
+ class Integer
26
+ alias _rdoba_to_s to_s
27
27
  def to_s(base = 10, *opts)
28
28
  v = parse_opts(opts)
29
29
 
@@ -31,8 +31,10 @@ class Fixnum
31
31
 
32
32
  raise "Base of number can't be equal or less then zero" if base <= 0
33
33
  raise "Padding count numberr can't be equal or less then zero" if v[:padding] <= 0
34
+
34
35
  value = self
35
- minus = if value < 0
36
+ minus =
37
+ if value < 0
36
38
  value = -value
37
39
  true
38
40
  end
@@ -42,7 +44,7 @@ class Fixnum
42
44
  rem += 0x40 - 0x39 if rem >= 10
43
45
  res += (0x30 + rem).chr
44
46
  end
45
- res += "0" * (v[:padding].to_i - res.size) if res.size < v[:padding].to_i
47
+ res += '0' * (v[:padding].to_i - res.size) if res.size < v[:padding].to_i
46
48
  res += 'x0' if v[:style_formatting] and base == 16
47
49
  res += '-' if minus
48
50
  res.reverse
@@ -54,7 +56,8 @@ class Numeric
54
56
  v = parse_opts(opts)
55
57
 
56
58
  value = self
57
- minus = if value < 0
59
+ minus =
60
+ if value < 0
58
61
  value = -value
59
62
  true
60
63
  end
@@ -64,12 +67,14 @@ class Numeric
64
67
  res += rem.chr
65
68
  end
66
69
 
67
- pad_char = if minus
70
+ pad_char =
71
+ if minus
68
72
  negres += ''
69
73
  over = 1
70
74
  res.each_byte do |byte|
71
75
  negbyte = 255 - byte + over
72
- negres += if negbyte > 255
76
+ negres +=
77
+ if negbyte > 255
73
78
  over = 1
74
79
  0
75
80
  else
@@ -85,9 +90,6 @@ class Numeric
85
90
 
86
91
  res += pad_char * (v[:padding].to_i - res.size) if res.size < v[:padding].to_i
87
92
 
88
- plain = (v[:be] ? res.reverse(String::ByteByByte) : res).to_p
89
- plain
93
+ (v[:be] ? res.reverse(String::ByteByByte) : res).to_p
90
94
  end
91
95
  end
92
-
93
-
data/lib/rdoba/os.rb ADDED
@@ -0,0 +1,127 @@
1
+ require 'ostruct'
2
+
3
+ require 'rdoba/blank'
4
+
5
+ class Object
6
+ def to_os
7
+ OpenStruct.new(self.to_h.map {|(x, y)| [x.to_s, [Hash, Array].include?(y.class) && y.to_os || y] }.to_h)
8
+ end
9
+ end
10
+
11
+ class Array
12
+ def to_os
13
+ OpenStruct.new(self.map.with_index {|y, x| [x.to_s, [Hash, Array].include?(y.class) && y.to_os || y] }.to_h)
14
+ end
15
+ end
16
+
17
+ class Integer
18
+ def to_sym
19
+ to_s.to_sym
20
+ end
21
+ end
22
+
23
+ class OpenStruct
24
+ def merge_to other
25
+ OpenStruct.new(other.to_h.merge(self.to_h))
26
+ end
27
+
28
+ def merge other
29
+ OpenStruct.new(self.to_h.merge(other.to_h))
30
+ end
31
+
32
+ def map *args, &block
33
+ res = self.class.new
34
+
35
+ self.each_pair do |key, value|
36
+ res[key] = block[key, value]
37
+ end
38
+
39
+ res
40
+ end
41
+
42
+ def select &block
43
+ res = self.class.new
44
+
45
+ self.each_pair do |key, value|
46
+ res[key] = value if block[key, value]
47
+ end
48
+
49
+ res
50
+ end
51
+
52
+ def compact
53
+ select { |_, value| !value.blank? }
54
+ end
55
+
56
+ def each *args, &block
57
+ self.each_pair(*args, &block)
58
+ end
59
+
60
+ def reduce default = nil, &block
61
+ res = default
62
+
63
+ self.each_pair do |key, value|
64
+ res = block[res, key, value]
65
+ end
66
+
67
+ res
68
+ end
69
+
70
+ # +deep_merge+ deeply merges the Open Struct hash structure with the +other_in+ enumerating it key by key.
71
+ # +options+ are the options to change behaviour of the method. It allows two keys: :mode, and :dedup
72
+ # :mode key can be :append, :prepend, or :replace, defaulting to :append, when mode is to append, it combines duplicated
73
+ # keys' values into an array, when :prepend it prepends an other value before previously stored one unlike for :append mode,
74
+ # when :replace it replace duplicate values with a last ones.
75
+ # :dedup key can be true, or false. It allows to deduplicate values when appending or prepending values.
76
+ # Examples:
77
+ # open_struct.deep_merge(other_open_struct)
78
+ # open_struct.deep_merge(other_open_struct, :prepend)
79
+ #
80
+ def deep_merge other_in, options_in = {}
81
+ return self if other_in.nil? or other_in.blank?
82
+
83
+ options = { mode: :append }.merge(options_in)
84
+
85
+ other =
86
+ if other_in.is_a?(OpenStruct)
87
+ other_in.dup
88
+ elsif other_in.is_a?(Hash)
89
+ other_in.to_os
90
+ else
91
+ OpenStruct.new(nil => other_in)
92
+ end
93
+
94
+ self.reduce(other) do |res, key, value|
95
+ res[key] =
96
+ if res.table.keys.include?(key)
97
+ case value
98
+ when Hash, OpenStruct
99
+ value.deep_merge(res[key], options)
100
+ when Array
101
+ value.concat([res[key]].compact.flatten(1))
102
+ when NilClass
103
+ res[key]
104
+ else
105
+ value_out =
106
+ if options[:mode] == :append
107
+ [res[key], value].compact.flatten(1)
108
+ elsif options[:mode] == :prepend
109
+ [value, res[key]].compact.flatten(1)
110
+ else
111
+ value
112
+ end
113
+
114
+ if value_out.is_a?(Array) && options[:dedup]
115
+ value_out.uniq
116
+ else
117
+ value_out
118
+ end
119
+ end
120
+ else
121
+ value
122
+ end
123
+
124
+ res
125
+ end
126
+ end
127
+ end
data/lib/rdoba/re.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 String
5
5
  def to_res
6
- ostr = self.dup
6
+ ostr = dup
7
7
  res = ''
8
8
  while true
9
- m = ostr.match(/(?:([+\[\]\\().*?{}^$\/|])|«([^«]*)»)/u)
9
+ m = ostr.match(%r{(?:([+\[\]\\().*?{}^$/|])|«([^«]*)»)}u)
10
10
  break unless m
11
+
11
12
  res += m.pre_match + (m[2] || m[1] && ('\\' + m[1]))
12
13
  ostr = m.post_match
13
14
  end
@@ -19,4 +20,3 @@ class String
19
20
  /#{to_res}/ui
20
21
  end
21
22
  end
22
-
data/lib/rdoba/require.rb CHANGED
@@ -1,56 +1,62 @@
1
1
  #!/usr/bin/ruby -KU
2
- #coding:utf-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  require 'rdoba/common'
5
5
  require 'rdoba/debug'
6
6
 
7
7
  module Kernel
8
- private
8
+ private
9
+
9
10
  def require_dir(dir, name)
10
11
  dbp11 "[require_dir] <<< dir = #{dir}, name = #{name}"
11
12
  begin
12
13
  rdir = File.join(dir, name)
13
14
  return false unless File.directory?(rdir)
15
+
14
16
  rdir = File.join(dir, name)
15
17
  $: << rdir unless $:.include?(rdir)
16
18
  dbp14 "[require_dir]> Found dir #{rdir}"
17
19
  Dir.foreach(rdir) do |file|
18
20
  next unless file =~ /(.*)\.(rb|so)$/
19
- dbp14 "[require_dir]> Loading ... #{$1}"
20
- require $1
21
+
22
+ dbp14 "[require_dir]> Loading ... #{Regexp.last_match(1)}"
23
+ require Regexp.last_match(1)
21
24
  end
22
25
  true
23
- rescue
26
+ rescue StandardError
24
27
  false
25
28
  end
26
29
  end
27
30
 
28
31
  def sub_require(name)
29
32
  dbp11 "[sub_require] <<< name = #{name} "
30
- $:.each do |dir|
31
- begin
32
- Dir.foreach(dir) do |file|
33
- next unless file =~ /^#{name}\.(rb|so)$/
34
- dbp14 "[sub_require]> Require Dir #{dir}/#{name} for #{file}"
35
- r1 = require_dir(dir, name + '.modules')
36
- r2 = require_dir(dir, name)
37
- dbp14 "[sub_require]> Require Dir #{(r1 || r2) && 'passed' || 'failed'} ... #{name}"
33
+ unless $".include?(name)
34
+ $:.each do |dir|
35
+ begin
36
+ Dir.foreach(dir) do |file|
37
+ next unless /^#{name}\.(rb|so)$/.match?(file)
38
+
39
+ dbp14 "[sub_require]> Require Dir #{dir}/#{name} for #{file}"
40
+ r1 = require_dir(dir, name + '.modules')
41
+ r2 = require_dir(dir, name)
42
+ dbp14 "[sub_require]> Require Dir #{(r1 || r2) && 'passed' || 'failed'} ... #{name}"
43
+ end
44
+ rescue StandardError
38
45
  end
39
- rescue
40
46
  end
41
- end unless $".include?(name)
47
+ end
42
48
  true
43
49
  end
44
50
 
45
- public
51
+ public
46
52
 
47
- alias :__require__ :require
53
+ alias __require__ require
48
54
  def require(name, *opts)
49
55
  v = parse_opts(opts)
50
56
  dbp11 "[require] <<< name = #{name}"
51
57
  begin
52
58
  res = __require__ name
53
- rescue => bang
59
+ rescue StandardError => e
54
60
  puts "Lib internal error: #{$!.class} -> #{$!}\n\t#{$@.join("\n\t")}"
55
61
  exit
56
62
  end
@@ -59,4 +65,3 @@ public
59
65
  res
60
66
  end
61
67
  end
62
-
data/lib/rdoba/roman.rb CHANGED
@@ -1,43 +1,55 @@
1
1
  #!/usr/bin/ruby -KU
2
+ # frozen_string_literal: true
2
3
 
3
4
  class Numeric
4
- Roman = { 1 => 'I',
5
- 4 => 'IV',
6
- 5 => 'V',
7
- 9 => 'IX',
8
- 10 => 'X',
9
- 40 => 'XL',
10
- 50 => 'L',
11
- 90 => 'XC',
12
- 100 => 'C',
13
- 400 => 'CD',
14
- 500 => 'D',
15
- 900 => 'CM',
16
- 1000 => 'M' }
17
- RomanNumbers = Roman.keys.sort
18
- RomanToInteger = Roman.invert
19
- RomanDigits = RomanToInteger.keys.sort { |x,y| x.size < y.size ? 1 : x.size > y.size ? -1 : x <=> y }
5
+ Roman = {
6
+ 1 => 'I',
7
+ 4 => 'IV',
8
+ 5 => 'V',
9
+ 9 => 'IX',
10
+ 10 => 'X',
11
+ 40 => 'XL',
12
+ 50 => 'L',
13
+ 90 => 'XC',
14
+ 100 => 'C',
15
+ 400 => 'CD',
16
+ 500 => 'D',
17
+ 900 => 'CM',
18
+ 1000 => 'M'
19
+ }
20
+ RomanNumbers = Roman.keys.sort
21
+ RomanToInteger = Roman.invert
22
+ RomanDigits = RomanToInteger.keys.sort { |x, y| x.size < y.size ? 1 : x.size > y.size ? -1 : x <=> y }
20
23
 
21
- def to_rom
22
- res = ''
23
- num = self
24
- i = RomanNumbers.size - 1
24
+ def to_rom
25
+ res = ''
26
+ num = self
27
+ i = RomanNumbers.size - 1
25
28
 
26
- while num > 0
27
- if num >= RomanNumbers[ i ]
28
- res << Roman[ RomanNumbers[ i ] ]
29
- num -= RomanNumbers[ i ]
30
- else
31
- i -= 1 ;end;end
32
- res ;end;end
29
+ while num > 0
30
+ if num >= RomanNumbers[i]
31
+ res << Roman[RomanNumbers[i]]
32
+ num -= RomanNumbers[i]
33
+ else
34
+ i -= 1
35
+ end
36
+ end
37
+ res
38
+ end
39
+ end
33
40
 
34
41
  class String
35
- RomanRe = /(#{Numeric::RomanDigits.join("|")})/
42
+ RomanRe = /(#{Numeric::RomanDigits.join('|')})/
36
43
 
37
- def rom
38
- numbers = self.upcase.scan(RomanRe).flatten.map { |x| Numeric::RomanToInteger[x] }
39
- numbers.sort do |x, y|
40
- if x < y
41
- raise "Invalid roman number" ;end
42
- 0 ;end
43
- numbers.sum ;end;end
44
+ def rom
45
+ numbers = upcase.scan(RomanRe).flatten.map { |x| Numeric::RomanToInteger[x] }
46
+ numbers.sort do |x, y|
47
+ if x < y
48
+ raise 'Invalid roman number'
49
+ end
50
+
51
+ 0
52
+ end
53
+ numbers.sum
54
+ end
55
+ end
data/lib/rdoba/strings.rb CHANGED
@@ -1,12 +1,11 @@
1
1
  #!/usr/bin/ruby -KU
2
- #coding:utf-8
2
+ # frozen_string_literal: true
3
3
 
4
- STDERR.puts "Warning: the module 'string' has kept only for backward " \
5
- "compatibility\nPlease use 'rdoba :mixin' form instead"
4
+ warn "Warning: the module 'string' has kept only for backward " \
5
+ "compatibility\nPlease use 'rdoba :mixin' form instead"
6
6
 
7
7
  class String
8
- alias :to_p :to_s
8
+ alias to_p to_s
9
9
  end
10
10
 
11
- rdoba :mixin => [ :case, :reverse, :compare ]
12
-
11
+ rdoba mixin: %i[case reverse compare]
data/lib/rdoba/yaml.rb CHANGED
@@ -1,48 +1,50 @@
1
1
  #!/usr/bin/ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'rdoba/common'
4
5
 
5
6
  class Object
6
- def to_yml( o = {} )
7
+ def to_yml(o = {})
7
8
  level = o[:level] || 0
8
9
  res = ''
9
10
  res += '---' if level == 0
10
- res += case self.class.to_sym
11
+ res +=
12
+ case self.class.to_sym
11
13
  when :Hash
12
14
  rs = ''
13
- self.keys.sort do |x,y|
14
- (ix, iy) = o[:order] ? [ o[:order].index(x), o[:order].index(y) ] : [ nil, nil ]
15
+ keys.sort do |x, y|
16
+ ix, iy = o[:order] ? [o[:order].index(x), o[:order].index(y)] : [nil, nil]
15
17
  (ix and iy) ? ix <=> iy : (ix ? -1 : (iy ? 1 : x <=> y))
16
18
  end.each do |key|
17
19
  value = self[key]
18
- rs += "\n" + ' ' * level * 2 + key.to_yml( { :level => level + 1, :order => o[:order] } )
19
- rs += ': ' + value.to_yml( { :level => level + 1, :order => o[:order] } )
20
+ rs += "\n" + ' ' * level * 2 + key.to_yml({ level: level + 1, order: o[:order] })
21
+ rs += ': ' + value.to_yml({ level: level + 1, order: o[:order] })
20
22
  end
21
- rs.empty? and "{}" or rs
23
+ rs.empty? and '{}' or rs
22
24
  when :Array
23
25
  rs = ''
24
- self.each do |value|
25
- rs += "\n" + ' ' * level * 2 + '- ' + value.to_yml( { :level => level + 1, :order => o[:order] } )
26
+ each do |value|
27
+ rs += "\n" + ' ' * level * 2 + '- ' + value.to_yml({ level: level + 1, order: o[:order] })
26
28
  end
27
- rs.empty? and "[]" or rs
29
+ rs.empty? and '[]' or rs
28
30
  when :Fixnum
29
- self.to_s
31
+ to_s
30
32
  when :String
31
33
  if self =~ /^["'\-:!#={}\[\]~]/ or self =~ /\s+$/ or self =~ /:\s/
32
- if self.count("'") < self.count('"')
33
- "'#{self.gsub("'","\\'")}'"
34
- else
35
- "\"#{self.gsub('"','\"')}\""
36
- end
34
+ if count("'") < count('"')
35
+ "'#{gsub("'", "\\'")}'"
36
+ else
37
+ "\"#{gsub('"', '\"')}\""
38
+ end
37
39
  else
38
40
  self
39
41
  end
40
42
  when :NilClass
41
43
  ''
42
44
  else
43
- $stderr.puts "Unsupported class #{self.class} to export to yml"; ''
45
+ warn "Unsupported class #{self.class} to export to yml"
46
+ ''
44
47
  end
45
48
  res
46
49
  end
47
50
  end
48
-
data/lib/rdoba.rb CHANGED
@@ -1,51 +1,60 @@
1
- #encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Kernel
4
- Modules = [ :bcd, :mixin, :log, :debug ]
5
-
6
- def rdoba *options
7
- options.each do |option|
8
- ( option.is_a?( Hash ) &&
9
- option || { option.to_s.to_sym => {} }
10
- ).each_pair do |key, value|
11
- if Modules.include? key
12
- require "rdoba/#{key}"
13
- if Rdoba.methods.include? key
14
- if !value.is_a? Hash
15
- value = { :value => value } ; end
16
- value.replace( { :self => self }.merge value )
17
- Rdoba.send key, value ; end ; end ; end; end ; end ; end
4
+ Modules = %i[bcd mixin log debug]
5
+
6
+ def rdoba(*options)
7
+ options.each do |option|
8
+ (option.is_a?(Hash) && option || { option.to_s.to_sym => {} }).each_pair do |key, value|
9
+ next unless Modules.include? key
10
+
11
+ require "rdoba/#{key}"
12
+ next unless Rdoba.methods.include? key
13
+
14
+ unless value.is_a? Hash
15
+ value = { value: value }
16
+ end
17
+ value.replace({ self: self }.merge(value))
18
+ Rdoba.send key, value
19
+ end
20
+ end
21
+ end
22
+ end
18
23
 
19
24
  require 'rbconfig'
20
25
 
21
26
  module Rdoba
22
- def self.gemroot name = nil, path = ''
23
- if !gem( name )
24
- raise "Invalid gem named as #{name.inspect}" ; end
25
- g = Gem::Specification.find_by_name( name )
26
- File.join g.full_gem_path, path
27
- end
28
-
29
- def self.os
30
- @@os ||= (
31
- host_os = RbConfig::CONFIG['host_os']
32
- case host_os
33
- when /(mswin|msys|mingw|cygwin|bccwin|wince|emc)/
34
- plat = $1 == 'mswin' && 'native' || $1
35
- out = `ver`.encode( 'US-ASCII',
36
- :invalid => :replace, :undef => :replace )
37
- if out =~ /\[.* (\d+)\.([\d\.]+)\]/
38
- "windows-#{plat}-#{$1 == '5' && 'xp' || 'vista'}-#{$1}.#{$2}"
39
- else
40
- "windows-#{plat}" ; end
41
- when /darwin|mac os/
42
- 'macosx'
43
- when /linux/
44
- 'linux'
45
- when /(solaris|bsd)/
46
- "unix-#{$1}"
47
- else
48
- raise "unknown os: #{host_os.inspect}"
49
- end)
50
- end ; end
27
+ def self.gemroot(name = nil, path = '')
28
+ unless gem(name)
29
+ raise "Invalid gem named as #{name.inspect}"
30
+ end
31
+
32
+ g = Gem::Specification.find_by_name(name)
33
+ File.join g.full_gem_path, path
34
+ end
51
35
 
36
+ def self.os
37
+ @@os ||=
38
+ begin
39
+ host_os = RbConfig::CONFIG['host_os']
40
+ case host_os
41
+ when /(mswin|msys|mingw|cygwin|bccwin|wince|emc)/
42
+ plat = Regexp.last_match(1) == 'mswin' && 'native' || Regexp.last_match(1)
43
+ out = `ver`.encode('US-ASCII', invalid: :replace, undef: :replace)
44
+ if out =~ /\[.* (\d+)\.([\d.]+)\]/
45
+ "windows-#{plat}-#{Regexp.last_match(1) == '5' && 'xp' || 'vista'}-#{Regexp.last_match(1)}.#{Regexp.last_match(2)}"
46
+ else
47
+ "windows-#{plat}"
48
+ end
49
+ when /darwin|mac os/
50
+ 'macosx'
51
+ when /linux/
52
+ 'linux'
53
+ when /(solaris|bsd)/
54
+ "unix-#{Regexp.last_match(1)}"
55
+ else
56
+ raise "unknown os: #{host_os.inspect}"
57
+ end
58
+ end
59
+ end
60
+ end
data/rdoba.gemspec CHANGED
@@ -1,5 +1,6 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ $:.push File.expand_path('lib', __dir__)
3
4
  require "rdoba/_version_"
4
5
 
5
6
  Gem::Specification.new do |s|
@@ -23,16 +24,16 @@ Gem::Specification.new do |s|
23
24
  s.extra_rdoc_files = [ 'README.md', 'LICENSE', 'CHANGES.md' ] |
24
25
  `find html/`.split( "\n" )
25
26
 
26
- s.add_development_dependency 'simplecov', '~> 0'
27
- s.add_development_dependency 'tddium', '~> 0'
28
- s.add_development_dependency 'rake', '~> 0'
29
- s.add_development_dependency 'bundler', '~> 1.5'
27
+ s.add_development_dependency 'bundler', '~> 2.0'
28
+ s.add_development_dependency 'coveralls'
30
29
  s.add_development_dependency 'cucumber', '~> 1.3'
31
- s.add_development_dependency 'coveralls', '~> 0'
30
+ s.add_development_dependency 'ffi-stat', '~> 0.4'
31
+ s.add_development_dependency 'rake', '~> 12.0', '>= 12.3.3'
32
32
  s.add_development_dependency 'rdiscount', '~> 2.1'
33
- s.add_development_dependency 'rdoc', '~> 4.2'
33
+ s.add_development_dependency 'rdoc', '~> 6.2'
34
34
  s.add_development_dependency 'rspec-expectations', '~> 3.3'
35
- s.add_development_dependency 'ffi-stat', '~> 0.4'
35
+ s.add_development_dependency 'simplecov', '~> 0'
36
+ s.add_development_dependency 'tddium', '~> 1.25'
36
37
 
37
38
  s.required_rubygems_version = '>= 1.6.0'
38
39
  s.required_ruby_version = '>= 1.9.0' ; end