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 +4 -4
- data/lib/hashcraft/base.rb +34 -18
- data/lib/hashcraft/dsl.rb +23 -10
- data/lib/hashcraft/generic/dictionary.rb +6 -0
- data/lib/hashcraft/generic/registry.rb +1 -5
- data/lib/hashcraft/option.rb +10 -32
- data/lib/hashcraft/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: de90f4a4235178d5dd03c534a7b371583825795ea40b0218f678e5be516dbaba
|
4
|
+
data.tar.gz: 0227f29c0dc69de054f58abd31f854fe4f2a9fed7ad4ba60c439d8ca76d09cbe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d1376245cf1dbf76948e6241965287776c169cf3d2c5e2879e396be0697f3bac9eb25539c3b8198997f4a5f2b2a74a9dfa9943229cf7f2183eeb196bb4cfeaa
|
7
|
+
data.tar.gz: 4da6b340943dd7241ed820853892808664aea71ed7ea45047764aaf1cc63e6b703129d9ab446fcac49b2b8417ff6378d2142fb5329a6c855211bfdffab1f1c75
|
data/lib/hashcraft/base.rb
CHANGED
@@ -19,12 +19,13 @@ module Hashcraft
|
|
19
19
|
:option?,
|
20
20
|
:option_set,
|
21
21
|
:find_option,
|
22
|
-
:
|
23
|
-
:
|
22
|
+
:key_transformer_to_use,
|
23
|
+
:value_transformer_to_use
|
24
24
|
|
25
25
|
def initialize(opts = {}, &block)
|
26
|
-
@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
|
52
|
-
option_set.
|
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)
|
72
|
-
|
73
|
-
|
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
|
data/lib/hashcraft/dsl.rb
CHANGED
@@ -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 { @
|
22
|
+
tap { @local_key_transformer = TransformerRegistry.resolve(name) }
|
20
23
|
end
|
21
24
|
|
22
25
|
def value_transformer(name)
|
23
|
-
tap { @
|
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
|
@@ -46,11 +46,7 @@ module Hashcraft
|
|
46
46
|
def resolve(value)
|
47
47
|
return value unless lookup?(value)
|
48
48
|
|
49
|
-
|
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
|
data/lib/hashcraft/option.rb
CHANGED
@@ -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
|
41
|
-
|
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
|
-
|
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
|
data/lib/hashcraft/version.rb
CHANGED