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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db2d7639d1187e0cdec21a9e93861ad46085b6065cb8cf432da94d781f6a0a34
4
- data.tar.gz: f10230eabbade54919e7050089929d0363231f4e18d6895b69dd7d2765ca4cc7
3
+ metadata.gz: d0640ffa1020dada980373dcacc2af5a497bdc0f5edfe997539b97ea63458a1e
4
+ data.tar.gz: f7712ce1ce7b1f3b2e40cd05de0f9c23779870d38c86e4d4c0869509f9c67f38
5
5
  SHA512:
6
- metadata.gz: 9a77e10c1d30b234402dbe9dae9ea307bb6efd3944f5b63b8602736b5bf7e100e0801c5017478975f64e06ace4f5d3c98e6f02e8bae14ab18233124773234f57
7
- data.tar.gz: cc545c55ec4ab05c272ba152156d32f9bcd4bfbee946d939c819ce90761308f03168eb33b1667e7bd5fda3f941694e38867aad13043d14c1eb31bde4dab619e3
6
+ metadata.gz: a231dcfa2b4fbe72ee14fc77360a51ef1c6374a57aa506d372225575f4630e1adeb37f3c4a4cd06948f340b24b4380b00d864e546956616c2aa3d100f2759c1b
7
+ data.tar.gz: 505015f30608bdc215c9d253b7e91da1aa642120a6ae1a8a702cb22088ccf7f7dc4445da848810cce0c3a689ad1fd94838b79a4727682b3d014fe2619157a8a0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- openhash (0.0.2)
4
+ openhash (0.0.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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 Smith", hometown: { city: "London" })
9
- # person.name #=> "John Smith"
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 = "Piter Lee"
14
- # person.hometown.city = "Guangzhou"
15
- # person.parents.father.name = "Heron Lee"
16
- # person #=> { name: "Piter Lee", hometown: { city: "Guangzhou" }, parents: { father: { name: "Heron Lee" } } }
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[method_name] = ::OpenHash.new
57
- last_ohash = last_ohash[method_name]
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
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "openhash"
8
- spec.version = '0.0.3'
8
+ spec.version = '0.0.4'
9
9
  spec.authors = ["cenxky"]
10
10
  spec.email = ["cenxky@gmail.com"]
11
11
 
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.3
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-24 00:00:00.000000000 Z
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: