diff --git a/Rakefile b/Rakefile index 8e8f30f..021464f 100644 --- a/Rakefile +++ b/Rakefile @@ -63,4 +63,3 @@ task :default => :cucumber # task :all => [ :bundleup, :up, :cucumber, :'gem:make', :distclean ] # task :build => [ :bundleup, :up, :cucumber, :rdoc, # :'gem:build', :'gem:install', :distclean ] - diff --git a/features/step_definitions/mixin_steps.rb b/features/step_definitions/mixin_steps.rb index f7ad957..1172140 100644 --- a/features/step_definitions/mixin_steps.rb +++ b/features/step_definitions/mixin_steps.rb @@ -70,8 +70,7 @@ end if @плодъ != плодъ - raise "Плодна Страза со значенꙇемъ '#{@плодъ}' должна имѣти значенꙇе " \ - "'#{плодъ}'" + raise "Плодна Страза со значенꙇемъ '#{@плодъ}' должна имѣти значенꙇе '#{плодъ}'" end end @@ -257,7 +256,8 @@ end То(/^исключение невѣрнѣ опции вызвано будетъ$/) do - expect { @проц.call }.to raise_error(Rdoba::Mixin::InvalidOption) + expect { @проц.call } + .to raise_error(Rdoba::Mixin::InvalidOption) end # #!/usr/bin/ruby -KU diff --git a/features/support/env.rb b/features/support/env.rb index bc92061..8cc6c2b 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -16,13 +16,13 @@ RdobaSimSimpleHead = <<~HEAD #!/usr/bin/env ruby - + require 'rdoba' HEAD RdobaSimClsHead = <<~HEAD #!/usr/bin/env ruby - + require 'rdoba' class Cls HEAD diff --git a/features/support/mixin_support.rb b/features/support/mixin_support.rb index 9630930..b3bb47e 100644 --- a/features/support/mixin_support.rb +++ b/features/support/mixin_support.rb @@ -2,7 +2,8 @@ module MixinSupport def random_string(count) - Random.new.bytes((count + 1) / 2).split('').map do |b| b.ord.to_s(16)end.join[0...count] + Random.new.bytes((count + 1) / 2).split('').map do |b| b.ord.to_s(16) end + .join[0...count] end def tmpfile diff --git a/lib/rdoba/a.rb b/lib/rdoba/a.rb index 3d87b22..c5ffec8 100755 --- a/lib/rdoba/a.rb +++ b/lib/rdoba/a.rb @@ -27,7 +27,7 @@ class Hash def geta_value(cid, options = {}) res = (!cid || cid.empty?) && self || self[cid] || - (options[:сокр] && (self[options[:сокр][cid]] || self[options[:сокр].reverse[cid]])) + (options[:сокр] && (self[options[:сокр][cid]] || self[options[:сокр].reverse[cid]])) if !res and options[:try_regexp] keys.each do |key| diff --git a/lib/rdoba/blank.rb b/lib/rdoba/blank.rb index d928b59..89b4ace 100644 --- a/lib/rdoba/blank.rb +++ b/lib/rdoba/blank.rb @@ -1,14 +1,14 @@ class Object - def blank? - case self - when NilClass, FalseClass - true - when TrueClass - false - when Hash, Array - !self.any? - else - self.to_s == "" - end - end + def blank? + case self + when NilClass, FalseClass + true + when TrueClass + false + when Hash, Array + !self.any? + else + self.to_s == "" + end + end end diff --git a/lib/rdoba/combinations.rb b/lib/rdoba/combinations.rb index a122759..9787e72 100755 --- a/lib/rdoba/combinations.rb +++ b/lib/rdoba/combinations.rb @@ -2,6 +2,28 @@ # frozen_string_literal: true class Array + public + + def each_comby(*args) + return self if empty? or !block_given? + + if args.include?(:backward) + yield [dup] + ((1 << (size - 1)) - 2).downto(0) do |i| + c = __comby(i, size - 1) + yield c + end + else + 0.upto((1 << (size - 1)) - 2) do |i| + c = __comby(i, size - 1) + yield c + end + yield [dup] + end + + self + end + private def __comby(i, size) @@ -36,26 +58,4 @@ def up0(v) up0(v) if v[:c0] > 1 v[:res] end - - public - - def each_comby(*args) - return self if empty? or !block_given? - - if args.include?(:backward) - yield [dup] - ((1 << (size - 1)) - 2).downto(0) do |i| - c = __comby(i, size - 1) - yield c - end - else - 0.upto((1 << (size - 1)) - 2) do |i| - c = __comby(i, size - 1) - yield c - end - yield [dup] - end - - self - end end diff --git a/lib/rdoba/common.rb b/lib/rdoba/common.rb index 89bd9eb..2762ee0 100755 --- a/lib/rdoba/common.rb +++ b/lib/rdoba/common.rb @@ -36,7 +36,7 @@ def parse_opts(opts) end def apply_opts(opts) - parse_opts(opts).each do |x, y| instance_variable_set("@#{x}".to_sym, y)end + parse_opts(opts).each do |x, y| instance_variable_set("@#{x}".to_sym, y) end end end @@ -103,7 +103,7 @@ def -(other) (0...len).each do |idx| break bc = idx if self[idx] == other[0] end - ((bc + 1)...len).each do |idx| break ec = idx if self[idx] != other[idx - bc]end if bc + ((bc + 1)...len).each do |idx| break ec = idx if self[idx] != other[idx - bc] end if bc bc ? ec ? self[0, bc] + self[ec, len - ec] : self[0, bc] : clone end diff --git a/lib/rdoba/debug.rb b/lib/rdoba/debug.rb index 755703b..861ccd4 100644 --- a/lib/rdoba/debug.rb +++ b/lib/rdoba/debug.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true -warn "Warning: the module has kept only for backward compatibility\n" \ - "Please use 'rdoba :log' form instead" +warn "Warning: the module has kept only for backward compatibility\nPlease use 'rdoba :log' form instead" require 'rdoba/log' diff --git a/lib/rdoba/io.rb b/lib/rdoba/io.rb index ecaeb10..2128336 100755 --- a/lib/rdoba/io.rb +++ b/lib/rdoba/io.rb @@ -26,12 +26,12 @@ def sprintf(format, *args) indent = ' ' * (Regexp.last_match(2) == '*' ? args.shift : Regexp.last_match(2)).to_i plain = value && - value.to_p( - padding: - (Regexp.last_match(3) == '*' ? args.shift : Regexp.last_match(3).empty? ? 1 : Regexp.last_match(3)) - .to_i, - be: Regexp.last_match(4).empty? ? nil : true - ) || '' + value.to_p( + padding: + (Regexp.last_match(3) == '*' ? args.shift : Regexp.last_match(3).empty? ? 1 : Regexp.last_match(3)) + .to_i, + be: Regexp.last_match(4).empty? ? nil : true + ) || '' nformat += (Regexp.last_match(1) ? plain + indent : indent + plain) + str else nformat += '%' + keys + 'c' + str @@ -63,32 +63,32 @@ def scanf_re(format) #  TODO add performing the special case in fss[1] nformat += fss.pre_match[pos..-1].to_res + - case fss[4] - when 'x' - '(?:0[xX])?([a-fA-F0-9]+)' - when 'i' - '([+\-]?[0-9]+)' - when 'u' - '([0-9]+)' - when 'e' - '([+\-]?[0-9]+[eE][+\-]?[0-9]+)' - when 'f' - '([+\-]?[0-9]+\.[0-9]*)' - when 'g' - '([+\-]?[0-9]+(?:[eE][+\-]?[0-9]+|\.[0-9]*))' - when 'c' - fss[2] ? "(.{1,#{fss[2]}})" : '(.)' - when 'b' - '([01]+)b?' - when 'o' - '0([0-9]+)' - when 'd' - '([+\-]?(?:0X)?[A-F0-9.+]+)' - when 's' - '(.+)' - when 'r' - '([IVXLCDMivxlcdm]+)' - end + case fss[4] + when 'x' + '(?:0[xX])?([a-fA-F0-9]+)' + when 'i' + '([+\-]?[0-9]+)' + when 'u' + '([0-9]+)' + when 'e' + '([+\-]?[0-9]+[eE][+\-]?[0-9]+)' + when 'f' + '([+\-]?[0-9]+\.[0-9]*)' + when 'g' + '([+\-]?[0-9]+(?:[eE][+\-]?[0-9]+|\.[0-9]*))' + when 'c' + fss[2] ? "(.{1,#{fss[2]}})" : '(.)' + when 'b' + '([01]+)b?' + when 'o' + '0([0-9]+)' + when 'd' + '([+\-]?(?:0X)?[A-F0-9.+]+)' + when 's' + '(.+)' + when 'r' + '([IVXLCDMivxlcdm]+)' + end pos = fss.pos end diff --git a/lib/rdoba/mixin.rb b/lib/rdoba/mixin.rb index 20a5f6e..d962511 100755 --- a/lib/rdoba/mixin.rb +++ b/lib/rdoba/mixin.rb @@ -20,8 +20,10 @@ def crop_diacritics(x) (x < 0x300 || x > 0x36f && x < 0x483 || x > 0x487 && x < 0xa67c || x > 0xa67d) && x || nil end - (unpack('U*').map do |x| crop_diacritics(x)end.compact) <=> - (value.unpack('U*').map do |x| crop_diacritics(x)end.compact) + (unpack('U*').map do |x| crop_diacritics(x) end +.compact) <=> + (value.unpack('U*').map do |x| crop_diacritics(x) end +.compact) else self <=> value end @@ -295,7 +297,7 @@ module Split_byArray # def split_by idxs = [] - rejected = reject.with_index do |v, i| yield(v) && (idxs << i)end + rejected = reject.with_index do |v, i| yield(v) && (idxs << i) end [values_at(*idxs), rejected] end end diff --git a/lib/rdoba/os.rb b/lib/rdoba/os.rb index 887a5a8..d169923 100644 --- a/lib/rdoba/os.rb +++ b/lib/rdoba/os.rb @@ -3,125 +3,131 @@ require 'rdoba/blank' class Object - def to_os - OpenStruct.new(self.to_h.map {|(x, y)| [x.to_s, [Hash, Array].include?(y.class) && y.to_os || y] }.to_h) - end + def to_os + OpenStruct.new( + self.to_h.map { |(x, y)| [x.to_s, [Hash, Array].include?(y.class) && y.to_os || y] } + .to_h + ) + end end class Array - def to_os - OpenStruct.new(self.map.with_index {|y, x| [x.to_s, [Hash, Array].include?(y.class) && y.to_os || y] }.to_h) - end + def to_os + OpenStruct.new( + self.map.with_index { |y, x| [x.to_s, [Hash, Array].include?(y.class) && y.to_os || y] } + .to_h + ) + end end class Integer - def to_sym - to_s.to_sym - end + def to_sym + to_s.to_sym + end end class OpenStruct - def merge_to other - OpenStruct.new(other.to_h.merge(self.to_h)) - end - - def merge other - OpenStruct.new(self.to_h.merge(other.to_h)) - end - - def map *args, &block - res = self.class.new - - self.each_pair do |key, value| - res[key] = block[key, value] - end - - res - end - - def select &block - res = self.class.new - - self.each_pair do |key, value| - res[key] = value if block[key, value] - end - - res - end - - def compact - select { |_, value| !value.blank? } - end - - def each *args, &block - self.each_pair(*args, &block) - end - - def reduce default = nil, &block - res = default - - self.each_pair do |key, value| - res = block[res, key, value] + def merge_to other + OpenStruct.new(other.to_h.merge(self.to_h)) + end + + def merge other + OpenStruct.new(self.to_h.merge(other.to_h)) + end + + def map *args, &block + res = self.class.new + + self.each_pair do |key, value| + res[key] = block[key, value] + end + + res + end + + def select &block + res = self.class.new + + self.each_pair do |key, value| + res[key] = value if block[key, value] + end + + res + end + + def compact + select { |_, value| !value.blank? } + end + + def each *args, &block + self.each_pair(*args, &block) + end + + def reduce default = nil, &block + res = default + + self.each_pair do |key, value| + res = block[res, key, value] + end + + res + end + + # +deep_merge+ deeply merges the Open Struct hash structure with the +other_in+ enumerating it key by key. + # +options+ are the options to change behaviour of the method. It allows two keys: :mode, and :dedup + # :mode key can be :append, :prepend, or :replace, defaulting to :append, when mode is to append, it combines duplicated + # keys' values into an array, when :prepend it prepends an other value before previously stored one unlike for :append mode, + # when :replace it replace duplicate values with a last ones. + # :dedup key can be true, or false. It allows to deduplicate values when appending or prepending values. + # Examples: + # open_struct.deep_merge(other_open_struct) + # open_struct.deep_merge(other_open_struct, :prepend) + # + def deep_merge other_in, options_in = {} + return self if other_in.nil? or other_in.blank? + + options = { mode: :append }.merge(options_in) + + other = + if other_in.is_a?(OpenStruct) + other_in.dup + elsif other_in.is_a?(Hash) + other_in.to_os + else + OpenStruct.new(nil => other_in) end - res - end - - # +deep_merge+ deeply merges the Open Struct hash structure with the +other_in+ enumerating it key by key. - # +options+ are the options to change behaviour of the method. It allows two keys: :mode, and :dedup - # :mode key can be :append, :prepend, or :replace, defaulting to :append, when mode is to append, it combines duplicated - # keys' values into an array, when :prepend it prepends an other value before previously stored one unlike for :append mode, - # when :replace it replace duplicate values with a last ones. - # :dedup key can be true, or false. It allows to deduplicate values when appending or prepending values. - # Examples: - # open_struct.deep_merge(other_open_struct) - # open_struct.deep_merge(other_open_struct, :prepend) - # - def deep_merge other_in, options_in = {} - return self if other_in.nil? or other_in.blank? - - options = { mode: :append }.merge(options_in) - - other = - if other_in.is_a?(OpenStruct) - other_in.dup - elsif other_in.is_a?(Hash) - other_in.to_os - else - OpenStruct.new(nil => other_in) - end - - self.reduce(other) do |res, key, value| - res[key] = - if res.table.keys.include?(key) - case value - when Hash, OpenStruct - value.deep_merge(res[key], options) - when Array - value.concat([res[key]].compact.flatten(1)) - when NilClass - res[key] - else - value_out = - if options[:mode] == :append - [res[key], value].compact.flatten(1) - elsif options[:mode] == :prepend - [value, res[key]].compact.flatten(1) - else - value - end - - if value_out.is_a?(Array) && options[:dedup] - value_out.uniq - else - value_out - end - end + self.reduce(other) do |res, key, value| + res[key] = + if res.table.keys.include?(key) + case value + when Hash, OpenStruct + value.deep_merge(res[key], options) + when Array + value.concat([res[key]].compact.flatten(1)) + when NilClass + res[key] + else + value_out = + if options[:mode] == :append + [res[key], value].compact.flatten(1) + elsif options[:mode] == :prepend + [value, res[key]].compact.flatten(1) + else + value + end + + if value_out.is_a?(Array) && options[:dedup] + value_out.uniq else - value + value_out end + end + else + value + end - res - end - end + res + end + end end diff --git a/lib/rdoba/strings.rb b/lib/rdoba/strings.rb index d31375d..f74bc25 100755 --- a/lib/rdoba/strings.rb +++ b/lib/rdoba/strings.rb @@ -1,8 +1,7 @@ #!/usr/bin/ruby -KU # frozen_string_literal: true -warn "Warning: the module 'string' has kept only for backward " \ - "compatibility\nPlease use 'rdoba :mixin' form instead" +warn "Warning: the module 'string' has kept only for backward compatibility\nPlease use 'rdoba :mixin' form instead" class String alias to_p to_s diff --git a/rdoba.gemspec b/rdoba.gemspec index 33158a3..5fbd233 100644 --- a/rdoba.gemspec +++ b/rdoba.gemspec @@ -4,36 +4,36 @@ $:.push File.expand_path('lib', __dir__) require "rdoba/_version_" Gem::Specification.new do |s| - s.name = "rdoba" - s.version = Rdoba::VERSION - s.platform = Gem::Platform::RUBY - s.authors = [ 'Малъ Скрылёвъ (Malo Skrylevo)' ] - s.email = [ '3aHyga@gmail.com' ] - s.homepage = 'https://github.com/3aHyga/rdoba' - s.summary = 'Ruby extension library (Ruby DOBAvka)' - s.description = 'Ruby extension library. It extends Kernel, Object, ' \ - 'String, Hash, Array, and some other classes. Also allows ' \ - 'to log application state with debug lines to an io' - s.license = 'MIT' + s.name = "rdoba" + s.version = Rdoba::VERSION + s.platform = Gem::Platform::RUBY + s.authors = ['Малъ Скрылёвъ (Malo Skrylevo)'] + s.email = ['3aHyga@gmail.com'] + s.homepage = 'https://github.com/3aHyga/rdoba' + s.summary = 'Ruby extension library (Ruby DOBAvka)' + s.description = 'Ruby extension library. It extends Kernel, Object, ' \ + 'String, Hash, Array, and some other classes. Also allows ' \ + 'to log application state with debug lines to an io' + s.license = 'MIT' - s.rubyforge_project = "rdoba" - s.files = `git ls-files`.split("\n") - s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") - s.executables = `git ls-files -- bin/*`.split( "\n" ).map{ |f| File.basename(f) } - s.require_paths = [ "lib" ] - s.extra_rdoc_files = [ 'README.md', 'LICENSE', 'CHANGES.md' ] | - `find html/`.split( "\n" ) + s.rubyforge_project = "rdoba" + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) } + s.require_paths = ["lib"] + s.extra_rdoc_files = ['README.md', 'LICENSE', 'CHANGES.md'] | `find html/`.split("\n") - s.add_development_dependency 'bundler', '~> 2.0' - s.add_development_dependency 'coveralls' - s.add_development_dependency 'cucumber', '~> 1.3' - s.add_development_dependency 'ffi-stat', '~> 0.4' - s.add_development_dependency 'rake', '~> 12.0', '>= 12.3.3' - s.add_development_dependency 'rdiscount', '~> 2.1' - s.add_development_dependency 'rdoc', '~> 6.2' - s.add_development_dependency 'rspec-expectations', '~> 3.3' - s.add_development_dependency 'simplecov', '~> 0' - s.add_development_dependency 'tddium', '~> 1.25' + s.add_development_dependency 'bundler', '~> 2.0' + s.add_development_dependency 'coveralls' + s.add_development_dependency 'cucumber', '~> 1.3' + s.add_development_dependency 'ffi-stat', '~> 0.4' + s.add_development_dependency 'rake', '~> 12.0', '>= 12.3.3' + s.add_development_dependency 'rdiscount', '~> 2.1' + s.add_development_dependency 'rdoc', '~> 6.2' + s.add_development_dependency 'rspec-expectations', '~> 3.3' + s.add_development_dependency 'simplecov', '~> 0' + s.add_development_dependency 'tddium', '~> 1.25' - s.required_rubygems_version = '>= 1.6.0' - s.required_ruby_version = '>= 1.9.0' ; end + s.required_rubygems_version = '>= 1.6.0' + s.required_ruby_version = '>= 1.9.0' +end