hash-tweaks 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ded40e1fbddfb884c7437c48a72fd9c4fe226a96
4
- data.tar.gz: eefa3120e45f4ed5301389a6205cdac0755445ea
2
+ SHA256:
3
+ metadata.gz: 7fd091e5ffbe1efac9107418db41c21fc3230799659de5c0c3d3ef7c4b4dad36
4
+ data.tar.gz: cbc658e257278e31ed90d4b71f375da4b66555ff013e7ec6302aa4d67058fdad
5
5
  SHA512:
6
- metadata.gz: c3da87dc74cd56b8f52d2fe99c122506b34bc97efd67bf28b9763722f5ba90615a45d229c4611bc70e1bc2810312abce84ddda19f8366b0c0a489daec1bbd5c4
7
- data.tar.gz: 30540ef9e501779d18e7fec704345d9d7f5fd15a78d00bbf78b7f932bea98f97565bbaf67fbd79d042806b388e97b64c42598388989917efcb6a4eebc7b71819
6
+ metadata.gz: f055ad691c38da558f1e52250898de33914d98559280d541c58331d5a74d68856fbf9d91fc8484b222f804006628f74af527e33d7411e82ab90c49e55dd52f8f
7
+ data.tar.gz: 165dc3ba771d625edd2db92b5d0a385e6371c69b96f091a3160bf360df5061bf3dc27bf0436f5be21e9c2d0554185f5ef1750bc0c1fd3adb3f2153cf43c9d881
@@ -1,6 +1,6 @@
1
1
  language: ruby
2
2
 
3
3
  rvm:
4
- - 2.4.0
4
+ - 2.5.1
5
5
 
6
6
  cache: bundler
data/Gemfile CHANGED
@@ -1,5 +1,9 @@
1
1
  # encoding: UTF-8
2
2
  # frozen_string_literal: true
3
3
 
4
- source 'https://rubygems.org'
4
+ source "https://rubygems.org"
5
+
5
6
  gemspec
