hashcraft 1.0.0.pre.alpha.3 → 1.0.0.pre.alpha.4

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: 5eeb66db8e62e9998006101e746cf23be8e66da88a98b3292b3f769b89fb5bd9
4
- data.tar.gz: dba52c0e835c9c09fb82e5fa963d45c57a8988dcde962eb8bb5e6e3feb0e3a9f
3
+ metadata.gz: de90f4a4235178d5dd03c534a7b371583825795ea40b0218f678e5be516dbaba
4
+ data.tar.gz: 0227f29c0dc69de054f58abd31f854fe4f2a9fed7ad4ba60c439d8ca76d09cbe
5
5
  SHA512:
6
- metadata.gz: 4d493d7190833e14ed7c14f3deaeec7a86ad1d779cdd93d22141d80195986a0c18382fa1011e4cce2fd872ca2ea1c00aca4797aafdc70193b9405ccfd0c0af82
7
- data.tar.gz: aae0ba2e2b5dd0b7bd55514276d823337dcfe7e0f5a17fe149cfbc521117b3706a31c9056516c2fd0c1d7f495f2d48859ac4b77f9ee66d94a01155ad4d417768
6
+ metadata.gz: 1d1376245cf1dbf76948e6241965287776c169cf3d2c5e2879e396be0697f3bac9eb25539c3b8198997f4a5f2b2a74a9dfa9943229cf7f2183eeb196bb4cfeaa
7
+ data.tar.gz: 4da6b340943dd7241ed820853892808664aea71ed7ea45047764aaf1cc63e6b703129d9ab446fcac49b2b8417ff6378d2142fb5329a6c855211bfdffab1f1c75
@@ -19,12 +19,13 @@ module Hashcraft
19
19
  :option?,
20
20
  :option_set,
21
21
  :find_option,
22
- :key_transformer_object,
23
- :value_transformer_object
22
+ :key_transformer_to_use,
23
+ :value_transformer_to_use
24
24
 
25
25
  def initialize(opts = {}, &block)
26
- @data = make_default_data
26
+ @data = {}
27
27
 
28
+ load_default_data
28
29
  load_opts(opts)
29
30
 
30
31
  return unless block_given?
@@ -48,14 +49,8 @@ module Hashcraft
48
49
 
49
50
  attr_reader :data
50
51
 
51
- def make_default_data
52
- option_set.values.each_with_object({}) do |o, memo|
53
- o.default!(
54
- memo,
55
- key_transformer_object,
56
- value_transformer_object
57
- )
58
- end
52
+ def load_default_data
53
+ option_set.each { |option| default!(option) }
59
54
  end
60
55
 
61
56
  def load_opts(opts)
@@ -68,13 +63,9 @@ module Hashcraft
68
63
 
69
64
  def method_missing(method_name, *arguments, &block)
70
65
  if option?(method_name)
71
- find_option(method_name).value!(
72
- data,
73
- arguments.first,
74
- key_transformer_object,
75
- value_transformer_object,
76
- &block
77
- )
66
+ option = find_option(method_name)
67
+
68
+ value!(option, arguments.first, &block)
78
69
  else
79
70
  super
80
71
  end
@@ -95,5 +86,30 @@ module Hashcraft
95
86
 
96
87
  self
97
88
  end
89
+
90
+ def default!(option)
91
+ return self unless option.eager?
92
+
93
+ key = hash_key(option)
94
+ value = value_transformer_to_use.transform(option.default.dup, option)
95
+
96
+ data[key] = value
97
+
98
+ self
99
+ end
100
+
101
+ def value!(option, value, &block)
102
+ key = hash_key(option)
103
+ value = option.craft_value(value, &block)
104
+ value = value_transformer_to_use.transform(value, option)
105
+
106
+ option.value!(data, key, value)
107
+
108
+ self
109
+ end
110
+
111
+ def hash_key(option)
112
+ key_transformer_to_use.transform(option.hash_key, option)
113
+ end
98
114
  end
99
115
  end
@@ -15,12 +15,33 @@ module Hashcraft
15
15
  # OptionSet instance along with materializing one for its
16
16
  # inheritance chain (child has precedence.)
17
17
  module Dsl
18
+ attr_reader :local_key_transformer,
19
+ :local_value_transformer
20
+
18
21
  def key_transformer(name)
19
- tap { @key_transformer_object = TransformerRegistry.resolve(name) }
22
+ tap { @local_key_transformer = TransformerRegistry.resolve(name) }
20
23
  end
21
24
 
22
25
  def value_transformer(name)
