Skip to content

Commit

Permalink
Merge pull request #4 from sealink/stringify_values_recursively_reduc…
Browse files Browse the repository at this point in the history
…ed_monkey_patching

Stringify values recursively reduced monkey patching
  • Loading branch information
Michael Noack committed May 18, 2018
2 parents 196525b + 52ee04b commit 81d8d0e
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 45 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ This changelog adheres to [Keep a CHANGELOG](http://keepachangelog.com/).
## Unreleased

### Added

- [TT-4020] Implemented Rubocop

### Removed
- Remove no longer supported Array methods #to_param/#show_name

### Changed
- stringify_values_recursively now just works on to_s for all objects

## 0.0.1

### Added
Expand Down
8 changes: 0 additions & 8 deletions lib/ruby_core_extensions/array.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
class Array
def to_param
self.collect { |element| element.respond_to?(:to_param) ? element.to_param : element }
end

def show_name
first.titleize
end

# Key should be unique, or latest element with that key will override previous ones.
def hash_by(key = nil, method = nil, &block)
self.inject({}) do |h, element|
Expand Down
5 changes: 0 additions & 5 deletions lib/ruby_core_extensions/recursive.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
require 'ruby_core_extensions/recursive/array'
require 'ruby_core_extensions/recursive/big_decimal'
require 'ruby_core_extensions/recursive/date'
require 'ruby_core_extensions/recursive/date_time'
require 'ruby_core_extensions/recursive/fixnum'
require 'ruby_core_extensions/recursive/hash'
require 'ruby_core_extensions/recursive/object'
require 'ruby_core_extensions/recursive/time'
5 changes: 0 additions & 5 deletions lib/ruby_core_extensions/recursive/big_decimal.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/ruby_core_extensions/recursive/date.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/ruby_core_extensions/recursive/date_time.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/ruby_core_extensions/recursive/fixnum.rb

This file was deleted.

6 changes: 5 additions & 1 deletion lib/ruby_core_extensions/recursive/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ def return_self
self
end

def return_to_s
to_s
end

alias_method :convert_values_recursively, :convert
alias_method :convert_recursively, :convert

alias_method :convert_keys_recursively, :return_self
alias_method :symbolize_keys_recursively, :return_self
alias_method :stringify_values_recursively, :return_self
alias_method :stringify_values_recursively, :return_to_s
alias_method :make_indifferent_access_recursively, :return_self
end
5 changes: 0 additions & 5 deletions lib/ruby_core_extensions/recursive/time.rb

This file was deleted.

25 changes: 20 additions & 5 deletions spec/array_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
require 'spec_helper'

describe Array do

it "should allow converting all values to strings recursively" do
@now = Time.now
expect([1, 2, @now, [3, 4]].stringify_values_recursively).to eq(
['1', '2', @now.to_s, ['3', '4']]
)
to_s_class = Class.new do
def to_s
'Myself'
end
end
stringify_values_recursively_class = Class.new do
def stringify_values_recursively
'Special'
end
end
now = Time.now
array = [1, 2, now, [3, 4], to_s_class.new, stringify_values_recursively_class.new]
output = ['1', '2', now.to_s, %w[3 4], 'Myself', 'Special']
expect(array.stringify_values_recursively).to eq output
end

it "should allow removing all blank values" do
Expand Down Expand Up @@ -37,6 +46,12 @@
expect([1, 2, 3].hash_by { |k| k * 2 }).to eq({ 2 => 1, 4 => 2, 6 => 3 })
end

it "#hash_by_id" do
one = double(id: 1, name: 'One')
two = double(id: 2, name: 'Two')
expect([one, two].hash_by_id).to eq(1 => one, 2 => two)
end

it "should allow executing blocks recursively" do
array = [1, [2, 3], [4, [5, 6], 7, [8]], 9, [[10]]]
result = []
Expand Down

0 comments on commit 81d8d0e

Please sign in to comment.