acts_as_hashable 1.2.0 → 1.3.0.pre.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +15 -0
- data/lib/acts_as_hashable/constant_resolver.rb +34 -0
- data/lib/acts_as_hashable/type_factory.rb +20 -5
- data/lib/acts_as_hashable/version.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2de48212f044e48916dff969e09f1b5a529bc0a69cf2f926f06ebdf9b2ece979
|
4
|
+
data.tar.gz: c3734d811a204f19b654f8ae8488af8f492b2cb0dacdd29ceea6a085376aad4a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15bcaa8249b0ecd0a8e5f7fa0600657910cda2ad24dffaead96c10475d388b768f73d9489ffc834765920a7b1b5b5e109cf4718473f43c37f82a95d6b3c1e745
|
7
|
+
data.tar.gz: d45f1f6e0edd465a0eadb9003acf807f424b4ec3b255d8af660f42501c6319c1434123b0b9e0aed4738f064823ef5ffd108951b2db52ce61bce989f324e0645f
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -197,6 +197,21 @@ end
|
|
197
197
|
|
198
198
|
In case you need full control of the registry you can also choose to simply override the class-level `registry` method which will simply return a hash of keys (names) and values (class constants).
|
199
199
|
|
200
|
+
### Resolving Constants at Runtime
|
201
|
+
|
202
|
+
Factories can also be resolved using Ruby's Object#const_get and Object#const_missing. Simply register a string representing the class in order to use these mechanics, such as:
|
203
|
+
|
204
|
+
```ruby
|
205
|
+
class ExampleFactory
|
206
|
+
acts_as_hashable_factory
|
207
|
+
|
208
|
+
type_key 'object_type'
|
209
|
+
|
210
|
+
register 'Pet', 'Pet'
|
211
|
+
|
212
|
+
register 'HeadOfHousehold', ->(_key) { HeadOfHousehold }
|
213
|
+
end
|
214
|
+
|
200
215
|
## Contributing
|
201
216
|
|
202
217
|
### Development Environment Configuration
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright (c) 2019-present, Blue Marble Payroll, LLC
|
5
|
+
#
|
6
|
+
# This source code is licensed under the MIT license found in the
|
7
|
+
# LICENSE file in the root directory of this source tree.
|
8
|
+
#
|
9
|
+
|
10
|
+
module ActsAsHashable
|
11
|
+
# This class is responsible for turning strings and symbols into constants.
|
12
|
+
# It does not deal with inflection, simply just constant resolution.
|
13
|
+
class ConstantResolver
|
14
|
+
# Only use Module constant resolution if a string or symbol was passed in.
|
15
|
+
# Any other type is defined as an acceptable constant and is simply returned.
|
16
|
+
def constantize(value)
|
17
|
+
value.is_a?(String) || value.is_a?(Symbol) ? object_constant(value) : value
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
# If the constant has been loaded, we can safely use it through const_get.
|
23
|
+
# If the constant has not been loaded, we need to defer to const_missing to resolve it.
|
24
|
+
# If we blindly call const_get, it may return false positives for namespaced constants
|
25
|
+
# or anything nested.
|
26
|
+
def object_constant(value)
|
27
|
+
if Object.const_defined?(value, false)
|
28
|
+
Object.const_get(value, false)
|
29
|
+
else
|
30
|
+
Object.const_missing(value)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -7,6 +7,8 @@
|
|
7
7
|
# LICENSE file in the root directory of this source tree.
|
8
8
|
#
|
9
9
|
|
10
|
+
require_relative 'constant_resolver'
|
11
|
+
|
10
12
|
module ActsAsHashable
|
11
13
|
# A TypeFactory object understands how to build objects using a special designated 'type' key.
|
12
14
|
class TypeFactory
|
@@ -17,8 +19,9 @@ module ActsAsHashable
|
|
17
19
|
attr_reader :registry, :type_key
|
18
20
|
|
19
21
|
def initialize(registry = {}, type_key = DEFAULT_TYPE_KEY)
|
20
|
-
@
|
21
|
-
@
|
22
|
+
@constant_resolver = ConstantResolver.new
|
23
|
+
@registry = registry.symbolize_keys
|
24
|
+
@type_key = type_key.to_s.to_sym
|
22
25
|
|
23
26
|
freeze
|
24
27
|
end
|
@@ -34,9 +37,7 @@ module ActsAsHashable
|
|
34
37
|
def make(config = {})
|
35
38
|
config = (config || {}).symbolize_keys
|
36
39
|
type = config[type_key].to_s.to_sym
|
37
|
-
object_class =
|
38
|
-
|
39
|
-
raise ArgumentError, "cannot find registration for: '#{type}'" unless object_class
|
40
|
+
object_class = resolve_object_class(type)
|
40
41
|
|
41
42
|
config_without_type = config.reject { |k| k == type_key }
|
42
43
|
|
@@ -47,5 +48,19 @@ module ActsAsHashable
|
|
47
48
|
|
48
49
|
object_class.send(method_name, config_without_type)
|
49
50
|
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
attr_reader :constant_resolver
|
55
|
+
|
56
|
+
def resolve_object_class(type)
|
57
|
+
object_class = registry[type]
|
58
|
+
|
59
|
+
raise ArgumentError, "cannot find registration for: '#{type}'" unless object_class
|
60
|
+
|
61
|
+
return object_class unless object_class.is_a?(String)
|
62
|
+
|
63
|
+
constant_resolver.constantize(object_class)
|
64
|
+
end
|
50
65
|
end
|
51
66
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_hashable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0.pre.alpha
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Ruggio
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: caution
|
@@ -162,6 +162,7 @@ files:
|
|
162
162
|
- bin/console
|
163
163
|
- exe/.gitkeep
|
164
164
|
- lib/acts_as_hashable.rb
|
165
|
+
- lib/acts_as_hashable/constant_resolver.rb
|
165
166
|
- lib/acts_as_hashable/factory.rb
|
166
167
|
- lib/acts_as_hashable/hash_refinements.rb
|
167
168
|
- lib/acts_as_hashable/hashable.rb
|
@@ -187,9 +188,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
187
188
|
version: '2.5'
|
188
189
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
189
190
|
requirements:
|
190
|
-
- - "
|
191
|
+
- - ">"
|
191
192
|
- !ruby/object:Gem::Version
|
192
|
-
version:
|
193
|
+
version: 1.3.1
|
193
194
|
requirements: []
|
194
195
|
rubygems_version: 3.0.3
|
195
196
|
signing_key:
|