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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f5f83e22faa7ed0b0ff1d37038a51dcb92b5bbf07e2ae4e0256091d5954da642
4
- data.tar.gz: 7c6d956f731420f46a1c924a6e7c8f2213aede31fb6f90ec397b734482eeb806
3
+ metadata.gz: 2de48212f044e48916dff969e09f1b5a529bc0a69cf2f926f06ebdf9b2ece979
4
+ data.tar.gz: c3734d811a204f19b654f8ae8488af8f492b2cb0dacdd29ceea6a085376aad4a
5
5
  SHA512:
6
- metadata.gz: 67ac75655f768375a9148cb86b51d9e39ab2071376a6480d03d33d93a289fb745fa18cb74ec55e31e16e729a86ae5c3e4d79d71314da44c80d8e0bfc324252ea
7
- data.tar.gz: ba7c1cbcbc7c1a538c7d8b7d9905f9f9a581cba4264339f5d1e41497128c3d512e817046b87c2964b44230997203bd2d138684732604bfd289c6c6230dd6c398
6
+ metadata.gz: 15bcaa8249b0ecd0a8e5f7fa0600657910cda2ad24dffaead96c10475d388b768f73d9489ffc834765920a7b1b5b5e109cf4718473f43c37f82a95d6b3c1e745
7
+ data.tar.gz: d45f1f6e0edd465a0eadb9003acf807f424b4ec3b255d8af660f42501c6319c1434123b0b9e0aed4738f064823ef5ffd108951b2db52ce61bce989f324e0645f
@@ -1,3 +1,9 @@
1
+ # 1.3.0 (September 1st, 2020)
2
+
3
+ Additions:
4
+
5
+ * Added dynamic class constantization when a string is registered for a Factory.
6
+
1
7
  # 1.2.0 (June 9th, 2020)
2
8
 
3
9
  * Bumped minimum Ruby version to >= 2.5
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
- @registry = registry.symbolize_keys
21
- @type_key = type_key.to_s.to_sym
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 = registry[type]
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
@@ -8,5 +8,5 @@
8
8
  #
9
9
 
10
10
  module ActsAsHashable
11
- VERSION = '1.2.0'
11
+ VERSION = '1.3.0-alpha'
12
12
  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.2.0
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-06-09 00:00:00.000000000 Z
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: '0'
193
+ version: 1.3.1
193
194
  requirements: []
194
195
  rubygems_version: 3.0.3
195
196
  signing_key: