openhash 0.0.1 → 0.0.6

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: ce62159b96a8bf1a4e9858346d62db25bfa36735d07338b31e6f7033397386dd
4
- data.tar.gz: '0748023153497b09f2923a17e48b4d1ed7697d14655119ac45ef89eeeeb0524d'
3
+ metadata.gz: 67d8084ef667ae484317c9be75e958b00aeed1cc2e8b154fe94c4838368ef5a9
4
+ data.tar.gz: 8309b6ca1c44e37df79ab49e1604795c8af519541e0890aab3cbf44082d6bf32
5
5
  SHA512:
6
- metadata.gz: 3c0751f116ec4d76be10389e35e251fc5a2824f90aa055b97c0b0606900af31d43c4b7e999b26b348dc4bb9b7b03e985c22583ea04f8e286aeb0160c57612405
7
- data.tar.gz: e4c3f5fc2b3380b6a33214bfa4a303221dcfff606489e1258442eea9eb34479eef2829a0c202cb0d6a57d0240307982b456392d9a830be276a900c4b50b26972
6
+ metadata.gz: 9516725c185c703941a947e6a57cb26bee9179d09c4f141ad6fd15d0c0d70d4ff67570978f3826ccb518ca25a70039012afe3037af137fd58b0f263d0c5e8a75
7
+ data.tar.gz: 6b5a97958ad18f0494b7f33f21263903781f3c352388113578b25e6476d18925e868984274e2140200d075ec36ff9fa60d25ca8536c864c8c6482b7d6fa01bca
data/.gitignore CHANGED
@@ -9,3 +9,6 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+
13
+ # gem files
14
+ *.gem
@@ -1,13 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- openhash (0.0.1)
4
+ openhash (0.0.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  diff-lcs (1.3)
10
- rake (10.5.0)
10
+ rake (12.3.3)
11
11
  rspec (3.8.0)
12
12
  rspec-core (~> 3.8.0)
13
13
  rspec-expectations (~> 3.8.0)
@@ -28,8 +28,8 @@ PLATFORMS
28
28
  DEPENDENCIES
29
29
  bundler (~> 2.0)
30
30
  openhash!
31
- rake (~> 10.0)
31
+ rake (~> 12.3)
32
32
  rspec (~> 3.0)
33
33
 
34
34
  BUNDLED WITH
35
- 2.0.1
35
+ 2.0.2
data/README.md CHANGED
@@ -11,15 +11,50 @@
11
11
 
12
12
  ### Usage ###
13
13
  ```ruby
14
- # person = OpenHash.new(name: "John Smith", hometown: { city: "London" })
15
- # person.name #=> "John Smith"
16
- # person.hometown.country #=> "UK"
17
- #
18
- # person = OpenHash.new
19
- # person.name = "Piter Lee"
20
- # person.hometown.city = "Guangzhou"
21
- # person.parents.father.name = "Heron Lee"
22
- # person #=> { name: "Piter Lee", hometown: { city: "Guangzhou" }, parents: { father: { name: "Heron Lee" } } }
14
+ person = OpenHash.new(name: "John", hometown: { city: "London" }, pets: [{ name: "Mia", animal: "Cat" }])
15
+ person.name #=> "John"
16
+ person.hometown.city #=> "London"
17
+ person.pets[0].name #=> "Mia"
18
+
19
+ person = OpenHash.new
20
+ person.name = "Lion"
21
+ person.hometown.city = "Paris"
22
+ person.parents.father.name = "Heron"
23
+ person #=> { name: "Lion", hometown: { city: "Paris" }, parents: { father: { name: "Heron" } } }
23
24
  ```
25
+
26
+ ### Known Issue ###
27
+
28
+ Since Ruby doesn't support to override logic oprators,
29
+ so please pay attention to use oprators below when you are using `OpenHash`:
30
+
31
+ - `&&`, `and`
32
+ - `||`, `or`
33
+ - `&.`
34
+
35
+ The most recommend way once you have to do some logic checking:
36
+
37
+ ```ruby
38
+ person = OpenHash.new
39
+
40
+ # Wrong
41
+ city = person.city || "London"
42
+
43
+ # Right
44
+ city = person.city.nil? ? "London" : person.city
45
+
46
+ # Wrong
47
+ state = person.is_succeed && "Success"
48
+
49
+ # Right
50
+ state = "Success" if !person.is_succeed.nil?
51
+
52
+ # Wrong
53
+ person.city&.upcase
54
+
55
+ # Right
56
+ !person.city.nil? && person.city.upcase
57
+ ```
58
+
24
59
  ### License ###
25
60
  Released under the [MIT](http://opensource.org/licenses/MIT) license. See LICENSE file for details.
@@ -5,20 +5,28 @@ 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"
10
- # person.hometown.country #=> "UK"
8
+ # person = OpenHash.new(name: "John", hometown: { city: "London" }, pets: [{ name: "Mia", animal: "Cat" }])
9
+ # person.name #=> "John"
10
+ # person.hometown.city #=> "London"
11
+ # person.pets[0].name #=> "Mia"
11
12
  #
12
13
  # 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" } } }
14
+ # person.name = "Lion"
15
+ # person.hometown.city = "Paris"
16
+ # person.parents.father.name = "Heron"
17
+ # person #=> { name: "Lion", hometown: { city: "Paris" }, parents: { father: { name: "Heron" } } }
17
18
  #
18
19
  class OpenHash < OpenStruct
19
20
  def initialize(hash = {})
20
21
  hash = hash.each_with_object({}) do |(k, v), r|
21
- r[k] = v.is_a?(Hash) ? OpenHash.new(v) : v
22
+ r[k] = case v
23
+ when Hash, OpenStruct
24
+ OpenHash.new(v)
25
+ when Array
26
+ v.map { |x| OpenHash.new(x) }
27
+ else
28
+ v
29
+ end
22
30
  end
23
31
 
24
32
  super
@@ -32,13 +40,17 @@ class OpenHash < OpenStruct
32
40
 
33
41
  # BlackHole Class
34
42
  class BlackHole < BasicObject
43
+ NIL_METHODS = ::NilClass.instance_methods(false) | %i[== != ! equal? is_a? kind_of? instance_of?]
44
+
35
45
  def initialize(ohash, *args)
36
46
  @ohash = ohash
37
47
  @chain_methods = args
38
48
  end
39
49
 
40
- def inspect
41
- "nil"
50
+ NIL_METHODS.each do |method_name|
51
+ define_method method_name do |*args|
52
+ nil.send(method_name, *args)
53
+ end
42
54
  end
43
55
 
44
56
  # Append a method name to chain methods
@@ -53,11 +65,16 @@ class OpenHash < OpenStruct
53
65
  last_ohash = @ohash
54
66
 
55
67
  @chain_methods.each do |method_name|
56
- last_ohash[method_name] = ::OpenHash.new
57
- last_ohash = last_ohash[method_name]
68
+ last_ohash = if last_ohash.respond_to?(method_name)
69
+ last_ohash[method_name]
70
+ else
71
+ last_ohash[method_name] = ::OpenHash.new
72
+ end
58
73
  end
59
74
 
60
- last_ohash[name] = args[0]
75
+ last_ohash[$1] = args[0]
76
+ when /\A[^a-z_A-Z]+/
77
+ nil.send(name, *args)
61
78
  else
62
79
  self << name
63
80
  end
@@ -0,0 +1 @@
1
+ require_relative 'open_hash'
@@ -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.1'
8
+ spec.version = '0.0.6'
9
9
  spec.authors = ["cenxky"]
10
10
  spec.email = ["cenxky@gmail.com"]
11
11
 
@@ -25,6 +25,6 @@ Gem::Specification.new do |spec|
25
25
  spec.require_paths = ["lib"]
26
26
 
27
27
  spec.add_development_dependency "bundler", "~> 2.0"
28
- spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rake", "~> 12.3"
29
29
  spec.add_development_dependency "rspec", "~> 3.0"
30
30
  end
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.1
4
+ version: 0.0.6
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: 2020-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '12.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '12.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -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:
@@ -90,8 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
91
  - !ruby/object:Gem::Version
91
92
  version: '0'
92
93
  requirements: []
93
- rubyforge_project:
94
- rubygems_version: 2.7.6
94
+ rubygems_version: 3.0.3
95
95
  signing_key:
96
96
  specification_version: 4
97
97
  summary: OpenHash lets Hash called and assigned by the key in chainable way.