openhash 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/open_hash.rb +20 -8
- data/lib/openhash.rb +1 -0
- data/openhash.gemspec +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0640ffa1020dada980373dcacc2af5a497bdc0f5edfe997539b97ea63458a1e
|
4
|
+
data.tar.gz: f7712ce1ce7b1f3b2e40cd05de0f9c23779870d38c86e4d4c0869509f9c67f38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a231dcfa2b4fbe72ee14fc77360a51ef1c6374a57aa506d372225575f4630e1adeb37f3c4a4cd06948f340b24b4380b00d864e546956616c2aa3d100f2759c1b
|
7
|
+
data.tar.gz: 505015f30608bdc215c9d253b7e91da1aa642120a6ae1a8a702cb22088ccf7f7dc4445da848810cce0c3a689ad1fd94838b79a4727682b3d014fe2619157a8a0
|
data/Gemfile.lock
CHANGED
data/lib/open_hash.rb
CHANGED
@@ -5,15 +5,15 @@ require 'ostruct'
|
|
5
5
|
# OpenHash lets Hash called and assigned by the key in chainable way.
|
6
6
|
# Example:
|
7
7
|
#
|
8
|
-
# person = OpenHash.new(name: "John
|
9
|
-
# person.name #=> "John
|
8
|
+
# person = OpenHash.new(name: "John", hometown: { city: "London" })
|
9
|
+
# person.name #=> "John"
|
10
10
|
# person.hometown.city #=> "London"
|
11
11
|
#
|
12
12
|
# person = OpenHash.new
|
13
|
-
# person.name = "
|
14
|
-
# person.hometown.city = "
|
15
|
-
# person.parents.father.name = "Heron
|
16
|
-
# person #=> { name: "
|
13
|
+
# person.name = "Lion"
|
14
|
+
# person.hometown.city = "Paris"
|
15
|
+
# person.parents.father.name = "Heron"
|
16
|
+
# person #=> { name: "Lion", hometown: { city: "Paris" }, parents: { father: { name: "Heron" } } }
|
17
17
|
#
|
18
18
|
class OpenHash < OpenStruct
|
19
19
|
def initialize(hash = {})
|
@@ -32,11 +32,20 @@ class OpenHash < OpenStruct
|
|
32
32
|
|
33
33
|
# BlackHole Class
|
34
34
|
class BlackHole < BasicObject
|
35
|
+
DELEGATE_REGEX = /(.+\?)|(to_.+)|(={2,3})\z/.freeze
|
36
|
+
@nil_methods = ::NilClass.instance_methods.grep(DELEGATE_REGEX)
|
37
|
+
|
35
38
|
def initialize(ohash, *args)
|
36
39
|
@ohash = ohash
|
37
40
|
@chain_methods = args
|
38
41
|
end
|
39
42
|
|
43
|
+
@nil_methods.each do |method_name|
|
44
|
+
define_method method_name do |*args|
|
45
|
+
nil.send(method_name, *args)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
40
49
|
def inspect
|
41
50
|
"nil"
|
42
51
|
end
|
@@ -53,8 +62,11 @@ class OpenHash < OpenStruct
|
|
53
62
|
last_ohash = @ohash
|
54
63
|
|
55
64
|
@chain_methods.each do |method_name|
|
56
|
-
last_ohash
|
57
|
-
|
65
|
+
last_ohash = if last_ohash.respond_to?(method_name)
|
66
|
+
last_ohash[method_name]
|
67
|
+
else
|
68
|
+
last_ohash[method_name] = ::OpenHash.new
|
69
|
+
end
|
58
70
|
end
|
59
71
|
|
60
72
|
last_ohash[$1] = args[0]
|
data/lib/openhash.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'open_hash'
|
data/openhash.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openhash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- cenxky
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-03-
|
11
|
+
date: 2019-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- bin/console
|
71
71
|
- bin/setup
|
72
72
|
- lib/open_hash.rb
|
73
|
+
- lib/openhash.rb
|
73
74
|
- openhash.gemspec
|
74
75
|
homepage: https://github.com/cenxky/openhash
|
75
76
|
licenses:
|