ruby_core_extensions 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +21 -0
  3. data/CHANGELOG.md +22 -0
  4. data/Rakefile +1 -1
  5. data/gemfiles/rails3.gemfile +1 -1
  6. data/gemfiles/rails4.gemfile +1 -1
  7. data/lib/ruby_core_extensions/array.rb +1 -10
  8. data/lib/ruby_core_extensions/class.rb +0 -3
  9. data/lib/ruby_core_extensions/compact/array.rb +1 -2
  10. data/lib/ruby_core_extensions/compact/hash.rb +3 -4
  11. data/lib/ruby_core_extensions/enumerable.rb +0 -3
  12. data/lib/ruby_core_extensions/file.rb +0 -1
  13. data/lib/ruby_core_extensions/hash.rb +4 -6
  14. data/lib/ruby_core_extensions/kernel.rb +0 -2
  15. data/lib/ruby_core_extensions/numeric.rb +0 -2
  16. data/lib/ruby_core_extensions/object.rb +5 -7
  17. data/lib/ruby_core_extensions/recursive/array.rb +42 -44
  18. data/lib/ruby_core_extensions/recursive/hash.rb +14 -17
  19. data/lib/ruby_core_extensions/recursive/object.rb +8 -4
  20. data/lib/ruby_core_extensions/recursive.rb +0 -5
  21. data/lib/ruby_core_extensions/string.rb +8 -9
  22. data/lib/ruby_core_extensions/version.rb +1 -1
  23. data/ruby_core_extensions.gemspec +3 -1
  24. data/spec/array_spec.rb +33 -12
  25. data/spec/compact_spec.rb +13 -13
  26. data/spec/enumerable_spec.rb +13 -9
  27. data/spec/filename_spec.rb +0 -1
  28. data/spec/hash_spec.rb +41 -30
  29. data/spec/object_spec.rb +4 -5
  30. data/spec/string_spec.rb +3 -1
  31. data/spec/support/coverage.rb +4 -29
  32. metadata +20 -9
  33. data/lib/ruby_core_extensions/recursive/big_decimal.rb +0 -5
  34. data/lib/ruby_core_extensions/recursive/date.rb +0 -8
  35. data/lib/ruby_core_extensions/recursive/date_time.rb +0 -8
  36. data/lib/ruby_core_extensions/recursive/fixnum.rb +0 -7
  37. data/lib/ruby_core_extensions/recursive/time.rb +0 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73d409278a49f15845c54d39663e24a0942bd004
4
- data.tar.gz: 61a669479de59c7b8e9105d674cc44d28824151a
3
+ metadata.gz: 702cb94d4f6195649a0c4ed456219c865a4883db
4
+ data.tar.gz: b03377cd78457c6949efc0278ae875d3a243074d
5
5
  SHA512:
