hashcraft 1.0.0.pre.alpha.4 → 1.0.0.pre.alpha.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +45 -9
- data/lib/hashcraft/mutator_registry.rb +6 -0
- data/lib/hashcraft/mutators/always_false.rb +21 -0
- data/lib/hashcraft/mutators/always_true.rb +21 -0
- data/lib/hashcraft/mutators/array.rb +2 -1
- data/lib/hashcraft/mutators/flat_array.rb +30 -0
- data/lib/hashcraft/mutators/property.rb +1 -3
- data/lib/hashcraft/option.rb +4 -4
- data/lib/hashcraft/version.rb +1 -1
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9c8a4af5a05076883863320714728589bf2b49a0bd3b7f78e59be638697f6c9
|
4
|
+
data.tar.gz: 076117b9ba1523dc28f1eb62ce0cf19633d5eefc45db338e3633a325e5884234
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7075af55fa51130fb90ab52fe7456e374faef7aba9b32a82ff9d88cff488b95ba378049f997e44bbd1f48f80cfdb66a71fcc37432359662ef92c31115c65d371
|
7
|
+
data.tar.gz: a5dbad75e29f37aac42b0142bec8ea7cb9c167408d9691c80247adf82475ed92c9769e2bd6be0e9bebe82c7e344bd3e44d149337bed0bf725107c9fde6b710db
|
data/README.md
CHANGED
@@ -147,7 +147,7 @@ This means there is an available method called api_url that can be called to set
|
|
147
147
|
* **eager**: always assign a value. When true it will always assign the key a value.
|
148
148
|
* **key**: allows for aliasing keys. If omitted, the key will be the option's method name (api_url as noted above).
|
149
149
|
* **meta**: used to store any arbitrary data that can be accessed with transformers.
|
150
|
-
* **mutator**: defines the type of
|
150
|
+
* **mutator**: defines the *type of update* to be made to the underlying value, defaulting to `property`. When the default, `property`, is used then it will simply assign the passed in value. Some other options are: `hash` and `array`. When hash is used then the passed in value will be merged onto the key's value. When array is used then the passed in value will be pushed onto the key's value. For other types see the `Hashcraft::MutatorRegistry` file.
|
151
151
|
|
152
152
|
### Internationalization Support
|
153
153
|
|
@@ -155,7 +155,7 @@ There is currently no first-class support for internationalization, but you can
|
|
155
155
|
|
156
156
|
### Transformers
|
157
157
|
|
158
|
-
Transformers are optional but come into play when you need any additional/special processing of keys and values. By default, keys and values use the pass-thru transformer
|
158
|
+
Transformers are optional but come into play when you need any additional/special processing of keys and values. By default, keys and values use the pass-thru transformer, `Hashcraft::Transformers::PassThru`, but can be explicitly passed any object that responds to `#transform(value, option)`.
|
159
159
|
|
160
160
|
#### Key Transformer Example
|
161
161
|
|
@@ -171,12 +171,35 @@ Say, for example, we wanted to transform all keys to camel case. We could creat
|
|
171
171
|
end
|
172
172
|
````
|
173
173
|
|
174
|
-
We can then use this when deriving hashes (
|
174
|
+
We can then use this when deriving hashes (building on the Grid example above):
|
175
175
|
|
176
176
|
````ruby
|
177
|
+
class Content < Hashcraft::Base
|
178
|
+
option :property
|
179
|
+
end
|
180
|
+
|
181
|
+
class Column < Hashcraft::Base
|
182
|
+
option :header
|
183
|
+
|
184
|
+
option :content, craft: Content,
|
185
|
+
mutator: :array,
|
186
|
+
key: :contents
|
187
|
+
end
|
188
|
+
|
189
|
+
class Grid < Hashcraft::Base
|
190
|
+
key_transformer CamelCase.new
|
191
|
+
|
192
|
+
option :api_url,
|
193
|
+
:name
|
194
|
+
|
195
|
+
option :column, craft: Column,
|
196
|
+
mutator: :array,
|
197
|
+
key: :columns
|
198
|
+
end
|
199
|
+
|
177
200
|
config = Grid.new do
|
178
201
|
api_url '/patients'
|
179
|
-
end.to_h
|
202
|
+
end.to_h
|
180
203
|
````
|
181
204
|
|
182
205
|
The resulting `config` value will now be:
|
@@ -190,12 +213,23 @@ The resulting `config` value will now be:
|
|
190
213
|
Note that this library ships with some basic transformers like the one mentioned above. If you want to use this then you can simply do this:
|
191
214
|
|
192
215
|
````ruby
|
216
|
+
class Grid < Hashcraft::Base
|
217
|
+
key_transformer :camel_case
|
218
|
+
|
219
|
+
option :api_url,
|
220
|
+
:name
|
221
|
+
|
222
|
+
option :column, craft: Column,
|
223
|
+
mutator: :array,
|
224
|
+
key: :columns
|
225
|
+
end
|
226
|
+
|
193
227
|
config = Grid.new do
|
194
228
|
api_url '/patients'
|
195
|
-
end.to_h
|
229
|
+
end.to_h
|
196
230
|
````
|
197
231
|
|
198
|
-
See Hashcraft::TransformerRegistry for a full list of provided transformers.
|
232
|
+
See the `Hashcraft::TransformerRegistry` file for a full list of provided transformers.
|
199
233
|
|
200
234
|
#### Value Transformer Example
|
201
235
|
|
@@ -213,10 +247,12 @@ class Localizer
|
|
213
247
|
end
|
214
248
|
````
|
215
249
|
|
216
|
-
Building on our Grid example, we could enhance the Column object
|
250
|
+
Building on our Grid example, we could enhance the Column object:
|
217
251
|
|
218
252
|
````ruby
|
219
253
|
class Column < Hashcraft::Base
|
254
|
+
value_transformer Localizer.new
|
255
|
+
|
220
256
|
option :header, meta: { localize: true }
|
221
257
|
|
222
258
|
option :content, craft: Content,
|
@@ -225,7 +261,7 @@ class Column < Hashcraft::Base
|
|
225
261
|
end
|
226
262
|
````
|
227
263
|
|
228
|
-
We can then use the new value transformer
|
264
|
+
We can then use the new value transformer, `Localizer`, when deriving hashes (building on the above Grid and updated Column example):
|
229
265
|
|
230
266
|
````yaml
|
231
267
|
en:
|
@@ -237,7 +273,7 @@ en:
|
|
237
273
|
config = Grid.new do
|
238
274
|
column header: :id
|
239
275
|
column header: :first
|
240
|
-
end.to_h
|
276
|
+
end.to_h
|
241
277
|
````
|
242
278
|
|
243
279
|
Assuming our en.yml looks like the above example and our locale is set to:en then the resulting `config` value will now be:
|
@@ -7,7 +7,10 @@
|
|
7
7
|
# LICENSE file in the root directory of this source tree.
|
8
8
|
#
|
9
9
|
|
10
|
+
require_relative 'mutators/always_false'
|
11
|
+
require_relative 'mutators/always_true'
|
10
12
|
require_relative 'mutators/array'
|
13
|
+
require_relative 'mutators/flat_array'
|
11
14
|
require_relative 'mutators/hash'
|
12
15
|
require_relative 'mutators/property'
|
13
16
|
|
@@ -16,7 +19,10 @@ module Hashcraft
|
|
16
19
|
class MutatorRegistry < Generic::Registry
|
17
20
|
def initialize
|
18
21
|
super(
|
22
|
+
'always_false' => Mutators::AlwaysFalse.instance,
|
23
|
+
'always_true' => Mutators::AlwaysTrue.instance,
|
19
24
|
'array' => Mutators::Array.instance,
|
25
|
+
'flat_array' => Mutators::FlatArray.instance,
|
20
26
|
'hash' => Mutators::Hash.instance,
|
21
27
|
'property' => Mutators::Property.instance,
|
22
28
|
'' => Mutators::Property.instance
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright (c) 2020-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 Hashcraft
|
11
|
+
module Mutators
|
12
|
+
# Set to false, no matter what.
|
13
|
+
class AlwaysFalse
|
14
|
+
include Singleton
|
15
|
+
|
16
|
+
def value!(data, key, _value)
|
17
|
+
tap { data[key] = false }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright (c) 2020-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 Hashcraft
|
11
|
+
module Mutators
|
12
|
+
# Set to true, no matter what.
|
13
|
+
class AlwaysTrue
|
14
|
+
include Singleton
|
15
|
+
|
16
|
+
def value!(data, key, _value)
|
17
|
+
tap { data[key] = true }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -9,13 +9,14 @@
|
|
9
9
|
|
10
10
|
module Hashcraft
|
11
11
|
module Mutators
|
12
|
-
# When a hash's key is an
|
12
|
+
# When a hash's key is an array then this mutator can be used to push a new value on the
|
13
13
|
# respective array.
|
14
14
|
class Array
|
15
15
|
include Singleton
|
16
16
|
|
17
17
|
def value!(data, key, value)
|
18
18
|
data[key] ||= []
|
19
|
+
|
19
20
|
data[key] << value
|
20
21
|
|
21
22
|
self
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright (c) 2020-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 Hashcraft
|
11
|
+
module Mutators
|
12
|
+
# If the value is an array then concat, if it is not an array then push.
|
13
|
+
class FlatArray
|
14
|
+
include Singleton
|
15
|
+
|
16
|
+
def value!(data, key, value)
|
17
|
+
data[key] ||= []
|
18
|
+
|
19
|
+
# Prefixed Array with double colons to not get confused with our Array mutator class.
|
20
|
+
if value.is_a?(::Array)
|
21
|
+
data[key] += value
|
22
|
+
else
|
23
|
+
data[key] << value
|
24
|
+
end
|
25
|
+
|
26
|
+
self
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/hashcraft/option.rb
CHANGED
@@ -12,10 +12,6 @@ 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
|
-
|
19
15
|
attr_reader :craft,
|
20
16
|
:default,
|
21
17
|
:eager,
|
@@ -39,6 +35,10 @@ module Hashcraft
|
|
39
35
|
freeze
|
40
36
|
end
|
41
37
|
|
38
|
+
def value!(data, key, value)
|
39
|
+
mutator.value!(data, key, value)
|
40
|
+
end
|
41
|
+
|
42
42
|
def meta(key)
|
43
43
|
internal_meta[key.to_s.to_sym]
|
44
44
|
end
|
data/lib/hashcraft/version.rb
CHANGED
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.
|
4
|
+
version: 1.0.0.pre.alpha.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Ruggio
|
@@ -138,7 +138,10 @@ files:
|
|
138
138
|
- lib/hashcraft/generic/dictionary.rb
|
139
139
|
- lib/hashcraft/generic/registry.rb
|
140
140
|
- lib/hashcraft/mutator_registry.rb
|
141
|
+
- lib/hashcraft/mutators/always_false.rb
|
142
|
+
- lib/hashcraft/mutators/always_true.rb
|
141
143
|
- lib/hashcraft/mutators/array.rb
|
144
|
+
- lib/hashcraft/mutators/flat_array.rb
|
142
145
|
- lib/hashcraft/mutators/hash.rb
|
143
146
|
- lib/hashcraft/mutators/property.rb
|
144
147
|
- lib/hashcraft/option.rb
|