7
+
8
+ gem "rake", "~> 12.3"
9
+ gem "test-unit", "~> 3.2"
@@ -1,26 +1,27 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hash-tweaks (1.0.0)
4
+ hash-tweaks (1.0.1)
5
5
  activesupport (>= 3.0, < 6.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (5.0.2)
10
+ activesupport (5.2.1)
11
11
  concurrent-ruby (~> 1.0, >= 1.0.2)
12
- i18n (~> 0.7)
12
+ i18n (>= 0.7, < 2)
13
13
  minitest (~> 5.1)
14
14
  tzinfo (~> 1.1)
15
15
  concurrent-ruby (1.0.5)
16
- i18n (0.8.1)
17
- minitest (5.10.1)
18
- power_assert (1.0.1)
19
- rake (12.0.0)
20
- test-unit (3.2.3)
16
+ i18n (1.1.0)
17
+ concurrent-ruby (~> 1.0)
18
+ minitest (5.11.3)
19
+ power_assert (1.1.3)
20
+ rake (12.3.1)
21
+ test-unit (3.2.8)
21
22
  power_assert
22
23
  thread_safe (0.3.6)
23
- tzinfo (1.2.2)
24
+ tzinfo (1.2.5)
24
25
  thread_safe (~> 0.1)
25
26
 
26
27
  PLATFORMS
@@ -29,8 +30,8 @@ PLATFORMS
29
30
  DEPENDENCIES
30
31
  bundler (~> 1.7)
31
32
  hash-tweaks!
32
- rake (~> 12.0)
33
+ rake (~> 12.3)
33
34
  test-unit (~> 3.2)
34
35
 
35
36
  BUNDLED WITH
36
- 1.14.3
37
+ 1.16.5
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Yaroslav Konoplov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,21 +1,25 @@
1
- ## Responsibly extends Ruby's Hash with simple and predictable utilities.
1
+ ## A gem extending Ruby's hash with useful utilities.
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/hash-tweaks.svg)](https://badge.fury.io/rb/hash-tweaks)
4
4
  [![Build Status](https://travis-ci.org/yivo/hash-tweaks.svg?branch=master)](https://travis-ci.org/yivo/hash-tweaks)
5
5
 
6
6
  ## Current features
7
- * `camelize`
8
- * `camelize!`
7
+ * `Hash#camelize`
8
+ * `Hash#camelize!`
9
+ * `Hash#deep_with_indifferent_access`
10
+ * `Array#deep_with_indifferent_access`
11
+ * `Array#deep_symbolize_keys`
12
+ * `Array#deep_stringify_keys`
9
13
 
10
14
  ## Installing gem
11
15
  Add to your Gemfile:
12
16
  ```ruby
13
- gem 'hash-tweaks', '~> 1.0'
17
+ gem "hash-tweaks", "~> 1.0"
14
18
  ```
15
19
 
16
20
  If you are using Ruby < 2.4 please add:
17
21
  ```ruby
18
- gem 'regexp-match-polyfill', '~> 1.0'
22
+ gem "regexp-match-polyfill", "~> 1.0"
19
23
  ```
20
24
 
21
25
  ## Running tests
data/Rakefile CHANGED
@@ -1,8 +1,8 @@
1
1
  # encoding: UTF-8
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'rake/testtask'
5
-
6
- Rake::TestTask.new { |t| t.libs << 'test' }
4
+ require "rake/testtask"
7
5
 
6
+ ENV["TESTOPTS"] = "--verbose"
7
+ Rake::TestTask.new { |t| t.libs << "test" }
8
8
  task default: :test
@@ -1,24 +1,18 @@
1
1
  # encoding: UTF-8
2
2
  # frozen_string_literal: true
3
3
 
4
- require File.expand_path('../lib/hash-tweaks/version', __FILE__)
5
-
6
4
  Gem::Specification.new do |s|
7
- s.name = 'hash-tweaks'
8
- s.version = HashTweaks::VERSION
9
- s.author = 'Yaroslav Konoplov'
10
- s.email = 'eahome00@gmail.com'
11
- s.summary = "Responsibly extends Ruby's Hash with simple and predictable utilities."
12
- s.description = "Responsibly extends Ruby's Hash with simple and predictable utilities."
13
- s.homepage = 'https://github.com/yivo/hash-tweaks'
14
- s.license = 'MIT'
15
-
16
- s.files = `git ls-files -z`.split("\x0")
17
- s.test_files = `git ls-files -z -- {test,spec,features}/*`.split("\x0")
18
- s.require_paths = ['lib']
19
-
20
- s.add_dependency 'activesupport', '>= 3.0', '< 6.0'
21
- s.add_development_dependency 'bundler', '~> 1.7'
22
- s.add_development_dependency 'rake', '~> 12.0'
23
- s.add_development_dependency 'test-unit', '~> 3.2'
5
+ s.name = "hash-tweaks"
6
+ s.version = "1.0.1"
7
+ s.author = "Yaroslav Konoplov"
8
+ s.email = "eahome00@gmail.com"
9
+ s.summary = "Utilities for Ruby's hash."
10
+ s.description = "A gem extending Ruby's hash with useful utilities."
11
+ s.homepage = "https://github.com/yivo/hash-tweaks"
12
+ s.license = "MIT"
13
+ s.files = `git ls-files -z`.split("\x0")
14
+ s.test_files = `git ls-files -z -- {test,spec,features}/*`.split("\x0")
15
+ s.require_paths = ["lib"]
16
+ s.add_dependency "activesupport", ">= 3.0", "< 6.0"
17
+ s.add_development_dependency "bundler", "~> 1.7"
24
18
  end
@@ -3,13 +3,17 @@
3
3
 
4
4
  require 'regexp-match-polyfill' if RUBY_VERSION < '2.4'
5
5
  require 'active_support/core_ext/string/inflections'
6
+ require 'active_support/core_ext/hash/keys'
7
+ require 'active_support/core_ext/hash/indifferent_access'
6
8
 
7
- module HashTweaks
8
- module Camelize
9
+ class Hash
10
+ unless method_defined?(:camelize)
9
11
  def camelize(first_letter = :upper)
10
12
  dup.camelize!(first_letter)
11
13
  end
14
+ end
12
15
 
16
+ unless method_defined?(:camelize!)
13
17
  def camelize!(first_letter = :upper)
14
18
  keys.each do |key|
15
19
  # Check if key is symbol or string and if it is not CONSTANT_VARIABLE.
@@ -21,6 +25,32 @@ module HashTweaks
21
25
  self
22
26
  end
23
27
  end
28
+
29
+ unless method_defined?(:deep_with_indifferent_access)
30
+ def deep_with_indifferent_access
31
+ each_with_object({}) do |(k, v), m|
32
+ m[k] = v.respond_to?(:deep_with_indifferent_access) ? v.deep_with_indifferent_access : v
33
+ end.with_indifferent_access
34
+ end
35
+ end
24
36
  end
25
37
 
26
- Hash.include HashTweaks::Camelize
38
+ class Array
39
+ unless method_defined?(:deep_with_indifferent_access)
40
+ def deep_with_indifferent_access
41
+ map { |item| item.respond_to?(:deep_with_indifferent_access) ? item.deep_with_indifferent_access : item }
42
+ end
43
+ end
44
+
45
+ unless method_defined?(:deep_symbolize_keys)
46
+ def deep_symbolize_keys
47
+ map { |item| item.respond_to?(:deep_symbolize_keys) ? item.deep_symbolize_keys : item }
48
+ end
49
+ end
50
+
51
+ unless method_defined?(:deep_stringify_keys)
52
+ def deep_stringify_keys
53
+ map { |item| item.respond_to?(:deep_stringify_keys) ? item.deep_stringify_keys : item }
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+ require "json"
5
+ Bundler.require
6
+
7
+ Test::Unit::TestCase.test_order = :random
@@ -1,13 +1,48 @@
1
1
  # encoding: UTF-8
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'bundler'
5
- Bundler.require :default, :development, :test
4
+ require_relative "helper"
6
5
 
7
6
  class HashTweaksTest < Test::Unit::TestCase
8
- def test_camelize
9
- assert_equal({fooBarBaz: 'qux'}, {foo_bar_baz: 'qux'}.camelize(:lower))
10
- assert_equal({FooBarBaz: 'qux'}, {foo_bar_baz: 'qux'}.camelize(:upper))
11
- assert_equal({FOO_BAZ_BAZ: 'qux'}, {FOO_BAZ_BAZ: 'qux'}.camelize(:upper))
7
+ test "Hash#camelize" do
8
+ assert_equal({ fooBarBaz: "qux" }, { foo_bar_baz: "qux" }.camelize(:lower))
9
+ assert_equal({ FooBarBaz: "qux" }, { foo_bar_baz: "qux" }.camelize(:upper))
10
+ assert_equal({ FOO_BAZ_BAZ: "qux" }, { FOO_BAZ_BAZ: "qux" }.camelize(:upper))
11
+ end
12
+
13
+ test "Hash#camelize!" do
14
+ hash = { foo_bar_baz: "qux" }
15
+ hash.camelize!
16
+ assert_equal [:FooBarBaz], hash.keys
17
+ end
18
+
19
+ test "Hash#deep_with_indifferent_access" do
20
+ hash_1 = { a: { "b" => { c: "d" } } }
21
+ hash_2 = hash_1.deep_with_indifferent_access
22
+ assert_equal JSON.generate(hash_1[:a]), JSON.generate(hash_2["a"])
23
+ assert_equal JSON.generate(hash_1[:a]), JSON.generate(hash_2[:a])
24
+ assert_equal JSON.generate(hash_1[:a]["b"]), JSON.generate(hash_2["a"]["b"])
25
+ assert_equal JSON.generate(hash_1[:a]["b"]), JSON.generate(hash_2["a"][:b])
26
+ assert_equal JSON.generate(hash_1[:a]["b"][:c]), JSON.generate(hash_2["a"][:b][:c])
27
+ assert_equal JSON.generate(hash_1[:a]["b"][:c]), JSON.generate(hash_2["a"][:b]["c"])
28
+ end
29
+
30
+ test "Array#deep_with_indifferent_access" do
31
+ array = [:foo, "baz", { a: { "b" => :c } }].deep_with_indifferent_access
32
+ assert_equal :foo, array[0]
33
+ assert_equal "baz", array[1]
34
+ assert_equal JSON.generate(a: { b: :c }), JSON.generate(array[2])
35
+ assert_equal JSON.generate(b: :c), JSON.generate(array[2][:a])
36
+ assert_equal JSON.generate(b: :c), JSON.generate(array[2]["a"])
37
+ assert_equal :c, array[2][:a]["b"]
38
+ assert_equal :c, array[2]["a"][:b]
39
+ end
40
+
41
+ test "Array#deep_symbolize_keys" do
42
+ assert_equal([{ foo: "bar" }, "baz", :baz], [{ "foo" => "bar" }, "baz", :baz].deep_symbolize_keys)
43
+ end
44
+
45
+ test "Array#deep_stringify_keys" do
46
+ assert_equal([{ "foo" => :bar }, "baz", :baz], [{ foo: :bar }, "baz", :baz].deep_stringify_keys)
12
47
  end
13
48
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash-tweaks
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Konoplov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-28 00:00:00.000000000 Z
11
+ date: 2018-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -44,35 +44,7 @@ dependencies:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
46
  version: '1.7'
47
- - !ruby/object:Gem::Dependency
48
- name: rake
49
- requirement: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - "~>"
52
- - !ruby/object:Gem::Version
53
- version: '12.0'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - "~>"
59
- - !ruby/object:Gem::Version
60
- version: '12.0'
61
- - !ruby/object:Gem::Dependency
62
- name: test-unit
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: '3.2'
68
- type: :development
69
- prerelease: false
70
- version_requirements: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - "~>"
73
- - !ruby/object:Gem::Version
74
- version: '3.2'
75
- description: Responsibly extends Ruby's Hash with simple and predictable utilities.
47
+ description: A gem extending Ruby's hash with useful utilities.
76
48
  email: eahome00@gmail.com
77
49
  executables: []
78
50
  extensions: []
@@ -82,11 +54,12 @@ files:
82
54
  - ".travis.yml"
83
55
  - Gemfile
84
56
  - Gemfile.lock
57
+ - LICENSE
85
58
  - README.md
86
59
  - Rakefile
87
60
  - hash-tweaks.gemspec
88
61
  - lib/hash-tweaks.rb
89
- - lib/hash-tweaks/version.rb
62
+ - test/helper.rb
90
63
  - test/test-hash-tweaks.rb
91
64
  homepage: https://github.com/yivo/hash-tweaks
92
65
  licenses:
@@ -108,9 +81,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
81
  version: '0'
109
82
  requirements: []
110
83
  rubyforge_project:
111
- rubygems_version: 2.6.8
84
+ rubygems_version: 2.7.6
112
85
  signing_key:
113
86
  specification_version: 4
114
- summary: Responsibly extends Ruby's Hash with simple and predictable utilities.
87
+ summary: Utilities for Ruby's hash.
115
88
  test_files:
89
+ - test/helper.rb
116
90
  - test/test-hash-tweaks.rb
@@ -1,6 +0,0 @@
1
- # encoding: UTF-8
2
- # frozen_string_literal: true
3
-
4
- module HashTweaks
5
- VERSION = '1.0.0'
6
- end