6
- metadata.gz: addb261d51cdd07ced187a5efefa83d474d712875133108585a255be62168add46445b532a7816292466110c8766720394f91012c2b38db2e584a70830b700de
7
- data.tar.gz: 3e37676250b767f9d97808b353e25e187c386de97f99ec457ff8a6823a7c25fbb8c74195e3488e0ed367de7db71aabb5b18ed9e600d10b558b5d839a908cbe47
6
+ metadata.gz: 24b92150e349a329f51e9f88b7f5e6ff491f583e2e26d93b394605debc86274171e1a7f339b1cd27dde2e197987ee668f14cba88c194c5c77c532948ea7a624a
7
+ data.tar.gz: c22a0acd4cea5b4cc86afbd035203ee7a87c4742ccc6b8c68cc810f2d848fcb25bb320d6ab01c7cdb09138b98efa1284507a215f160b23398550a532d73ae6d5
data/.rubocop.yml ADDED
@@ -0,0 +1,21 @@
1
+ inherit_gem:
2
+ rubocop-rails:
3
+ - config/rails.yml
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.4
7
+
8
+ Style/FrozenStringLiteralComment:
9
+ Enabled: false
10
+
11
+ Style/StringLiterals:
12
+ Enabled: false
13
+
14
+ Layout/IndentationWidth:
15
+ IgnoredPatterns: ['^\s*private$']
16
+
17
+ Style/BracesAroundHashParameters:
18
+ Enabled: false
19
+
20
+ Metrics/LineLength:
21
+ Max: 100
data/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
1
+ # Change Log
2
+ All notable changes to this project will be documented in this file.
3
+ This project adheres to [Semantic Versioning](http://semver.org/).
4
+ This changelog adheres to [Keep a CHANGELOG](http://keepachangelog.com/).
5
+
6
+ ## Unreleased
7
+
8
+ ## 0.1.0
9
+
10
+ ### Added
11
+ - [TT-4020] Implemented Rubocop
12
+
13
+ ### Removed
14
+ - Remove no longer supported Array methods #to_param/#show_name
15
+
16
+ ### Changed
17
+ - stringify_values_recursively now just works on to_s for all objects
18
+
19
+ ## 0.0.1
20
+
21
+ ### Added
22
+ - [TT-1392] Changelog file
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
2
 
3
3
  desc 'Default: run specs.'
4
- task :default => :spec
4
+ task default: :spec
5
5
 
6
6
  require 'rspec/core/rake_task'
7
7
 
@@ -1,5 +1,5 @@
1
1
  source 'https://rubygems.org'
2
- gemspec :path => '../'
2
+ gemspec path: '../'
3
3
 
4
4
  group :development, :test do
5
5
  gem 'rake'
@@ -1,5 +1,5 @@
1
1
  source 'https://rubygems.org'
2
- gemspec :path => '../'
2
+ gemspec path: '../'
3
3
 
4
4
  group :development, :test do
5
5
  gem 'rake'
@@ -1,13 +1,4 @@
1
1
  class Array
2
-
3
- def to_param
4
- self.collect { |element| element.respond_to?(:to_param) ? element.to_param : element }
5
- end
6
-
7
- def show_name
8
- first.titleize
9
- end
10
-
11
2
  # Key should be unique, or latest element with that key will override previous ones.
12
3
  def hash_by(key = nil, method = nil, &block)
13
4
  self.inject({}) do |h, element|
@@ -31,7 +22,7 @@ class Array
31
22
  end
32
23
 
33
24
  def intersects?(other)
34
- self.any?{|i| other.include?(i)}
25
+ self.any? { |i| other.include?(i) }
35
26
  end
36
27
 
37
28
  # Same effect as Array.wrap(object).first
@@ -1,8 +1,5 @@
1
1
  class Class
2
-
3
2
  def downcase_symbol
4
3
  self.to_s.downcase.to_sym
5
4
  end
6
-
7
5
  end
8
-
@@ -1,6 +1,6 @@
1
1
  class Array
2
2
  def compact_blank!
3
- delete_if{|v| v.blank?}
3
+ delete_if { |v| v.blank? }
4
4
  end
5
5
 
6
6
  def recursive_compact_blank!
@@ -17,4 +17,3 @@ class Array
17
17
  end
18
18
  end
19
19
  end
20
-
@@ -6,7 +6,7 @@ class Hash
6
6
 
7
7
  # Remove nil values - !desctructively!
8
8
  def compact!
9
- delete_if{|k,v| v.nil?}
9
+ delete_if { |k, v| v.nil? }
10
10
  end
11
11
 
12
12
  def compact_blank
@@ -14,11 +14,11 @@ class Hash
14
14
  end
15
15
 
16
16
  def compact_blank!
17
- delete_if{|k,v| v.blank?}
17
+ delete_if { |k, v| v.blank? }
18
18
  end
19
19
 
20
20
  def recursive_compact_blank!
21
- delete_if do |k,v|
21
+ delete_if do |k, v|
22
22
  if v.is_a?(Hash)
23
23
  v.recursive_compact_blank!
24
24
  v.recursive_blank?
@@ -31,4 +31,3 @@ class Hash
31
31
  end
32
32
  end
33
33
  end
34
-
@@ -1,5 +1,4 @@
1
1
  module Enumerable
2
-
3
2
  def map_methods(*methods)
4
3
  map do |object|
5
4
  methods.inject({}) do |h, method|
@@ -34,6 +33,4 @@ module Enumerable
34
33
  obj
35
34
  end
36
35
  end
37
-
38
36
  end
39
-
@@ -9,4 +9,3 @@ class File
9
9
  gsub(/\-+/, '-') # limit - character to once
10
10
  end
11
11
  end
12
-
@@ -1,7 +1,6 @@
1
1
  class Hash
2
-
3
2
  unless self.method_defined?(:extract!)
4
- #Imported from Rails 3
3
+ # Imported from Rails 3
5
4
  def extract!(*keys)
6
5
  result = {}
7
6
  keys.each { |key| result[key] = delete(key) }
@@ -12,23 +11,22 @@ class Hash
12
11
 
13
12
  def map_key_value(key_method, value_method = nil)
14
13
  value_method ||= key_method
15
- each.with_object({}) do |(k,v), new_hash|
14
+ each.with_object({}) do |(k, v), new_hash|
16
15
  new_hash[k.send(key_method)] = v.send(value_method)
17
16
  end
18
17
  end
19
18
 
20
19
 
21
20
  def map_key(method)
22
- each.with_object({}) do |(k,v), new_hash|
21
+ each.with_object({}) do |(k, v), new_hash|
23
22
  new_hash[k.send(method)] = v
24
23
  end
25
24
  end
26
25
 
27
26
 
28
27
  def map_value(method)
29
- each.with_object({}) do |(k,v), new_hash|
28
+ each.with_object({}) do |(k, v), new_hash|
30
29
  new_hash[k] = v.send(method)
31
30
  end
32
31
  end
33
-
34
32
  end
@@ -1,5 +1,4 @@
1
1
  module Kernel
2
-
3
2
  def require_dirs(dirs)
4
3
  Array.wrap(dirs).each do |load_path|
5
4
  Dir.glob("#{load_path}/**/*.rb").each do |file|
@@ -7,5 +6,4 @@ module Kernel
7
6
  end
8
7
  end
9
8
  end
10
-
11
9
  end
@@ -1,7 +1,5 @@
1
1
  class Numeric
2
-
3
2
  def to_bool
4
3
  !zero?
5
4
  end
6
-
7
5
  end
@@ -3,16 +3,16 @@ class Object
3
3
  def sounds_like?(other)
4
4
  self.phonetic_code == other.phonetic_code
5
5
  end
6
-
6
+
7
7
  # Convert this object into a string, then convert that to phonetic code
8
8
  def phonetic_code
9
9
  self.to_s.phonetic_code
10
10
  end
11
-
11
+
12
12
  def to_long_s
13
13
  to_s
14
14
  end
15
-
15
+
16
16
  def virtual_belongs_to(*associations)
17
17
  options = associations.extract_options!
18
18
 
@@ -62,11 +62,9 @@ class Object
62
62
  end
63
63
  EVAL
64
64
  end
65
-
66
-
65
+
66
+
67
67
  def to_bool
68
68
  self.to_s.to_bool
69
69
  end
70
-
71
70
  end
72
-
@@ -1,45 +1,43 @@
1
- class Array
2
-
3
- def convert
4
- self
5
- end
1
+ class Array
2
+ def convert
3
+ self
4
+ end
6
5
 
7
- def convert_keys_recursively(&converter)
8
- map { |v| v.convert_keys_recursively(&converter) }
9
- end
10
-
11
- def convert_values_recursively(&converter)
12
- map { |v| v.convert_values_recursively(&converter) }
13
- end
14
-
15
- def symbolize_keys_recursively
16
- map(&:symbolize_keys_recursively)
17
- end
18
-
19
- def stringify_values_recursively
20
- map(&:stringify_values_recursively)
21
- end
22
-
23
- def make_indifferent_access_recursively
24
- map(&:make_indifferent_access_recursively)
25
- end
26
-
27
- def recursive_blank?
28
- each do |v|
29
- if v.respond_to?(:recursive_blank?)
30
- return false unless v.recursive_blank?
31
- else
32
- return false unless v.blank?
33
- end
34
- end
35
- true
36
- end
37
-
38
- def recursively(&block)
39
- each do |element|
40
- block.call(element)
41
- element.recursively(&block) if element.respond_to?(:recursively)
42
- end
43
- end
44
-
45
- end
6
+ def convert_keys_recursively(&converter)
7
+ map { |v| v.convert_keys_recursively(&converter) }
8
+ end
9
+
10
+ def convert_values_recursively(&converter)
11
+ map { |v| v.convert_values_recursively(&converter) }
12
+ end
13
+
14
+ def symbolize_keys_recursively
15
+ map(&:symbolize_keys_recursively)
16
+ end
17
+
18
+ def stringify_values_recursively
19
+ map(&:stringify_values_recursively)
20
+ end
21
+
22
+ def make_indifferent_access_recursively
23
+ map(&:make_indifferent_access_recursively)
24
+ end
25
+
26
+ def recursive_blank?
27
+ each do |v|
28
+ if v.respond_to?(:recursive_blank?)
29
+ return false unless v.recursive_blank?
30
+ else
31
+ return false unless v.blank?
32
+ end
33
+ end
34
+ true
35
+ end
36
+
37
+ def recursively(&block)
38
+ each do |element|
39
+ block.call(element)
40
+ element.recursively(&block) if element.respond_to?(:recursively)
41
+ end
42
+ end
43
+ end
@@ -1,5 +1,4 @@
1
1
  class Hash
2
-
3
2
  def recursive_blank?
4
3
  each do |k, v|
5
4
  if v.respond_to?(:recursive_blank?)
@@ -10,18 +9,18 @@ class Hash
10
9
  end
11
10
  true
12
11
  end
13
-
12
+
14
13
  def convert
15
14
  self
16
15
  end
17
-
16
+
18
17
  def convert_keys(&converter)
19
18
  inject({}) do |hash, (key, value)|
20
19
  hash[converter.call(key)] = value
21
20
  hash
22
21
  end
23
22
  end
24
-
23
+
25
24
  def convert_values(*keys, &converter)
26
25
  inject(clone) do |hash, (key, value)|
27
26
  hash[key] = value.convert(&converter) if keys.blank? || keys.include?(key)
@@ -33,54 +32,52 @@ class Hash
33
32
  Hash[map do |key, value|
34
33
  k = converter.call(key)
35
34
  v = value.convert_keys_recursively(&converter)
36
- [k,v]
35
+ [k, v]
37
36
  end]
38
37
  end
39
-
38
+
40
39
  def convert_values_recursively(&converter)
41
40
  inject({}) do |hash, (key, value)|
42
41
  hash[key] = value.convert_values_recursively(&converter)
43
42
  hash
44
43
  end
45
44
  end
46
-
45
+
47
46
  def symbolize_keys_recursively
48
47
  Hash[map do |key, value|
49
48
  k = key.is_a?(String) ? key.to_sym : key
50
49
  v = value.symbolize_keys_recursively
51
- [k,v]
50
+ [k, v]
52
51
  end]
53
52
  end
54
-
53
+
55
54
  def stringify_values_recursively
56
55
  inject({}) do |options, (key, value)|
57
56
  options[key] = value.stringify_values_recursively
58
57
  options
59
58
  end
60
59
  end
61
-
60
+
62
61
  def make_indifferent_access_recursively
63
62
  HashWithIndifferentAccess.new(inject({}) do |options, (key, value)|
64
63
  options[key] = value.make_indifferent_access_recursively
65
64
  options
66
65
  end)
67
66
  end
68
-
67
+
69
68
  def deep_dup
70
69
  duplicate = self.dup
71
- duplicate.each_pair do |k,v|
70
+ duplicate.each_pair do |k, v|
72
71
  tv = duplicate[k]
73
72
  duplicate[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_dup : v
74
73
  end
75
74
  duplicate
76
75
  end
77
-
76
+
78
77
  def recursively(&block)
79
- each do |key,value|
80
- block.call(key,value)
78
+ each do |key, value|
79
+ block.call(key, value)
81
80
  value.recursively(&block) if value.respond_to?(:recursively)
82
81
  end
83
82
  end
84
-
85
83
  end
86
-
@@ -2,16 +2,20 @@ class Object
2
2
  def convert(&converter)
3
3
  converter.call(self)
4
4
  end
5
-
5
+
6
6
  def return_self
7
7
  self
8
8
  end
9
-
9
+
10
+ def return_to_s
11
+ to_s
12
+ end
13
+
10
14
  alias_method :convert_values_recursively, :convert
11
15
  alias_method :convert_recursively, :convert
12
-
16
+
13
17
  alias_method :convert_keys_recursively, :return_self
14
18
  alias_method :symbolize_keys_recursively, :return_self
15
- alias_method :stringify_values_recursively, :return_self
19
+ alias_method :stringify_values_recursively, :return_to_s
16
20
  alias_method :make_indifferent_access_recursively, :return_self
17
21
  end
@@ -1,8 +1,3 @@
1
1
  require 'ruby_core_extensions/recursive/array'
2
- require 'ruby_core_extensions/recursive/big_decimal'
3
- require 'ruby_core_extensions/recursive/date'
4
- require 'ruby_core_extensions/recursive/date_time'
5
- require 'ruby_core_extensions/recursive/fixnum'
6
2
  require 'ruby_core_extensions/recursive/hash'
7
3
  require 'ruby_core_extensions/recursive/object'
8
- require 'ruby_core_extensions/recursive/time'
@@ -1,8 +1,8 @@
1
1
  class String
2
2
  def proper_underscore
3
- self.titleize.gsub(" ","").underscore
3
+ self.titleize.gsub(" ", "").underscore
4
4
  end
5
-
5
+
6
6
  # Generate a phonetic code - which is the same for similar sounding names
7
7
  def phonetic_code
8
8
  # Currently using 'metaphone' which is more accurate than soundex
@@ -11,7 +11,7 @@ class String
11
11
 
12
12
  # Solr requires numbers and letters to be separated
13
13
  def separate_numbers_and_letters
14
- gsub(/[a-z][0-9]|[0-9][a-z]/i){ |s| s[0].chr + ' ' + s[1].chr }
14
+ gsub(/[a-z][0-9]|[0-9][a-z]/i) { |s| s[0].chr + ' ' + s[1].chr }
15
15
  end
16
16
 
17
17
  # convert newlines to breaks
@@ -42,13 +42,13 @@ class String
42
42
  words = split(' ')
43
43
 
44
44
  # Return first letter of <limit> words
45
- return words.first(limit).map{|w| w.chars.first}.join if (words.size * 2 - 1) >= limit
45
+ return words.first(limit).map { |w| w.chars.first }.join if (words.size * 2 - 1) >= limit
46
46
 
47
47
  spaces = words.size - 1
48
48
  word_char_min = (limit - spaces) / words.size
49
49
  word_char_max = word_char_min + 1
50
50
 
51
- words = words.map{|word| word[0..(word_char_max - 1)]}
51
+ words = words.map { |word| word[0..(word_char_max - 1)] }
52
52
 
53
53
  words.reverse.each.with_index do |word, index|
54
54
  letters_to_remove = words.join(' ').size - limit
@@ -60,19 +60,18 @@ class String
60
60
  end
61
61
 
62
62
  # Replace word
63
- words[words.size - index - 1] = word[0..(letters_to_keep - 1)]
63
+ words[words.size - index - 1] = word[0..(letters_to_keep - 1)]
64
64
 
65
65
  break if last_case
66
66
  end
67
67
 
68
68
  words.join(' ')
69
69
  end
70
-
71
-
70
+
71
+
72
72
  def to_bool
73
73
  return true if self == true || self =~ (/(true|t|yes|y|1)$/i)
74
74
  return false if self == false || self.blank? || self =~ (/(false|f|no|n|0)$/i)
75
75
  raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
76
76
  end
77
77
  end
78
-
@@ -1,3 +1,3 @@
1
1
  module RubyCoreExtensions
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -1,4 +1,5 @@
1
1
  # coding: utf-8
2
+
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'ruby_core_extensions/version'
@@ -25,8 +26,9 @@ Gem::Specification.new do |spec|
25
26
  spec.add_development_dependency "bundler", "~> 1.3"
26
27
  spec.add_development_dependency "rake"
27
28
  spec.add_development_dependency 'rspec'
28
- spec.add_development_dependency 'simplecov'
29
+ spec.add_development_dependency 'coverage-kit'
29
30
  spec.add_development_dependency 'simplecov-rcov'
30
31
  spec.add_development_dependency 'coveralls'
31
32
  spec.add_development_dependency 'travis'
33
+ spec.add_development_dependency 'rubocop-rails'
32
34
  end
data/spec/array_spec.rb CHANGED
@@ -1,10 +1,21 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Array do
4
-
5
4
  it "should allow converting all values to strings recursively" do
6
- @now = Time.now
7
- expect([1, 2, @now, [3, 4]].stringify_values_recursively).to eq ['1', '2', @now.to_s, ['3', '4']]
5
+ to_s_class = Class.new do
6
+ def to_s
7
+ 'Myself'
8
+ end
9
+ end
10
+ stringify_values_recursively_class = Class.new do
11
+ def stringify_values_recursively
12
+ 'Special'
13
+ end
14
+ end
15
+ now = Time.now
16
+ array = [1, 2, now, [3, 4], to_s_class.new, stringify_values_recursively_class.new]
17
+ output = ['1', '2', now.to_s, %w[3 4], 'Myself', 'Special']
18
+ expect(array.stringify_values_recursively).to eq output
8
19
  end
9
20
 
10
21
  it "should allow removing all blank values" do
@@ -14,30 +25,40 @@ describe Array do
14
25
  end
15
26
 
16
27
  it "should allow removing all blank values recursively" do
17
- a = [1, 2, [" Kan", {}], nil, {:a => "", :b => {}}, ["garoos", " "]]
28
+ a = [1, 2, [" Kan", {}], nil, { a: "", b: {} }, ["garoos", " "]]
18
29
  a.recursive_compact_blank!
19
30
  expect(a.join).to eq "12 Kangaroos"
20
31
  end
21
32
 
22
33
  it "should allow verifying if all elements are blank recursively" do
23
- expect(['',nil,[nil,['']]]).to be_recursive_blank
24
- expect(['',nil,[nil,['',1]]]).to_not be_recursive_blank
34
+ expect(['', nil, [nil, ['']]]).to be_recursive_blank
35
+ expect(['', nil, [nil, ['', 1]]]).to_not be_recursive_blank
25
36
  end
26
37
 
27
38
  it "should allow converting to hash given a key" do
28
- expect([1,2,3].hash_by(:ordinalize)).to eq({'1st' => 1, "2nd" => 2, "3rd" => 3})
29
- expect([1,2,3].hash_by(:ordinalize, :to_s)).to eq({'1st' => '1', "2nd" => '2', "3rd" => '3'})
30
- expect([1,2,3].hash_by(:ordinalize){ |v| v + 1 }).to eq({'1st' => 2, "2nd" => 3, "3rd" => 4})
31
- expect([1,2,3].hash_by{|k| k * 2}).to eq({2 => 1, 4 => 2, 6 => 3})
39
+ expect([1, 2, 3].hash_by(:ordinalize)).to eq({ '1st' => 1, "2nd" => 2, "3rd" => 3 })
40
+ expect([1, 2, 3].hash_by(:ordinalize, :to_s)).to eq(
41
+ { '1st' => '1', "2nd" => '2', "3rd" => '3' }
42
+ )
43
+ expect([1, 2, 3].hash_by(:ordinalize) { |v| v + 1 }).to eq(
44
+ { '1st' => 2, "2nd" => 3, "3rd" => 4 }
45
+ )
46
+ expect([1, 2, 3].hash_by { |k| k * 2 }).to eq({ 2 => 1, 4 => 2, 6 => 3 })
47
+ end
48
+
49
+ it "#hash_by_id" do
50
+ one = double(id: 1, name: 'One')
51
+ two = double(id: 2, name: 'Two')
52
+ expect([one, two].hash_by_id).to eq(1 => one, 2 => two)
32
53
  end
33
54
 
34
55
  it "should allow executing blocks recursively" do
35
- array = [1,[2,3],[4,[5,6],7,[8]],9,[[10]]]
56
+ array = [1, [2, 3], [4, [5, 6], 7, [8]], 9, [[10]]]
36
57
  result = []
37
58
  array.recursively do |e|
38
59
  result << e unless e.is_a?(Array)
39
60
  end
40
- expect(result).to eq [1,2,3,4,5,6,7,8,9,10]
61
+ expect(result).to eq [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
41
62
  end
42
63
 
43
64
  it '#intersects?' do
data/spec/compact_spec.rb CHANGED
@@ -6,11 +6,11 @@ describe Array, 'compact' do
6
6
  end
7
7
 
8
8
  it 'should recursive compact blank' do
9
- a = [1, nil, {:a => nil, :b => 2}]
9
+ a = [1, nil, { a: nil, b: 2 }]
10
10
  a.recursive_compact_blank!
11
- expect(a).to eq [1, {:b => 2}]
11
+ expect(a).to eq [1, { b: 2 }]
12
12
 
13
- b = [1, nil, {:a => nil}]
13
+ b = [1, nil, { a: nil }]
14
14
  b.recursive_compact_blank!
15
15
  expect(b).to eq [1]
16
16
  end
@@ -18,32 +18,32 @@ end
18
18
 
19
19
  describe Hash, 'compact' do
20
20
  it 'should compact' do
21
- expect({:a => 1, :b => nil}.compact).to eq({:a => 1})
21
+ expect({ a: 1, b: nil }.compact).to eq({ a: 1 })
22
22
  end
23
23
 
24
24
  it 'should compact!' do
25
- a = {:a => 1, :b => nil}
25
+ a = { a: 1, b: nil }
26
26
  a.compact!
27
- expect(a).to eq({:a => 1})
27
+ expect(a).to eq({ a: 1 })
28
28
  end
29
29
 
30
30
  it 'should compact blank' do
31
- expect({:a => 1, :b => ''}.compact_blank).to eq({:a => 1})
31
+ expect({ a: 1, b: '' }.compact_blank).to eq({ a: 1 })
32
32
  end
33
33
 
34
34
  it 'should compact blank!' do
35
- a = {:a => 1, :b => ''}
35
+ a = { a: 1, b: '' }
36
36
  a.compact_blank!
37
- expect(a).to eq({:a => 1})
37
+ expect(a).to eq({ a: 1 })
38
38
  end
39
39
 
40
40
  it 'should recursive compact blank!' do
41
- a = {:a => 1, :b => {:c => 1, :d => ''}}
41
+ a = { a: 1, b: { c: 1, d: '' } }
42
42
  a.recursive_compact_blank!
43
- expect(a).to eq({:a => 1, :b => {:c => 1}})
43
+ expect(a).to eq({ a: 1, b: { c: 1 } })
44
44
 
45
- a = {:a => 1, :b => {:c => [], :d => ''}}
45
+ a = { a: 1, b: { c: [], d: '' } }
46
46
  a.recursive_compact_blank!
47
- expect(a).to eq({:a => 1})
47
+ expect(a).to eq({ a: 1 })
48
48
  end
49
49
  end
@@ -1,24 +1,28 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Enumerable do
4
-
5
- it 'should allow mapping elements of the collection to hashes associating method names to the returned values for each method' do
6
- expect([1,2,3].map_methods(:to_s,:to_f)).to eq [{:to_s => '1', :to_f => 1.0}, {:to_s => '2', :to_f => 2.0}, {:to_s => '3', :to_f => 3.0}]
4
+
5
+ it 'allow mapping elements to hashes with method names of the returned values for each method' do
6
+ expect([1, 2, 3].map_methods(:to_s, :to_f)).to eq(
7
+ [{ to_s: '1', to_f: 1.0 }, { to_s: '2', to_f: 2.0 }, { to_s: '3', to_f: 3.0 }]
8
+ )
7
9
  end
8
10
 
9
11
 
10
12
  context 'when detecting and returning the block value' do
11
- it { expect([1,2,3].detect_and_return { |number| number.even? && number * number }).to eq 4 }
12
- it { expect([1,3,5].detect_and_return { |number| number.even? && number * number }).to be nil }
13
+ it { expect([1, 2, 3].detect_and_return { |number| number.even? && number * number }).to eq 4 }
14
+ it { expect([1, 3, 5].detect_and_return { |number|
15
+ number.even? && number * number }).to be nil
16
+ }
13
17
  end
14
18
 
15
19
 
16
20
  it 'should allow selecting by attribute' do
17
- one = double(:name => 'one', :type => 'odd')
18
- two = double(:name => 'two', :type => 'even')
19
- thr = double(:name => 'thr', :type => 'odd')
21
+ one = double(name: 'one', type: 'odd')
22
+ two = double(name: 'two', type: 'even')
23
+ thr = double(name: 'thr', type: 'odd')
20
24
  expect([one, two, thr].select_by_attr(:type, 'odd')).to eq [one, thr]
21
25
  expect([one, two, thr].select_by_attr(:type, 'even')).to eq [two]
22
26
  end
23
-
27
+
24
28
  end
@@ -14,4 +14,3 @@ describe File do
14
14
  File.safe_name(from)
15
15
  end
16
16
  end
17
-
data/spec/hash_spec.rb CHANGED
@@ -3,14 +3,21 @@ require 'spec_helper'
3
3
  describe Hash do
4
4
 
5
5
  before do
6
- @sub_array1 = [3, BigDecimal('4'), Date.new(2000, 1, 1), DateTime.new(2000, 1, 1, 0, 0, 0), {:f => 5}]
7
- @sub_array2 = [3, BigDecimal('4'), Date.new(2000, 1, 1), DateTime.new(2000, 1, 1, 0, 0, 0), {'f' => 5}]
8
- @hash1 = {:a => 1, :b => {:c => 2}, :d => 'test', :e => @sub_array1}
9
- @hash2 = {'a' => 1, 'b' => {'c' => 2}, :d => 'test', 'e' => @sub_array2}
6
+ @sub_array1 = [3, BigDecimal('4'), Date.new(2000, 1, 1),
7
+ DateTime.new(2000, 1, 1, 0, 0, 0), { f: 5 }]
8
+ @sub_array2 = [3, BigDecimal('4'), Date.new(2000, 1, 1),
9
+ DateTime.new(2000, 1, 1, 0, 0, 0), { 'f' => 5 }]
10
+ @hash1 = { a: 1, b: { c: 2 }, d: 'test', e: @sub_array1 }
11
+ @hash2 = { 'a' => 1, 'b' => { 'c' => 2 }, :d => 'test', 'e' => @sub_array2 }
10
12
  end
11
13
 
12
14
  it "should allow converting all values to strings recursively" do
13
- expect(@hash1.stringify_values_recursively).to eq({:a => "1", :b => {:c => "2"}, :d => "test", :e => ["3", '4.0', "2000-01-01", '2000-01-01T00:00:00+00:00', {:f => "5"}]})
15
+ expect(@hash1.stringify_values_recursively).to eq(
16
+ {
17
+ a: "1", b: { c: "2" }, d: "test",
18
+ e: ["3", '4.0', "2000-01-01", '2000-01-01T00:00:00+00:00', { f: "5" }]
19
+ }
20
+ )
14
21
  end
15
22
 
16
23
  it "should allow converting all keys to symbols recursively" do
@@ -18,29 +25,33 @@ describe Hash do
18
25
  end
19
26
 
20
27
  it "should allow converting keys" do
21
- expect(@hash1.convert_keys(&:to_s)).to eq({"a" => 1, "b" => {:c => 2}, "d" => "test", "e" => @sub_array1})
28
+ expect(@hash1.convert_keys(&:to_s)).to eq(
29
+ { "a" => 1, "b" => { c: 2 }, "d" => "test", "e" => @sub_array1 }
30
+ )
22
31
  end
23
32
 
24
33
  it "should allow converting values" do
25
- expect(@hash1.convert_values(&:to_s)).to eq({:a => "1", :b => {:c => 2}, :d => "test", :e => @sub_array1})
34
+ expect(@hash1.convert_values(&:to_s)).to eq({ a: "1", b: { c: 2 }, d: "test", e: @sub_array1 })
26
35
  end
27
-
36
+
28
37
  it "should allow converting values only for specific keys" do
29
- expect(@hash1.convert_values(:d, :e, &:to_s)).to eq({:a => 1, :b => {:c => 2}, :d => "test", :e => @sub_array1})
38
+ expect(@hash1.convert_values(:d, :e, &:to_s)).to eq(
39
+ { a: 1, b: { c: 2 }, d: "test", e: @sub_array1 }
40
+ )
30
41
  end
31
42
 
32
43
  it "should allow making indifferent access recursively" do
33
44
  expect(@hash1.make_indifferent_access_recursively['b']['c']).to eq 2
34
45
  expect(@hash1.make_indifferent_access_recursively['e'][4]['f']).to eq 5
35
46
  end
36
-
47
+
37
48
  it "should allow executing blocks recursively" do
38
- hash = {:a => 1, :b => {:a => 2}, :c => {:a => 3, :b => 4, :c => {:a => 5}}}
49
+ hash = { a: 1, b: { a: 2 }, c: { a: 3, b: 4, c: { a: 5 } } }
39
50
  result = []
40
- hash.recursively do |k,v|
51
+ hash.recursively do |k, v|
41
52
  result << v unless v.is_a?(Hash)
42
- end
43
- expect(result.sort).to eq [1,2,3,4,5] # Ruby 1.8.7 doesn't order hash keys
53
+ end
54
+ expect(result.sort).to eq [1, 2, 3, 4, 5] # Ruby 1.8.7 doesn't order hash keys
44
55
  end
45
56
 
46
57
  end
@@ -49,36 +60,36 @@ end
49
60
  describe Hash do
50
61
 
51
62
  it 'should allow removing all nil values and return a new hash' do
52
- expect({:a => 1, :b => nil}.compact).to eq({:a => 1})
63
+ expect({ a: 1, b: nil }.compact).to eq({ a: 1 })
53
64
  end
54
65
 
55
66
  it 'should allow removing all nil values' do
56
- a = {:a => 1, :b => nil}
67
+ a = { a: 1, b: nil }
57
68
  a.compact!
58
- expect(a).to eq({:a => 1})
69
+ expect(a).to eq({ a: 1 })
59
70
  end
60
71
 
61
72
  it 'should allow removing all nil values and return a new hash' do
62
- expect({:a => 1, :b => ''}.compact_blank).to eq({:a => 1})
73
+ expect({ a: 1, b: '' }.compact_blank).to eq({ a: 1 })
63
74
  end
64
75
 
65
76
  it 'should allow removing all blank values' do
66
- a = {:a => 1, :b => ''}
77
+ a = { a: 1, b: '' }
67
78
  a.compact_blank!
68
- expect(a).to eq({:a => 1})
79
+ expect(a).to eq({ a: 1 })
69
80
  end
70
81
 
71
82
  it 'should allow removing all blank values recursively' do
72
- a = {:a => 1, :b => {:c => 1, :d => '', :e => []}}
83
+ a = { a: 1, b: { c: 1, d: '', e: [] } }
73
84
  a.recursive_compact_blank!
74
- expect(a).to eq({:a => 1, :b => {:c => 1}})
85
+ expect(a).to eq({ a: 1, b: { c: 1 } })
75
86
  end
76
87
 
77
88
  it 'should allow extracting subsets' do
78
- a = {:a => 1, :b => 2, :c => 3}
89
+ a = { a: 1, b: 2, c: 3 }
79
90
  b = a.extract!(:a, :c)
80
- expect(b).to eq({:a => 1, :c => 3})
81
- expect(a).to eq({:b => 2})
91
+ expect(b).to eq({ a: 1, c: 3 })
92
+ expect(a).to eq({ b: 2 })
82
93
  end
83
94
 
84
95
  end
@@ -86,22 +97,22 @@ end
86
97
 
87
98
  describe Hash, '#map_key_value' do
88
99
 
89
- subject { {'1' => '2', 3 => 4} }
100
+ subject { { '1' => '2', 3 => 4 } }
90
101
 
91
102
  it 'should map key' do
92
- expect(subject.map_key(:to_i)).to eq({1 => '2', 3 => 4})
103
+ expect(subject.map_key(:to_i)).to eq({ 1 => '2', 3 => 4 })
93
104
  end
94
105
 
95
106
  it 'should map value' do
96
- expect(subject.map_value(:to_i)).to eq({'1' => 2, 3 => 4})
107
+ expect(subject.map_value(:to_i)).to eq({ '1' => 2, 3 => 4 })
97
108
  end
98
109
 
99
110
  it 'should map key and value' do
100
- expect(subject.map_key_value(:to_i, :to_i)).to eq({1 => 2, 3 => 4})
111
+ expect(subject.map_key_value(:to_i, :to_i)).to eq({ 1 => 2, 3 => 4 })
101
112
  end
102
113
 
103
114
  it 'should map key and value if value not specified' do
104
- expect(subject.map_key_value(:to_i)).to eq({1 => 2, 3 => 4})
115
+ expect(subject.map_key_value(:to_i)).to eq({ 1 => 2, 3 => 4 })
105
116
  end
106
117
 
107
118
  end
data/spec/object_spec.rb CHANGED
@@ -26,14 +26,13 @@ describe Object do
26
26
  class ReadyError < StandardError; end
27
27
 
28
28
  class BooleanizeTest
29
-
30
29
  attr_accessor :ready
31
30
 
32
31
  def verify!
33
- fail ArgumentError, "Ready should be a boolean" unless ready.is_a?(TrueClass) || ready.is_a?(FalseClass)
32
+ fail ArgumentError, "Ready should be a boolean" unless ready.is_a?(TrueClass) ||
33
+ ready.is_a?(FalseClass)
34
34
  fail ReadyError, "Not ready" unless ready
35
35
  end
36
-
37
36
  end
38
37
 
39
38
 
@@ -42,8 +41,8 @@ describe Object do
42
41
  end
43
42
 
44
43
 
45
- it "should allow defining methods that will return boolean depending on the execution of another method" do
46
- expect { @object.booleanize(:verify!, :rescue => ReadyError) }.to_not raise_error
44
+ it "allows defining methods that return boolean depending on the execution of another method" do
45
+ expect { @object.booleanize(:verify!, rescue: ReadyError) }.to_not raise_error
47
46
  expect { @object.verify? }.to raise_error(ArgumentError, 'Ready should be a boolean')
48
47
  @object.ready = false
49
48
  expect { @object.verify? }.to_not raise_error
data/spec/string_spec.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  describe String do
2
2
 
3
3
  it "should convert to underscore replacing spaces with underscores" do
4
- expect("CamelCase UPPERCASE to be_Converted".proper_underscore).to eq "camel_case_uppercase_to_be_converted"
4
+ expect("CamelCase UPPERCASE to be_Converted".proper_underscore).to eq(
5
+ "camel_case_uppercase_to_be_converted"
6
+ )
5
7
  end
6
8
 
7
9
  it 'should separate numbers and letters' do
@@ -1,29 +1,4 @@
1
- MINIMUM_COVERAGE = 87
2
-
3
- unless ENV['COVERAGE'] == 'off'
4
- require 'simplecov'
5
- require 'simplecov-rcov'
6
- require 'coveralls'
7
- Coveralls.wear!
8
-
9
- SimpleCov.formatters = [
10
- SimpleCov::Formatter::RcovFormatter,
11
- Coveralls::SimpleCov::Formatter
12
- ]
13
- SimpleCov.start do
14
- add_filter '/vendor/'
15
- add_filter '/spec/'
16
- add_filter '/lib/error_handling/'
17
- add_group 'lib', 'lib'
18
- end
19
- SimpleCov.at_exit do
20
- SimpleCov.result.format!
21
- percent = SimpleCov.result.covered_percent
22
- unless percent >= MINIMUM_COVERAGE
23
- puts '*' * 80
24
- puts "Coverage must be above #{MINIMUM_COVERAGE}%. It is #{format('%.2f', percent)}%"
25
- puts '*' * 80
26
- Kernel.exit(1)
27
- end
28
- end
29
- end
1
+ require 'simplecov-rcov'
2
+ require 'coveralls'
3
+ require 'coverage/kit'
4
+ Coverage::Kit.setup(minimum_coverage: 87.2)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_core_extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Noack
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-06-17 00:00:00.000000000 Z
12
+ date: 2018-05-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -82,7 +82,7 @@ dependencies:
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
84
  - !ruby/object:Gem::Dependency
85
- name: simplecov
85
+ name: coverage-kit
86
86
  requirement: !ruby/object:Gem::Requirement
87
87
  requirements:
88
88
  - - ">="
@@ -137,6 +137,20 @@ dependencies:
137
137
  - - ">="
138
138
  - !ruby/object:Gem::Version
139
139
  version: '0'
140
+ - !ruby/object:Gem::Dependency
141
+ name: rubocop-rails
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
140
154
  description: These are extensions from core ruby classes.
141
155
  email: development@travellink.com.au
142
156
  executables: []
@@ -145,7 +159,9 @@ extra_rdoc_files: []
145
159
  files:
146
160
  - ".gitignore"
147
161
  - ".rspec"
162
+ - ".rubocop.yml"
148
163
  - ".travis.yml"
164
+ - CHANGELOG.md
149
165
  - Gemfile
150
166
  - LICENSE
151
167
  - README.md
@@ -166,13 +182,8 @@ files:
166
182
  - lib/ruby_core_extensions/object.rb
167
183
  - lib/ruby_core_extensions/recursive.rb
168
184
  - lib/ruby_core_extensions/recursive/array.rb
169
- - lib/ruby_core_extensions/recursive/big_decimal.rb
170
- - lib/ruby_core_extensions/recursive/date.rb
171
- - lib/ruby_core_extensions/recursive/date_time.rb
172
- - lib/ruby_core_extensions/recursive/fixnum.rb
173
185
  - lib/ruby_core_extensions/recursive/hash.rb
174
186
  - lib/ruby_core_extensions/recursive/object.rb
175
- - lib/ruby_core_extensions/recursive/time.rb
176
187
  - lib/ruby_core_extensions/string.rb
177
188
  - lib/ruby_core_extensions/version.rb
178
189
  - ruby_core_extensions.gemspec
@@ -207,7 +218,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
207
218
  version: '0'
208
219
  requirements: []
209
220
  rubyforge_project:
210
- rubygems_version: 2.4.8
221
+ rubygems_version: 2.6.14.1
211
222
  signing_key:
212
223
  specification_version: 4
213
224
  summary: Set of extensions to core ruby libraries used by TravelLink Technology.
@@ -1,5 +0,0 @@
1
- class BigDecimal
2
- def stringify_values_recursively
3
- self.to_s
4
- end
5
- end
@@ -1,8 +0,0 @@
1
- class Date
2
-
3
- def stringify_values_recursively
4
- self.to_s
5
- end
6
-
7
- end
8
-
@@ -1,8 +0,0 @@
1
- class DateTime
2
-
3
- def stringify_values_recursively
4
- self.to_s
5
- end
6
-
7
- end
8
-
@@ -1,7 +0,0 @@
1
- class Fixnum
2
-
3
- def stringify_values_recursively
4
- to_s
5
- end
6
-
7
- end
@@ -1,8 +0,0 @@
1
- class Time
2
-
3
- def stringify_values_recursively
4
- self.to_s
5
- end
6
-
7
- end
8
-