intellihash 0.1.0 → 0.1.1
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/README.md +11 -11
- data/bin/console +2 -2
- data/lib/intellihash/callbacks.rb +2 -2
- data/lib/intellihash/configuration.rb +4 -4
- data/lib/intellihash/mixins.rb +9 -9
- data/lib/intellihash/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 429b0a803c4c01cb64cfe602910d610e01b5566c947937de8c474dbee3c71018
|
4
|
+
data.tar.gz: 82174f3ce70dbbec9a6b9c5807b942797a1e30307a3145dd72a92df490a5b184
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 889c62ba1d243f33768b73764266d7f708782f479dac23cc9556e9e1a9c08af091fe0f25157f842457972c64ef70ac93bbde9eb1d61f1e0b9579239a9ed71ede
|
7
|
+
data.tar.gz: 4cd3d0efd49c59df9bfe8b0638c1e713b05ae25d5e39d9866dbf5e7e8ef2d6383a33793c280735896fbe7b4f0f1666e9aa377939249e8b6178a144901fbf8a6e
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
A fast implementation of hashes as objects, benchmarked against OpenStruct. It allows chaining hash attributes as method calls instead of standard hash syntax.
|
4
4
|
|
5
|
-
Since an Intellihash extends the native Ruby `Hash`, this means instantiating a new
|
5
|
+
Since an Intellihash extends the native Ruby `Hash`, this means instantiating a new intelligent hash is just as fast as a normal hash, _and_ you retain the ability to call all the normal instance methods on it.
|
6
6
|
|
7
7
|
```ruby
|
8
8
|
intellihash = {
|
@@ -56,7 +56,7 @@ Creating an Intellihash is approximately 5x faster than instantiating an OpenStr
|
|
56
56
|
```
|
57
57
|
user system total real
|
58
58
|
OpenStruct: 4.046875 0.906250 4.953125 ( 4.979611)
|
59
|
-
Intellihash:
|
59
|
+
Intellihash: 0.828125 0.125000 0.953125 ( 0.956110)
|
60
60
|
```
|
61
61
|
|
62
62
|
See [Testing Performance](#testing-performance) for details on running benchmarks.
|
@@ -85,7 +85,7 @@ If you need to customize Intellihash, you may create a configuration:
|
|
85
85
|
Intellihash.configure do |config|
|
86
86
|
config.enabled = true # (Boolean) Default: false
|
87
87
|
config.default_format = :symbol # (Symbol) Default: :symbol
|
88
|
-
config.
|
88
|
+
config.intelligent_by_default = false # (Boolean) Default: false
|
89
89
|
end
|
90
90
|
```
|
91
91
|
|
@@ -99,9 +99,9 @@ end
|
|
99
99
|
- **default_format**
|
100
100
|
- Valid values: `:sym, :symbol, :str, :string, :any`
|
101
101
|
- This determines the default storage and retrieval options
|
102
|
-
- **
|
103
|
-
- Whether newly created hashes are
|
104
|
-
- When `false`, new hashes can still be converted to
|
102
|
+
- **intelligent_by_default**
|
103
|
+
- Whether newly created hashes are intelligent
|
104
|
+
- When `false`, new hashes can still be converted to intelligent hashes via `hash.is_intelligent = true` or `hash.to_intellihash!`
|
105
105
|
|
106
106
|
### Rails
|
107
107
|
|
@@ -111,21 +111,21 @@ Place the above configuration in an initializer (such as `config/initializers/in
|
|
111
111
|
|
112
112
|
### Instantiating an Intellihash
|
113
113
|
|
114
|
-
If you've configured Intellihash `
|
114
|
+
If you've configured Intellihash `intelligent_by_default = true`, you need to do nothing else as _all_ new hashes are Intellihashes.
|
115
115
|
|
116
116
|
However, if you prefer not to use this configuration, you can create an Intellihash from any existing hash:
|
117
117
|
|
118
118
|
```ruby
|
119
119
|
hash = { foo: :bar }
|
120
120
|
|
121
|
-
hash.
|
121
|
+
hash.is_intelligent?
|
122
122
|
#=> false
|
123
123
|
|
124
|
-
hash.
|
124
|
+
hash.is_intelligent = true
|
125
125
|
# or
|
126
126
|
hash.to_intellihash!
|
127
127
|
|
128
|
-
hash.
|
128
|
+
hash.is_intelligent?
|
129
129
|
#=> true
|
130
130
|
```
|
131
131
|
|
@@ -155,7 +155,7 @@ When configured correctly, they work even when the object contains arrays and ot
|
|
155
155
|
```ruby
|
156
156
|
Intellihash.configure do |config|
|
157
157
|
config.enabled = true
|
158
|
-
config.
|
158
|
+
config.intelligent_by_default = true
|
159
159
|
config.default_format = :any
|
160
160
|
end
|
161
161
|
|
data/bin/console
CHANGED
@@ -4,7 +4,7 @@ module Intellihash
|
|
4
4
|
module Callbacks
|
5
5
|
# https://ruby-doc.org/core-3.0.1/Hash.html
|
6
6
|
#
|
7
|
-
# Methods that return a copy of :self: need this callback to populate :
|
7
|
+
# Methods that return a copy of :self: need this callback to populate :intelligent: from :self:
|
8
8
|
AFTER_CALLBACK_TARGETS = %i[
|
9
9
|
compact
|
10
10
|
invert
|
@@ -24,7 +24,7 @@ module Intellihash
|
|
24
24
|
result = super(*args, &block)
|
25
25
|
|
26
26
|
# Register :after: callbacks
|
27
|
-
result.
|
27
|
+
result.is_intelligent = intelligent if result.respond_to?(:is_intelligent=) && !result.frozen?
|
28
28
|
|
29
29
|
result
|
30
30
|
end
|
@@ -25,13 +25,13 @@ module Intellihash
|
|
25
25
|
end
|
26
26
|
|
27
27
|
class Configuration
|
28
|
-
attr_accessor :enabled, :
|
28
|
+
attr_accessor :enabled, :intelligent_by_default
|
29
29
|
attr_reader :default_format
|
30
30
|
|
31
31
|
def initialize
|
32
|
-
@default_format
|
33
|
-
@enabled
|
34
|
-
@
|
32
|
+
@default_format = :symbol
|
33
|
+
@enabled = false
|
34
|
+
@intelligent_by_default = false
|
35
35
|
end
|
36
36
|
|
37
37
|
def default_format=(other)
|
data/lib/intellihash/mixins.rb
CHANGED
@@ -2,22 +2,22 @@
|
|
2
2
|
|
3
3
|
module Intellihash
|
4
4
|
module Mixins
|
5
|
-
def
|
6
|
-
@
|
5
|
+
def intelligent
|
6
|
+
@intelligent = @intelligent.nil? ? Intellihash.configuration.intelligent_by_default : @intelligent
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
9
|
+
def is_intelligent=(value)
|
10
10
|
# Ensure this is a boolean
|
11
|
-
@
|
11
|
+
@intelligent = value == true
|
12
12
|
end
|
13
13
|
|
14
14
|
def to_intellihash
|
15
|
-
@
|
15
|
+
@intelligent = true
|
16
16
|
self
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
20
|
-
|
19
|
+
def is_intelligent?
|
20
|
+
intelligent
|
21
21
|
end
|
22
22
|
|
23
23
|
def default_format
|
@@ -39,7 +39,7 @@ module Intellihash
|
|
39
39
|
}.freeze
|
40
40
|
|
41
41
|
def method_missing(method_name, *args, **kwargs, &block)
|
42
|
-
super unless respond_to?(:
|
42
|
+
super unless respond_to?(:is_intelligent?) && is_intelligent?
|
43
43
|
|
44
44
|
if method_name[-1] == '='
|
45
45
|
send(:store, method_name[0, method_name.size - 1].send(key_store_as), args.first)
|
@@ -53,7 +53,7 @@ module Intellihash
|
|
53
53
|
end
|
54
54
|
|
55
55
|
def respond_to_missing?(*)
|
56
|
-
|
56
|
+
is_intelligent? ? true : super
|
57
57
|
end
|
58
58
|
|
59
59
|
def key_store_as
|
data/lib/intellihash/version.rb
CHANGED