hash_ext 0.3.0 → 0.6.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
2
  SHA256:
3
- metadata.gz: bb3a7d52074eaa16664df7d5bdcea9e55707c03f1b588b11ec343a216486a0fb
4
- data.tar.gz: cd5424081c17dbf054d2c419535f28fd9a01d1ed29193a48ba4a423a797c58bf
3
+ metadata.gz: 99fff501a3fca91d49b7506705477c1fefcbf3c8b0ee72f7262da7857a41d00e
4
+ data.tar.gz: 495e26e293b9534a261026537848bd674873c09a77d766d19941a9d7068405c1
5
5
  SHA512:
6
- metadata.gz: 2026727664be4c2092e83630acc50397cabefea5f3f3a85ebc89453d83befffcd16edb704ca627128d9f17b59e3f920d23cbc02e904df229e123e1ac00eacf5d
7
- data.tar.gz: 7a86313de223db2f7eeca2ed0d4776d9233844184a678e10386e4335d329a1452d62d619d98820a8726982f28d9e28f77243f91f2fc0b4c31482ea7c18c82ac6
6
+ metadata.gz: 795a80a5c4bfd86e24095fd4e5231a711dcbf25a5a87cd3fc68ffc686a7aa1f7392d7b9ca5c985a8a33dd943ad02b9e082156bdba88be81b9c74855e3bba6482
7
+ data.tar.gz: 71ebaa63fbfb78e8d58cf280c5999ef84a82cc30d06753a4fc84e961cade3553f8229514777bcb0c1370dfb5c02690b3b3de758fe43c63217fbcb0d826f20169
@@ -5,11 +5,12 @@ rvm:
5
5
  - 2.0
6
6
  - 2.1
7
7
  - 2.2
8
- - 2.3.0
9
- - 2.4.0
10
- - 2.5.0
11
- - jruby-1.7.25
12
- - jruby-9.1.7.0
8
+ - 2.3
9
+ - 2.4
10
+ - 2.5
11
+ - 2.6
12
+ - 2.7
13
+ - jruby-9.2.9.0
13
14
  - ruby-head
14
15
  - jruby-head
15
16
 
@@ -17,7 +18,4 @@ matrix:
17
18
  fast_finish: true
18
19
  allow_failures:
19
20
  - rvm: ruby-head
