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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: de90f4a4235178d5dd03c534a7b371583825795ea40b0218f678e5be516dbaba
4
- data.tar.gz: 0227f29c0dc69de054f58abd31f854fe4f2a9fed7ad4ba60c439d8ca76d09cbe
3
+ metadata.gz: d9c8a4af5a05076883863320714728589bf2b49a0bd3b7f78e59be638697f6c9
4
+ data.tar.gz: 076117b9ba1523dc28f1eb62ce0cf19633d5eefc45db338e3633a325e5884234
5
5
  SHA512:
6
- metadata.gz: 1d1376245cf1dbf76948e6241965287776c169cf3d2c5e2879e396be0697f3bac9eb25539c3b8198997f4a5f2b2a74a9dfa9943229cf7f2183eeb196bb4cfeaa
7
- data.tar.gz: 4da6b340943dd7241ed820853892808664aea71ed7ea45047764aaf1cc63e6b703129d9ab446fcac49b2b8417ff6378d2142fb5329a6c855211bfdffab1f1c75
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 the data backing the method, defaulting to property. When the default property is used then it will simply assign the passed in value. Other values 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.
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 (Hashable::Transformers::PassThru) but can be explicitly passed any object that responds to `#transform(value, option)`.
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 (using the above Grid example):
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(key_transformer: CamelCase.new)
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(key_transformer: :camel_case)
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 as such:
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 (Localizer) when deriving hashes (using the above Grid and updated Column example):
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(value_transformer: Localizer.new)
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 arry then this mutator can be used to push a new value on the
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
@@ -15,9 +15,7 @@ module Hashcraft
15
15
  include Singleton
16
16
 
17
17
  def value!(data, key, value)
18
- data[key] = value
19
-
20
- self
18
+ tap { data[key] = value }
21
19
  end
22
20
  end
23
21
  end
@@ -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
@@ -8,5 +8,5 @@
8
8
  #
9
9
 
10
10
  module Hashcraft
11
- VERSION = '1.0.0-alpha.4'
11
+ VERSION = '1.0.0-alpha.5'
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.4
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