neohash 0.3.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7ebf4d1873a29aacc1400bff53f99a4347a48b430df16368d5b0a3661caca84d
4
+ data.tar.gz: 428a6969244858c844789f2842bd42a9353a264aabe927a92589176b5ea8b425
5
+ SHA512:
6
+ metadata.gz: 8703d7ff38bd455f3e4e6bb056caf7499430c10adf2f697b499ea3201d06bcb2222d87c0bb5ef03bd8ff1f9c86020d2bfd1c23e687132332253d1a3dbcdd3e0f
7
+ data.tar.gz: 533f3261479ab0e001f7831b52931eda5b5920568961be08bfff4cd9d58d63d9a93615290ee0a544bb1606539d3ea21009a6af5a3c874b3a7030ee36fd3d3544
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /rdoc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /Gemfile.lock
11
+
12
+ ## Environment normalization:
13
+ /.bundle/
14
+ /vendor/bundle
15
+ /lib/bundler/man/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in neohash.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'simplecov', require: false
8
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Toshimitsu Takahashi
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.
@@ -0,0 +1,46 @@
1
+ # NeoHash
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/neohash.svg)](https://badge.fury.io/rb/neohash)
4
+ [![Build Status](https://travis-ci.org/tilfin/neohash.svg?branch=master)](https://travis-ci.org/tilfin/neohash)
5
+ [![Code Climate](https://codeclimate.com/github/tilfin/neohash/badges/gpa.svg)](https://codeclimate.com/github/tilfin/neohash)
6
+ [![Test Coverage](https://codeclimate.com/github/tilfin/neohash/badges/coverage.svg)](https://codeclimate.com/github/tilfin/neohash/coverage)
7
+
8
+ Accessible Hash and attaching it to an Object
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'neohash'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install neohash
25
+
26
+ ## Usage
27
+
28
+ ```ruby
29
+ class DynamicObject
30
+ include NeoHash::Support
31
+
32
+ def initialize(hash)
33
+ set_inner_hash(hash)
34
+ end
35
+ end
36
+
37
+ obj = DynamicObject.new({ port: 80, "host" => "localhost" })
38
+ p obj.port # 80
39
+ p obj[:host] # "localhost"
40
+ obj[:host] = "192.168.1.1"
41
+ p obj.host # "192.168.1.1"
42
+ ```
43
+
44
+ ## License
45
+
46
+ [MIT](LICENSE)
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,8 @@
1
+ # Hash with attribute accessor
2
+ class NeoHash
3
+ # Constructor
4
+ # @param [Hash] h ({}) initial hash
5
+ def initialize(h = {})
6
+ set_inner_hash(h)
7
+ end
8
+ end
@@ -0,0 +1,120 @@
1
+ class NeoHash
2
+ # Provides attribute readers and Hash features
3
+ # A class that includes this has `@h` as inner hash.
4
+ # @example
5
+ # class DynamicObject
6
+ # include NeoHash::Support
7
+ #
8
+ # def initialize(hash)
9
+ # set_inner_hash(hash)
10
+ # end
11
+ # end
12
+ #
13
+ # obj = DynamicObject.new({ port: 80, "host" => "localhost" })
14
+ # p obj.port # 80
15
+ # p obj[:host] # "localhost"
16
+ # obj[:host] = "192.168.1.1"
17
+ # p obj.host # "192.168.1.1"
18
+ module Support
19
+ # @see Hash#[]
20
+ def [](key)
21
+ @h[key.to_sym]
22
+ end
23
+
24
+ # @see Hash#[]=
25
+ def []=(key, value)
26
+ sk = key.to_sym
27
+ define_attr_accessor(sk) unless @h.key?(sk)
28
+ @h[sk] = convert_val(value)
29
+ end
30
+
31
+ # @see Hash#include?
32
+ def include?(key)
33
+ @h.include?(key.to_sym)
34
+ end
35
+
36
+ alias_method :has_key?, :include?
37
+ alias_method :key?, :include?
38
+ alias_method :member?, :include?
39
+
40
+ # @see Hash#delete
41
+ def delete(key, &block)
42
+ @h.delete(key.to_sym, &block)
43
+ end
44
+
45
+ # @see Hash#fetch
46
+ def fetch(*args, &block)
47
+ args[0] = args[0].to_sym
48
+ @h.fetch(*args, &block)
49
+ end
50
+
51
+ def method_missing(method, *args, &block)
52
+ if Hash.public_method_defined?(method)
53
+ @h.send method, *args, &block
54
+ else
55
+ super
56
+ end
57
+ end
58
+
59
+ # Convert to original Hash
60
+ #
61
+ # @return [Hash] hash
62
+ # @param [Hash] opts the options to intialize
63
+ # @option opts [String] :symbolize_keys (true) whether symbolize names or not
64
+ def to_h(opts = {})
65
+ symbolize = opts.fetch(:symbolize_keys, true)
66
+
67
+ @h.map {|key, val|
68
+ [symbolize ? key : key.to_s, to_primitive(val, opts)]
69
+ }.to_h
70
+ end
71
+
72
+ # Implicitly convert to Hash
73
+ # @return [Hash] generated hash
74
+ def to_hash
75
+ to_h
76
+ end
77
+
78
+ protected
79
+
80
+ # Set inner hash and replace hashes in descendant with wrapping them recursively
81
+ def set_inner_hash(hash)
82
+ @h = coerce_hash(hash)
83
+ define_attr_accessors
84
+ end
85
+
86
+ private
87
+
88
+ def define_attr_accessors
89
+ @h.each_key {|name| define_attr_accessor(name) }
90
+ end
91
+
92
+ def define_attr_accessor(name)
93
+ define_singleton_method name, lambda { @h[name] }
94
+ define_singleton_method "#{name}=", lambda {|val| @h[name] = convert_val(val) }
95
+ end
96
+
97
+ def coerce_hash(hash)
98
+ {}.tap do |new_h|
99
+ hash.each do |name, val|
100
+ new_h[name.to_sym] = convert_val(val)
101
+ end
102
+ end
103
+ end
104
+
105
+ def convert_val(val)
106
+ return NeoHash.new(val) if val.is_a?(Hash)
107
+ return val.map {|item| convert_val(item) } if val.instance_of?(Array)
108
+ val
109
+ end
110
+
111
+ def to_primitive(val, opts)
112
+ return val.to_h(opts) if val.instance_of?(NeoHash)
113
+ return val.to_hash if val.respond_to?(:to_hash)
114
+ return val.map {|item| to_primitive(item, opts) } if val.instance_of?(Array)
115
+ val
116
+ end
117
+ end
118
+
119
+ include Support
120
+ end
@@ -0,0 +1,3 @@
1
+ class NeoHash
2
+ VERSION = '0.3.0'
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'neo_hash/version'
2
+ require 'neo_hash/neo_hash'
3
+ require 'neo_hash/support'
@@ -0,0 +1,168 @@
1
+ require 'spec_helper'
2
+
3
+ describe NeoHash do
4
+ subject { described_class.new(h) }
5
+
6
+ let!(:h) do
7
+ {
8
+ 'port' => 80,
9
+ 'log' => { 'level' => 'warn' },
10
+ 'array' => [123, {
11
+ 'data' => 'data'
12
+ }, {
13
+ 'value' => 100
14
+ }]
15
+ }
16
+ end
17
+
18
+ let!(:h_symbol) do
19
+ {
20
+ port: 80,
21
+ log: { level: 'warn' },
22
+ array: [123, {
23
+ data: 'data'
24
+ }, {
25
+ value: 100
26
+ }]
27
+ }
28
+ end
29
+
30
+ describe '#initialize' do
31
+ it 'an instance that has accessors by method' do
32
+ expect(subject.port).to eq(80)
33
+ expect(subject.log.level).to eq('warn')
34
+ expect(subject.array[0]).to eq(123)
35
+ expect(subject.array[1].data).to eq('data')
36
+ expect(subject.array[2].value).to eq(100)
37
+ end
38
+
39
+ it 'an instance that has accessors by symbol' do
40
+ expect(subject[:port]).to eq(80)
41
+ expect(subject[:log][:level]).to eq('warn')
42
+ expect(subject[:array][0]).to eq(123)
43
+ expect(subject[:array][1].data).to eq('data')
44
+ expect(subject[:array][2].value).to eq(100)
45
+ end
46
+
47
+ it 'an instance that has accessors by string' do
48
+ expect(subject['port']).to eq(80)
49
+ expect(subject['log']['level']).to eq('warn')
50
+ expect(subject['array'][0]).to eq(123)
51
+ expect(subject['array'][1].data).to eq('data')
52
+ expect(subject['array'][2].value).to eq(100)
53
+ end
54
+
55
+ it 'an instance that has accessors by various ways' do
56
+ expect(subject.log[:level]).to eq('warn')
57
+ expect(subject.log['level']).to eq('warn')
58
+ expect(subject[:log].level).to eq('warn')
59
+ expect(subject[:log]['level']).to eq('warn')
60
+ expect(subject['log'].level).to eq('warn')
61
+ expect(subject['log'][:level]).to eq('warn')
62
+ end
63
+ end
64
+
65
+ describe '#to_h' do
66
+ it 'creates an hash with name as symbol' do
67
+ expect(subject.to_h).to eq(h_symbol)
68
+ end
69
+
70
+ it 'creates an hash with name as symbol' do
71
+ expect(subject.to_h(symbolize_keys: true)).to eq(h_symbol)
72
+ end
73
+
74
+ it 'creates an hash with name as string' do
75
+ expect(subject.to_h(symbolize_keys: false)).to eq(h)
76
+ end
77
+ end
78
+
79
+ describe '#to_hash' do
80
+ it 'creates an hash with name as symbol' do
81
+ expect(subject.to_hash).to eq(h_symbol)
82
+ end
83
+ end
84
+
85
+ describe 'dynamically change settings' do
86
+ it 'can add new settings' do
87
+ subject.log['file'] = '/var/log/app.log'
88
+ subject[:backend] = { host: 'cms.example.com', "port" => 7080 }
89
+
90
+ expect(subject.log.level).to eq('warn')
91
+ expect(subject.log.file).to eq('/var/log/app.log')
92
+ expect(subject.backend.host).to eq('cms.example.com')
93
+ expect(subject.backend.port).to eq(7080)
94
+ end
95
+
96
+ it 'can update values' do
97
+ subject['port'] = 8888
98
+ expect(subject.port).to eq(8888)
99
+
100
+ subject.port = 789
101
+ expect(subject.port).to eq(789)
102
+
103
+ subject.log.level = :trace
104
+ expect(subject.log.level).to eq(:trace)
105
+
106
+ subject.log = { file: '/var/log/app2.log' }
107
+ expect{ subject.log.level }.to raise_error(NoMethodError)
108
+ expect(subject.log.file).to eq('/var/log/app2.log')
109
+ end
110
+ end
111
+
112
+ describe '#include?' do
113
+ it 'call inner hash#include?' do
114
+ expect(subject.include?(:port)).to be_truthy
115
+ expect(subject.include?('port')).to be_truthy
116
+ end
117
+ end
118
+
119
+ describe '#fetch' do
120
+ it 'call inner hash#fetch' do
121
+ expect(subject.fetch(:port)).to eq(80)
122
+ expect(subject.fetch('port')).to eq(80)
123
+ expect{ subject.fetch(:none) }.to raise_error(KeyError)
124
+ expect(subject.fetch(:none, 'none')).to eq('none')
125
+ expect(subject.fetch(:none) {|k| k }).to eq(:none)
126
+ end
127
+ end
128
+
129
+ describe '#delete' do
130
+ it 'call inner hash#delete' do
131
+ expect(subject.delete(:none)).to be_nil
132
+ expect(subject.delete('port')).to eq(80)
133
+ expect(subject.delete(:log)).not_to be_nil
134
+ end
135
+ end
136
+
137
+ describe '#method_missing' do
138
+ it 'can call inner hash#each' do
139
+ subject.each do |k, v|
140
+ expect(k).to eq :port
141
+ expect(v).to eq 80
142
+ break
143
+ end
144
+ end
145
+
146
+ it 'can call inner hash#empty?' do
147
+ expect(subject.empty?).to be_falsey
148
+ end
149
+
150
+ it 'can call inner hash#keys' do
151
+ expect(subject.keys).to eq([:port, :log, :array])
152
+ end
153
+
154
+ it 'can call inner hash#map' do
155
+ expect(subject.map {|k, v| k }).to eq [:port, :log, :array]
156
+ end
157
+
158
+ it 'can call inner hash#select' do
159
+ h = subject.select {|k, v| k == :port }
160
+ expect(h[:port]).to eq(80)
161
+ end
162
+
163
+ it 'can call inner hash#store' do
164
+ subject.store(:port, 999)
165
+ expect(subject.port).to eq(999)
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,7 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter "/spec/"
4
+ end
5
+
6
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
7
+ require 'neohash'
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: neohash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Toshimitsu Takahashi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-17 00:00:00.000000000 Z
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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Accessible Hash and attaching it to an Object
56
+ email:
57
+ - toshi@tilfin.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - lib/neo_hash/neo_hash.rb
69
+ - lib/neo_hash/support.rb
70
+ - lib/neo_hash/version.rb
71
+ - lib/neohash.rb
72
+ - spec/neo_hash_spec.rb
73
+ - spec/spec_helper.rb
74
+ homepage: https://github.com/tilfin/neohash
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.7.3
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Accessible Hash and attaching it to an Object
98
+ test_files:
99
+ - spec/neo_hash_spec.rb
100
+ - spec/spec_helper.rb