20
- - rvm: jruby-head
21
-
22
- before_install:
23
- - gem install bundler
21
+ - rvm: jruby-head
data/README.md CHANGED
@@ -4,7 +4,6 @@
4
4
  [![Build Status](https://travis-ci.org/gabynaiman/hash_ext.svg?branch=master)](https://travis-ci.org/gabynaiman/hash_ext)
5
5
  [![Coverage Status](https://coveralls.io/repos/gabynaiman/hash_ext/badge.svg?branch=master)](https://coveralls.io/r/gabynaiman/hash_ext?branch=master)
6
6
  [![Code Climate](https://codeclimate.com/github/gabynaiman/hash_ext.svg)](https://codeclimate.com/github/gabynaiman/hash_ext)
7
- [![Dependency Status](https://gemnasium.com/gabynaiman/hash_ext.svg)](https://gemnasium.com/gabynaiman/hash_ext)
8
7
 
9
8
  Hash extensions without monkey patching
10
9
 
@@ -92,6 +91,14 @@ hash[:a] = 3
92
91
  hash.keys # => [:a, :b, :c]
93
92
  ```
94
93
 
94
+ ### Nested
95
+ ```ruby
96
+ hash = Hash::Nested.new
97
+
98
+ hash[:level_1][:level_2][:level_3] = 'value'
99
+
100
+ hash # => {level_1: {level_2: {level_3: 'value'}}}
101
+ ```
95
102
 
96
103
  ## Contributing
97
104
 
@@ -19,9 +19,8 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ['lib']
21
21
 
22
- spec.add_development_dependency 'bundler', '~> 1.12'
23
22
  spec.add_development_dependency 'rake', '~> 11.0'
24
- spec.add_development_dependency 'minitest', '~> 5.0'
23
+ spec.add_development_dependency 'minitest', '~> 5.0', '< 5.11'
25
24
  spec.add_development_dependency 'minitest-colorin', '~> 0.1'
26
25
  spec.add_development_dependency 'minitest-line', '~> 0.6'
27
26
  spec.add_development_dependency 'simplecov', '~> 0.14'
@@ -2,8 +2,10 @@ require 'forwardable'
2
2
 
3
3
  require_relative 'hash_ext/version'
4
4
 
5
+ require_relative 'hash_ext/deep_freezable'
5
6
  require_relative 'hash_ext/normalized'
6
7
  require_relative 'hash_ext/builder'
7
8
  require_relative 'hash_ext/indifferent'
8
9
  require_relative 'hash_ext/accessible'
9
- require_relative 'hash_ext/sorted'
10
+ require_relative 'hash_ext/sorted'
11
+ require_relative 'hash_ext/nested'
@@ -1,6 +1,6 @@
1
1
  class Hash
2
2
  class Builder
3
-
3
+
4
4
  def initialize
5
5
  @hash = {}
6
6
  @scopes = []
@@ -0,0 +1,25 @@
1
+ class Hash
2
+ module DeepFreezable
3
+
4
+ def deep_freeze
5
+ each_value { |v| deep_freeze_value v }
6
+ freeze
7
+ end
8
+
9
+ def deep_freeze_value(value)
10
+ if value.respond_to? :deep_freeze
11
+ value.deep_freeze
12
+ else
13
+ if value.is_a? Hash
14
+ value.each_value { |v| deep_freeze_value v }
15
+ elsif value.respond_to? :each
16
+ value.each { |v| deep_freeze_value v }
17
+ end
18
+ value.freeze
19
+ end
20
+ end
21
+
22
+ private :deep_freeze_value
23
+
24
+ end
25
+ end
@@ -8,7 +8,7 @@ class Hash
8
8
  private
9
9
 
10
10
  def normalize_key(key)
11
- key.to_sym
11
+ key.kind_of?(String) ? key.to_sym : key
12
12
  end
13
13
 
14
14
  end
@@ -0,0 +1,15 @@
1
+ class Hash
2
+ class Nested < Hash
3
+
4
+ include DeepFreezable
5
+
6
+ def [](key)
7
+ if key? key
8
+ super(key)
9
+ else
10
+ self[key] = Nested.new
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -1,6 +1,8 @@
1
1
  class Hash
2
2
  class Normalized < Hash
3
3
 
4
+ include DeepFreezable
5
+
4
6
  def self.subclass(&block)
5
7
  Class.new(self) do
6
8
  define_method :initialize do |hash={}|
@@ -51,6 +53,13 @@ class Hash
51
53
  super normalize_key(key), *args, &block
52
54
  end
53
55
 
56
+ def dig(*keys)
57
+ normalized_keys = keys.map { |k| normalize_key(k) }
58
+ normalized_keys.inject(self) do |target, key|
59
+ target ? target[key] : nil
60
+ end
61
+ end
62
+
54
63
  def to_h
55
64
  each_with_object({}) do |(key, value), hash|
56
65
  hash[key] = value_to_h value
@@ -64,8 +73,10 @@ class Hash
64
73
  end
65
74
 
66
75
  def normalize_value(value)
67
- if value.kind_of? Hash
68
- self.class.new value
76
+ if value.is_a? self.class
77
+ value
78
+ elsif value.kind_of? Hash
79
+ self.class.new value, &@normalization_block
69
80
  elsif value.kind_of? Array
70
81
  value.map { |v| normalize_value v }
71
82
  else
@@ -3,15 +3,16 @@ class Hash
3
3
 
4
4
  extend Forwardable
5
5
  include Enumerable
6
+ include DeepFreezable
6
7
 
7
8
  def_delegators :@hash, :[], :[]=, :value?, :fetch, :values_at,
8
9
  :key?, :include?, :empty?,
9
- :delete, :delete_if, :clear,
10
+ :delete, :delete_if, :clear,
10
11
  :merge, :merge!, :update, :store, :replace,
11
12
  :count, :size, :length,
12
13
  :==, :eql, :hash
13
-
14
- def_delegators :to_h, :to_a, :to_s, :inspect,
14
+
15
+ def_delegators :to_h, :to_a, :to_s, :inspect,
15
16
  :select, :reject, :flatten,
16
17
  :as_json, :to_json
17
18
 
@@ -55,7 +56,7 @@ class Hash
55
56
  attr_reader :direction, :sort_criteria
56
57
 
57
58
  class << self
58
-
59
+
59
60
  def ascending(hash={}, &sort_criteria)
60
61
  new hash, :asc, &sort_criteria
61
62
  end
@@ -1,3 +1,3 @@
1
1
  module HashExt
2
- VERSION = '0.3.0'
2
+ VERSION = '0.6.1'
3
3
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-04 00:00:00.000000000 Z
11
+ date: 2020-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.12'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.12'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rake
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -45,6 +31,9 @@ dependencies:
45
31
  - - "~>"
46
32
  - !ruby/object:Gem::Version
47
33
  version: '5.0'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '5.11'
48
37
  type: :development
49
38
  prerelease: false
50
39
  version_requirements: !ruby/object:Gem::Requirement
@@ -52,6 +41,9 @@ dependencies:
52
41
  - - "~>"
53
42
  - !ruby/object:Gem::Version
54
43
  version: '5.0'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '5.11'
55
47
  - !ruby/object:Gem::Dependency
56
48
  name: minitest-colorin
57
49
  requirement: !ruby/object:Gem::Requirement
@@ -142,7 +134,9 @@ files:
142
134
  - lib/hash_ext.rb
143
135
  - lib/hash_ext/accessible.rb
144
136
  - lib/hash_ext/builder.rb
137
+ - lib/hash_ext/deep_freezable.rb
145
138
  - lib/hash_ext/indifferent.rb
139
+ - lib/hash_ext/nested.rb
146
140
  - lib/hash_ext/normalized.rb
147
141
  - lib/hash_ext/sorted.rb
148
142
  - lib/hash_ext/version.rb
@@ -165,8 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
159
  - !ruby/object:Gem::Version
166
160
  version: '0'
167
161
  requirements: []
168
- rubyforge_project:
169
- rubygems_version: 2.7.3
162
+ rubygems_version: 3.0.6
170
163
  signing_key:
171
164
  specification_version: 4
172
165
  summary: Hash extensions without monkey patching