23
- tap { @value_transformer_object = TransformerRegistry.resolve(name) }
26
+ tap { @local_value_transformer = TransformerRegistry.resolve(name) }
27
+ end
28
+
29
+ def key_transformer_to_use
30
+ return @key_transformer_to_use if @key_transformer_to_use
31
+
32
+ @closest_key_transformer =
33
+ ancestors.select { |a| a < Base }
34
+ .find(&:local_key_transformer)
35
+ &.local_key_transformer || Transformers::PassThru.instance
36
+ end
37
+
38
+ def value_transformer_to_use
39
+ return @value_transformer_to_use if @value_transformer_to_use
40
+
41
+ @closest_value_transformer =
42
+ ancestors.select { |a| a < Base }
43
+ .find(&:local_value_transformer)
44
+ &.local_value_transformer || Transformers::PassThru.instance
24
45
  end
25
46
 
26
47
  def option?(name)
@@ -54,13 +75,5 @@ module Hashcraft
54
75
  def local_option_set
55
76
  @local_option_set ||= Generic::Dictionary.new(key: :name)
56
77
  end
57
-
58
- def key_transformer_object
59
- @key_transformer_object || Transformers::PassThru.instance
60
- end
61
-
62
- def value_transformer_object
63
- @value_transformer_object || Transformers::PassThru.instance
64
- end
65
78
  end
66
79
  end
@@ -28,6 +28,12 @@ module Hashcraft
28
28
  freeze
29
29
  end
30
30
 
31
+ def each(&block)
32
+ return enum_for(:each) unless block_given?
33
+
34
+ values.each(&block)
35
+ end
36
+
31
37
  def exist?(key)
32
38
  !find(key).nil?
33
39
  end
@@ -46,11 +46,7 @@ module Hashcraft
46
46
  def resolve(value)
47
47
  return value unless lookup?(value)
48
48
 
49
- mutator = map[value.to_s]
50
-
51
- raise ArgumentError, "registration: #{value} not found" unless mutator
52
-
53
- mutator
49
+ map[value.to_s] || raise(ArgumentError, "registration: #{value} not found")
54
50
  end
55
51
 
56
52
  private
@@ -12,6 +12,10 @@ require_relative 'mutator_registry'
12
12
  module Hashcraft
13
13
  # Defines a method and corresponding attribute for a craftable class.
14
14
  class Option
15
+ extend Forwardable
16
+
17
+ def_delegators :mutator, :value!
18
+
15
19
  attr_reader :craft,
16
20
  :default,
17
21
  :eager,
@@ -19,6 +23,8 @@ module Hashcraft
19
23
  :mutator,
20
24
  :name
21
25
 
26
+ alias eager? eager
27
+
22
28
  def initialize(name, opts = {})
23
29
  raise ArgumentError, 'name is required' if name.to_s.empty?
24
30
 
@@ -37,44 +43,16 @@ module Hashcraft
37
43
  internal_meta[key.to_s.to_sym]
38
44
  end
39
45
 
40
- def default!(data, key_transformer, value_transformer)
41
- return self unless eager
42
-
43
- final_key = hash_key(key_transformer)
44
- value = value_transformer.transform(default.dup, self)
45
-
46
- data[final_key] = value
47
-
48
- self
46
+ def hash_key
47
+ key.empty? ? name : key
49
48
  end
50
49
 
51
- def value!(
52
- data,
53
- value,
54
- key_transformer,
55
- value_transformer,
56
- &block
57
- )
58
- value = craft_value(value, &block)
59
- value = value_transformer.transform(value, self)
60
-
61
- final_key = hash_key(key_transformer)
62
-
63
- mutator.value!(data, final_key, value)
64
-
65
- self
50
+ def craft_value(value, &block)
51
+ craft ? craft.new(value, &block) : value
66
52
  end
67
53
 
68
54
  private
69
55
 
70
56
  attr_reader :internal_meta
71
-
72
- def hash_key(key_transformer)
73
- key_transformer.transform(key.empty? ? name : key, self)
74
- end
75
-
76
- def craft_value(value, &block)
77
- craft ? craft.new(value, &block) : value
78
- end
79
57
  end
80
58
  end
@@ -8,5 +8,5 @@
8
8
  #
9
9
 
10
10
  module Hashcraft
11
- VERSION = '1.0.0-alpha.3'
11
+ VERSION = '1.0.0-alpha.4'
12
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashcraft
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.pre.alpha.3
4
+ version: 1.0.0.pre.alpha.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Ruggio