necromancer 0.3.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +70 -3
  3. data/README.md +204 -86
  4. data/lib/necromancer.rb +17 -18
  5. data/lib/necromancer/configuration.rb +1 -1
  6. data/lib/necromancer/context.rb +16 -3
  7. data/lib/necromancer/conversion_target.rb +31 -14
  8. data/lib/necromancer/conversions.rb +39 -16
  9. data/lib/necromancer/converter.rb +10 -8
  10. data/lib/necromancer/converters/array.rb +143 -45
  11. data/lib/necromancer/converters/boolean.rb +21 -19
  12. data/lib/necromancer/converters/date_time.rb +58 -13
  13. data/lib/necromancer/converters/hash.rb +119 -0
  14. data/lib/necromancer/converters/numeric.rb +32 -28
  15. data/lib/necromancer/converters/range.rb +44 -18
  16. data/lib/necromancer/null_converter.rb +4 -2
  17. data/lib/necromancer/version.rb +2 -2
  18. metadata +39 -72
  19. data/.gitignore +0 -14
  20. data/.rspec +0 -3
  21. data/.ruby-version +0 -1
  22. data/.travis.yml +0 -19
  23. data/Gemfile +0 -16
  24. data/Rakefile +0 -8
  25. data/necromancer.gemspec +0 -21
  26. data/spec/spec_helper.rb +0 -53
  27. data/spec/unit/can_spec.rb +0 -11
  28. data/spec/unit/config_spec.rb +0 -32
  29. data/spec/unit/configuration/new_spec.rb +0 -30
  30. data/spec/unit/conversions/register_spec.rb +0 -49
  31. data/spec/unit/convert_spec.rb +0 -104
  32. data/spec/unit/converters/array/array_to_boolean_spec.rb +0 -22
  33. data/spec/unit/converters/array/array_to_numeric_spec.rb +0 -22
  34. data/spec/unit/converters/array/array_to_set_spec.rb +0 -18
  35. data/spec/unit/converters/array/object_to_array_spec.rb +0 -21
  36. data/spec/unit/converters/array/string_to_array_spec.rb +0 -33
  37. data/spec/unit/converters/boolean/boolean_to_integer_spec.rb +0 -26
  38. data/spec/unit/converters/boolean/integer_to_boolean_spec.rb +0 -22
  39. data/spec/unit/converters/boolean/string_to_boolean_spec.rb +0 -36
  40. data/spec/unit/converters/date_time/string_to_date_spec.rb +0 -22
  41. data/spec/unit/converters/date_time/string_to_datetime_spec.rb +0 -32
  42. data/spec/unit/converters/numeric/string_to_float_spec.rb +0 -48
  43. data/spec/unit/converters/numeric/string_to_integer_spec.rb +0 -62
  44. data/spec/unit/converters/numeric/string_to_numeric_spec.rb +0 -32
  45. data/spec/unit/converters/range/string_to_range_spec.rb +0 -35
  46. data/spec/unit/new_spec.rb +0 -12
  47. data/spec/unit/register_spec.rb +0 -17
  48. data/tasks/console.rake +0 -10
  49. data/tasks/coverage.rake +0 -11
  50. data/tasks/spec.rake +0 -29
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 0ab93f1c0f77de5f9d0643fee2136e1a18448509
4
- data.tar.gz: 44e07300b65361d813dfa3e4d36381ef44cb0d8b
2
+ SHA256:
3
+ metadata.gz: ab3ba9bfe1c011b3ecf087642ff05a74b51939fa7fb881bbcdb7acec618e9d76
4
+ data.tar.gz: 4323c173f67da37e1eab170642aff2b8f179b8dc55f777b5424f487271bef105
5
5
  SHA512:
6
- metadata.gz: 9049c66ec36a5f139de3ffd32d14232eaf95118437c9323fec32cd34054722ea0106c7e1b59d7d284fb53ab52d14a1c3dc723cad07431f541f591200a84f167e
7
- data.tar.gz: 9471c9273853b50e269e9aa96efe7f3ae87d99236a7b222d8eb95fd03371a15485d7070fe903eba95949e524b68fec516d0746e05a9b1094c588d6029b52d9c2
6
+ metadata.gz: bbf17295bcc8268d93805f7b3e1fb6233358db0bdfb91bd5f7de644a5a7546e735e5f046430b1dbed3a85cffcba0dba3d38b107e12e5f4c366b89b71c8882d1e
7
+ data.tar.gz: 644eb159ae79c9a7f394b858ea2b367692959562979ad7e2c4befc45006f90ab014c3aa2e222469754f71687fee7ebd549dc6e2fe6ea5bc3439b11bf8bec9332
@@ -1,11 +1,78 @@
1
- 0.3.0 (December 14, 2014)
1
+ # Change log
2
2
 
3
+ ## [v0.7.0] - 2020-12-29
4
+
5
+ ### Added
6
+ * Add HashConverters for transforming string into hash of string, integer, float or boolean values
7
+ * Add converters for transforming string to array of booleans, integers, floats and numeric
8
+
9
+ ### Changed
10
+ * Change StringToRange converter to work with decimal numbers and spaces
11
+ * Change :strict to be a keyword argument
12
+ * Change StringToNumeric converter to allow numbers with space characters
13
+
14
+ ## [v0.6.0] - 2020-03-08
15
+
16
+ ### Changed
17
+ * Change gemspec to remove test artifacts
18
+
19
+ ## [v0.5.1] - 2019-11-24
20
+
21
+ ### Changed
22
+ * Change gemspec to include metadata
23
+
24
+ ### Fixed
25
+ * Fix Ruby 2.7 warnings by Ryan Davis(@zenspider)
26
+
27
+ ## [v0.5.0] - 2019-03-23
28
+
29
+ ### Changed
30
+ * Change to use Ruby >= 2.0.0
31
+ * Change to load files directly without git
32
+ * Change to raise on error in place of fail
33
+
34
+ ## [v0.4.0] - 2017-02-18
35
+
36
+ ### Added
37
+ * Add :string -> :time conversion
38
+ * Add inspection methods to Context and ConversionTarget
39
+ * Add module level Necromancer#convert for convenience and more functional style
40
+ * Add ConversionTarget#>> call for functional style converions
41
+
42
+ ### Changed
43
+ * Change fail to raise in ConversionTarget#for
44
+ * Change fail to raise in Conversions
45
+ * Change ConversionTarget#detect to handle Class type coercion
46
+
47
+ ### Fixed
48
+ * Fix bug with type detection
49
+ * Fix Ruby 2.4.0 warning about Fixnum & Bignum type
50
+
51
+ ## [v0.3.0] - 2014-12-14
52
+
53
+ ### Added
3
54
  * Add array converters for :hash, :set and :object conversions
4
55
  * Add ability to configure global conversion settings per instance
5
56
 
6
- 0.2.0 (December 7, 2014)
57
+ ## [v0.2.0] - 2014-12-07
7
58
 
59
+ ### Added
8
60
  * Add #fail_conversion_type to Converter and use in converters
9
61
  * Add DateTimeConverters
10
- * Change IntegerConverters & FloatConverters into Numeric Converters
11
62
  * Add string to numeric type conversion
63
+
64
+ ### Changed
65
+ * Change IntegerConverters & FloatConverters into Numeric Converters
66
+
67
+ ## [v0.1.0] - 2014-11-30
68
+
69
+ * Initial implementation and release
70
+
71
+ [v0.7.0]: https://github.com/piotrmurach/necromancer/compare/v0.6.0...v0.7.0
72
+ [v0.6.0]: https://github.com/piotrmurach/necromancer/compare/v0.5.1...v0.6.0
73
+ [v0.5.1]: https://github.com/piotrmurach/necromancer/compare/v0.5.0...v0.5.1
74
+ [v0.5.0]: https://github.com/piotrmurach/necromancer/compare/v0.4.0...v0.5.0
75
+ [v0.4.0]: https://github.com/piotrmurach/necromancer/compare/v0.3.0...v0.4.0
76
+ [v0.3.0]: https://github.com/piotrmurach/necromancer/compare/v0.2.0...v0.3.0
77
+ [v0.2.0]: https://github.com/piotrmurach/necromancer/compare/v0.1.0...v0.2.0
78
+ [v0.1.0]: https://github.com/piotrmurach/necromancer/compare/v0.1.0
data/README.md CHANGED
@@ -1,17 +1,22 @@
1
1
  # Necromancer
2
- [![Gem Version](https://badge.fury.io/rb/necromancer.png)][gem]
3
- [![Build Status](https://secure.travis-ci.org/peter-murach/necromancer.png?branch=master)][travis]
4
- [![Code Climate](https://codeclimate.com/github/peter-murach/necromancer.png)][codeclimate]
5
- [![Coverage Status](https://coveralls.io/repos/peter-murach/necromancer/badge.png)][coverage]
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/necromancer.svg)][gem]
4
+ [![Actions CI](https://github.com/piotrmurach/tty-runner/workflows/CI/badge.svg?branch=master)][gh_actions_ci]
5
+ [![Build status](https://ci.appveyor.com/api/projects/status/qj3xn5gbbfi4puet?svg=true)][appveyor]
6
+ [![Code Climate](https://codeclimate.com/github/piotrmurach/necromancer/badges/gpa.svg)][codeclimate]
7
+ [![Coverage Status](https://coveralls.io/repos/github/piotrmurach/necromancer/badge.svg?branch=master)][coverage]
8
+ [![Inline docs](http://inch-ci.org/github/piotrmurach/necromancer.svg?branch=master)][inchpages]
6
9
 
7
10
  [gem]: http://badge.fury.io/rb/necromancer
8
- [travis]: http://travis-ci.org/peter-murach/necromancer
9
- [codeclimate]: https://codeclimate.com/github/peter-murach/necromancer
10
- [coverage]: https://coveralls.io/r/peter-murach/necromancer
11
+ [gh_actions_ci]: https://github.com/piotrmurach/tty-runner/actions?query=workflow%3ACI
12
+ [appveyor]: https://ci.appveyor.com/project/piotrmurach/necromancer
13
+ [codeclimate]: https://codeclimate.com/github/piotrmurach/necromancer
14
+ [coverage]: https://coveralls.io/github/piotrmurach/necromancer
15
+ [inchpages]: http://inch-ci.org/github/piotrmurach/necromancer
11
16
 
12
17
  > Conversion from one object type to another with a bit of black magic.
13
18
 
14
- **Necromancer** provides independent type conversion component for [TTY](https://github.com/peter-murach/tty) toolkit.
19
+ **Necromancer** provides independent type conversion component for [TTY](https://github.com/piotrmurach/tty) toolkit.
15
20
 
16
21
  ## Motivation
17
22
 
@@ -30,7 +35,7 @@ Conversion between Ruby core types frequently comes up in projects but is solved
30
35
  Add this line to your application's Gemfile:
31
36
 
32
37
  ```ruby
33
- gem 'necromancer'
38
+ gem "necromancer"
34
39
  ```
35
40
 
36
41
  And then execute:
@@ -53,51 +58,67 @@ Or install it yourself as:
53
58
  * [3. Converters](#3-converters)
54
59
  * [3.1 Array](#31-array)
55
60
  * [3.2 Boolean](#32-boolean)
56
- * [3.3 Hash](#33-hash)
57
- * [3.4 Numeric](#34-numeric)
58
- * [3.5 Range](#35-range)
59
- * [3.6 Custom](#36-custom)
60
- * [3.6.1 Using an Object](#361-using-an-object)
61
- * [3.6.2 Using a Proc](#362-using-a-proc)
61
+ * [3.3 DateTime](#33-datetime)
62
+ * [3.4 Hash](#34-hash)
63
+ * [3.5 Numeric](#35-numeric)
64
+ * [3.6 Range](#36-range)
65
+ * [3.7 Custom](#37-custom)
66
+ * [3.7.1 Using an Object](#371-using-an-object)
67
+ * [3.7.2 Using a Proc](#372-using-a-proc)
62
68
 
63
69
  ## 1. Usage
64
70
 
65
- **Necromancer** requires you to instatiate it:
71
+ **Necromancer** knows how to handle conversions between various types using the `convert` method. The `convert` method takes as an argument the value to convert from. Then to perform actual coercion use the `to` or more functional style `>>` method that accepts the type for the returned value which can be `:symbol`, `object` or `ClassName`.
72
+
73
+ For example, to convert a string to a [range](#36-range) type:
66
74
 
67
75
  ```ruby
68
- converter = Necromancer.new
76
+ Necromancer.convert("1-10").to(:range) # => 1..10
77
+ Necromancer.convert("1-10") >> :range # => 1..10
78
+ Necromancer.convert("1-10") >> Range # => 1..10
69
79
  ```
70
80
 
71
- Once initialized **Necromancer** knows how to handle numerous conversions and also allows you to add your custom type [converters](#35-custom).
72
-
73
- For example, to convert a string to a [range](#34-range) type:
81
+ In order to handle [boolean](#32-boolean) conversions:
74
82
 
75
83
  ```ruby
76
- converter.convert('1-10').to(:range) # => 1..10
84
+ Necromancer.convert("t").to(:boolean) # => true
85
+ Necromancer.convert("t") >> true # => true
77
86
  ```
78
87
 
79
- In order to handle [boolean](#32-boolean) conversions:
88
+ To convert string to [numeric](#35-numeric) value:
80
89
 
81
90
  ```ruby
82
- converter.convert('t').to(:boolean) # => true
91
+ Necromancer.convert("10e1").to(:numeric) # => 100
83
92
  ```
84
93
 
85
- To convert string to [numeric](#34-numeric) value:
94
+ You can convert string to [array](#31-array) of values like `boolean`, `integer` or `float`:
86
95
 
87
96
  ```ruby
88
- converter.convert('10e1').to(:numeric) # => 100
97
+ Necromancer.convert("t,f,t"]).to(:booleans) # => [true, false, true]
98
+ Necromancer.convert("1,2.3,3.0"]).to(:integers) # => [1, 2, 3]
99
+ Necromancer.convert("1,2.3,3.0"]).to(:floats) # => [1.0, 2.3, 3.0]
89
100
  ```
90
101
 
91
- or get [array](#31-array) elements into numeric type:
102
+ To convert string to [hash](#34-hash) value:
92
103
 
93
104
  ```ruby
94
- converter.convert(['1', '2.3', '3.0']).to(:numeric) # => [1, 2.3, 3.0]
105
+ Necromancer.convert("a:1 b:2 c:3").to(:hash) # => {a: "1", b: "2", c: "3"}
106
+ Necromancer.convert("a=1 b=2 c=3").to(:hash) # => {a: "1", b: "2", c: "3"}
107
+ ````
108
+
109
+ To provide extra information about the conversion value type use the `from`:
110
+
111
+ ```ruby
112
+ Necromancer.convert(["1", "2.3", "3.0"]).from(:array).to(:numeric) # => [1, 2.3, 3.0]
95
113
  ```
96
114
 
97
- However, if you want to tell **Necromancer** about source type use `from`:
115
+ **Necromancer** also allows you to add [custom](#37-custom) conversions.
116
+
117
+ When conversion isn't possible, a `Necromancer::NoTypeConversionAvailableError` is thrown indicating that `convert` doesn't know how to perform the requested conversion:
98
118
 
99
119
  ```ruby
100
- converter.convert(['1', '2.3', '3.0']).from(:array).to(:numeric) # => [1, 2.3, 3.0]
120
+ Necromancer.convert(:foo).to(:float)
121
+ # => Necromancer::NoTypeConversionAvailableError: Conversion 'foo->float' unavailable.
101
122
  ```
102
123
 
103
124
  ## 2. Interface
@@ -109,13 +130,20 @@ converter.convert(['1', '2.3', '3.0']).from(:array).to(:numeric) # => [1, 2.3, 3
109
130
  For the purpose of divination, **Necromancer** uses `convert` method to turn source type into target type. For example, in order to convert a string into a range type do:
110
131
 
111
132
  ```ruby
112
- converter.convert('1,10').to(:range) # => 1..10
133
+ Necromancer.convert("1,10").to(:range) # => 1..10
113
134
  ```
114
135
 
115
136
  Alternatively, you can use block:
116
137
 
117
138
  ```ruby
118
- converter.convert { '1,10' }.to(:range) # => 1..10
139
+ Necromancer.convert { "1,10" }.to(:range) # => 1..10
140
+ ```
141
+
142
+ Conversion isn't always possible, in which case a `Necromancer::NoTypeConversionAvailableError` is thrown indicating that `convert` doesn't know how to perform the requested conversion:
143
+
144
+ ```ruby
145
+ Necromancer.convert(:foo).to(:float)
146
+ # => Necromancer::NoTypeConversionAvailableError: Conversion 'foo->float' unavailable.
119
147
  ```
120
148
 
121
149
  ### 2.2 from
@@ -123,45 +151,54 @@ converter.convert { '1,10' }.to(:range) # => 1..10
123
151
  To specify conversion source type use `from` method:
124
152
 
125
153
  ```ruby
126
- converter.convert('1.0').from(:string).to(:numeric)
154
+ Necromancer.convert("1.0").from(:string).to(:numeric)
127
155
  ```
128
156
 
129
157
  In majority of cases you do not need to specify `from` as the type will be inferred from the `convert` method argument and then appropriate conversion will be applied to result in `target` type such as `:numeric`. However, if you do not control the input to `convert` and want to ensure consistent behaviour please use `from`.
130
158
 
131
159
  The source parameters are:
132
160
 
133
- * :array
134
- * :boolean
135
- * :float
136
- * :integer
137
- * :numeric
138
- * :range
139
- * :string
161
+ * `:array`
162
+ * `:boolean`
163
+ * `:date`
164
+ * `:datetime`
165
+ * `:float`
166
+ * `:integer`
167
+ * `:numeric`
168
+ * `:range`
169
+ * `:string`
170
+ * `:time`
140
171
 
141
172
  ### 2.3 to
142
173
 
143
- To convert objects between types, **Necromancer** provides several target types. The `to` method allows you to pass target as an argument to perform actual conversion:
174
+ To convert objects between types, **Necromancer** provides several target types. The `to` or functional style `>>` method allows you to pass target as an argument to perform actual conversion. The target can be one of `:symbol`, `object` or `ClassName`:
144
175
 
145
176
  ```ruby
146
- converter.convert('yes').to(:boolean) # => true
177
+ Necromancer.convert("yes").to(:boolean) # => true
178
+ Necromancer.convert("yes") >> :boolean # => true
179
+ Necromancer.convert("yes") >> true # => true
180
+ Necromancer.convert("yes") >> TrueClass # => true
147
181
  ```
148
182
 
149
- By default, when target conversion fails the orignal value is returned. However, you can pass `strict` as an additional argument to ensure failure when conversion cannot be performed:
183
+ By default, when target conversion fails the original value is returned. However, you can pass `strict` as an additional argument to ensure failure when conversion cannot be performed:
150
184
 
151
185
  ```ruby
152
- converter.convert('1a').to(:integer, strict: true)
186
+ Necromancer.convert("1a").to(:integer, strict: true)
153
187
  # => raises Necromancer::ConversionTypeError
154
188
  ```
155
189
 
156
190
  The target parameters are:
157
191
 
158
- * :array
159
- * :boolean
160
- * :float
161
- * :integer
162
- * :numeric
163
- * :range
164
- * :string
192
+ * `:array`
193
+ * `:boolean`, `:booleans`, `:bools`, `:boolean_hash`, `:bool_hash`
194
+ * `:date`
195
+ * `:datetime`,
196
+ * `:float`, `:floats`, `:float_hash`
197
+ * `:integer`, `:integers`, `:ints`, `:integer_hash`, `:int_hash`
198
+ * `:numeric`, `:numerics`, `:nums`, `:numeric_hash`, `:num_hash`
199
+ * `:range`
200
+ * `:string`
201
+ * `:time`
165
202
 
166
203
  ### 2.4 can?
167
204
 
@@ -183,7 +220,7 @@ Necromancer.new do |config|
183
220
  end
184
221
  ```
185
222
 
186
- or calling `configure` method:
223
+ Or calling `configure` method:
187
224
 
188
225
  ```ruby
189
226
  converter = Necromancer.new
@@ -194,129 +231,204 @@ end
194
231
 
195
232
  Available configuration options are:
196
233
 
197
- * strict - ensures correct types for conversion, by default `false`
198
- * copy - ensures only copy is modified, by default `true`
234
+ * `strict` - ensures correct types for conversion, by default `false`
235
+ * `copy` - ensures only copy is modified, by default `true`
199
236
 
200
237
  ## 3. Converters
201
238
 
202
- **Necromancer** flexibility means you can register your own converters or use the already defined converters for such types as 'Array', 'Boolean', 'Hash', 'Numeric', 'Range'.
239
+ **Necromancer** flexibility means you can register your own converters or use the already defined converters for such types as `Array`, `Boolean`, `Date`, `DateTime`, `Hash`, `Numeric`, `Range` and `Time`.
203
240
 
204
241
  ### 3.1 Array
205
242
 
206
243
  The **Necromancer** allows you to transform arbitrary object into array:
207
244
 
208
245
  ```ruby
209
- converter.convert(nil).to(:array) # => []
210
- converter.convert({x: 1}).to(:array) # => [[:x, 1]]
246
+ Necromancer.convert(nil).to(:array) # => []
247
+ Necromancer.convert({x: 1}).to(:array) # => [[:x, 1]]
211
248
  ```
212
249
 
213
250
  In addition, **Necromancer** excels at converting `,` or `-` delimited string into an array object:
214
251
 
215
252
  ```ruby
216
- converter.convert('a, b, c').to(:array) # => ['a', 'b', 'c']
253
+ Necromancer.convert("a, b, c").to(:array) # => ["a", "b", "c"]
217
254
  ```
218
255
 
219
256
  If the string is a list of `-` or `,` separated numbers, they will be converted to their respective numeric types:
220
257
 
221
258
  ```ruby
222
- converter.convert('1 - 2 - 3').to(:array) # => [1, 2, 3]
259
+ Necromancer.convert("1 - 2 - 3").to(:array) # => [1, 2, 3]
260
+ ```
261
+
262
+ It handles conversion of string into an array of boolean values as well:
263
+
264
+ ```ruby
265
+ Necromancer.convert("yes,no,t").to(:booleans) # => [true, false, true]
266
+ Necromancer.convert("1 - f - FALSE").to(:bools) # => [true, false, false]
223
267
  ```
224
268
 
225
269
  You can also convert array containing string objects to array containing numeric values:
226
270
 
227
271
  ```ruby
228
- converter.convert(['1', '2.3', '3.0']).to(:numeric)
272
+ Necromancer.convert(["1", "2.3", "3.0"]).to(:numerics) # => [1, 2.3, 3.0]
273
+ Necromancer.convert(["1", "2.3", "3.0"]).to(:nums) # => [1, 2.3, 3.0]
274
+ ```
275
+
276
+ Or you can be more specific by using `:integers` and `:floats` as the resulting type:
277
+
278
+ ```ruby
279
+ Necromancer.convert(["1", "2.3", "3.0"]).to(:integers) # => [1, 2, 3]
229
280
  ```
230
281
 
231
282
  When in `strict` mode the conversion will raise a `Necromancer::ConversionTypeError` error like so:
232
283
 
233
284
  ```ruby
234
- converter.convert(['1', '2.3', false]).to(:numeric, strict: true)
235
- # => Necromancer::ConversionTypeError: false cannot be converted from `array` to `numeric`
285
+ Necromancer.convert(["1", "2.3", false]).to(:numerics, strict: true)
286
+ # => Necromancer::ConversionTypeError: false cannot be converted from `array` to `numerics`
236
287
  ```
237
288
 
238
289
  However, in `non-strict` mode the value will be simply returned unchanged:
239
290
 
240
291
  ```ruby
241
- converter.convert(['1', '2.3', false]).to(:numeric, strict: false)
292
+ Necromancer.convert(["1", "2.3", false]).to(:numerics, strict: false)
242
293
  # => [1, 2.3, false]
243
294
  ```
244
295
 
245
296
  ### 3.2 Boolean
246
297
 
247
- The **Necromancer** allows you to convert a string object to boolean object. The `1`, `'1'`, `'t'`, `'T'`, `'true'`, `'TRUE'`, `'y'`, `'Y'`, `'yes'`, `'Yes'`, `'on'`, `'ON'` values are converted to `TrueClass`.
298
+ The **Necromancer** allows you to convert a string object to boolean object. The `1`, `"1"`, `"t"`, `"T"`, `"true"`, `"TRUE"`, `"y"`, `"Y"`, `"yes"`, `"Yes"`, `"on"`, `"ON"` values are converted to `TrueClass`.
248
299
 
249
300
  ```ruby
250
- converter.convert('yes').to(:boolean) # => true
301
+ Necromancer.convert("yes").to(:boolean) # => true
251
302
  ```
252
303
 
253
- Similarly, the `0`, `'0'`, `'f'`, `'F'`, `'false'`, `'FALSE'`, `'n'`, `'N'`, `'no'`, `'No'`, `'off'`, `'OFF'` values are converted to `FalseClass`.
304
+ Similarly, the `0`, `"0"`, `"f"`, `"F"`, `"false"`, `"FALSE"`, `"n"`, `"N"`, `"no"`, `"No"`, `"off"`, `"OFF"` values are converted to `FalseClass`.
254
305
 
255
306
  ```ruby
256
- converter.convert('no').to(:boolean) # => false
307
+ Necromancer.convert("no").to(:boolean) # => false
257
308
  ```
258
309
 
259
310
  You can also convert an integer object to boolean:
260
311
 
261
312
  ```ruby
262
- converter.convert(1).to(:boolean) # => true
263
- converter.convert(0).to(:boolean) # => false
313
+ Necromancer.convert(1).to(:boolean) # => true
314
+ Necromancer.convert(0).to(:boolean) # => false
315
+ ```
316
+
317
+ ### 3.3 DateTime
318
+
319
+ **Necromancer** knows how to convert string to `date` object:
320
+
321
+ ```ruby
322
+ Necromancer.convert("1-1-2015").to(:date) # => "2015-01-01"
323
+ Necromancer.convert("01/01/2015").to(:date) # => "2015-01-01"
324
+ ```
325
+
326
+ You can also convert string to `datetime`:
327
+
328
+ ```ruby
329
+ Necromancer.convert("1-1-2015").to(:datetime) # => "2015-01-01T00:00:00+00:00"
330
+ Necromancer.convert("1-1-2015 15:12:44").to(:datetime) # => "2015-01-01T15:12:44+00:00"
331
+ ```
332
+
333
+ To convert a string to a time instance do:
334
+
335
+ ```ruby
336
+ Necromancer.convert("01-01-2015").to(:time) # => 2015-01-01 00:00:00 +0100
337
+ Necromancer.convert("01-01-2015 08:35").to(:time) # => 2015-01-01 08:35:00 +0100
338
+ Necromancer.convert("12:35").to(:time) # => 2015-01-04 12:35:00 +0100
339
+ ```
340
+
341
+ ### 3.4 Hash
342
+
343
+ With **Necromancer** you can convert a string with pairs delimited by `=` or `:` characters into a hash:
344
+
345
+ ```ruby
346
+ Necromancer.convert("a:1 b:2 c:3").to(:hash)
347
+ Necromancer.convert("a=1 b=2 c=3").to(:hash)
348
+ # => {a: "1", b: "2", c: "3"}
349
+ ```
350
+
351
+ The pairs can be separated by `&` symbols and mix `=` and `:` pair delimiters:
352
+
353
+ ```ruby
354
+ Necromancer.convert("a:1 & b=2 & c:3").to(:hash)
355
+ # => {a: "1", b: "2", c: "3"}
264
356
  ```
265
357
 
266
- ### 3.3 Hash
358
+ You can also convert string to hash with integer values using `:int_hash` type:
267
359
 
268
360
  ```ruby
269
- converter.convert({ x: '27.5', y: '4', z: '11'}).to(:numeric)
270
- # => { x: 27.5, y: 4, z: 11}
361
+ Necromancer.convert("a:1 b:2 c:3").to(:int_hash) # => {a: 1, b: 2, c: 3}
362
+ Necromancer.convert("a:1 b:2 c:3").to(:integer_hash) # => {a: 1, b: 2, c: 3}
271
363
  ```
272
364
 
273
- ### 3.4 Numeric
365
+ Similarly you can convert string to hash with `float` or `numeric` values using `:float_hash` and `numeric_hash` types:
366
+
367
+ ```ruby
368
+ Necromancer.convert("a:1 b:2 c:3").to(:float_hash) # => {a: 1.0, b: 2.0, c: 3.0}
369
+ Necromancer.convert("a:1 b:2.0 c:3").to(:num_hash) # => {a: 1, b:2.0, c: 3}
370
+ ```
371
+
372
+ String can also be converted to hash with boolean values using `:boolean_hash` or `:bool_hash`:
373
+
374
+ ```ruby
375
+ Necromancer.convert("a:yes b:no c:t").to(:bool_hash) # => {a: true, b: false, c: true}
376
+ ```
377
+
378
+ ### 3.5 Numeric
274
379
 
275
380
  **Necromancer** comes ready to convert all the primitive numeric values.
276
381
 
277
382
  To convert a string to a float do:
278
383
 
279
384
  ```ruby
280
- converter.convert('1.2a').to(:float) # => 1.2
385
+ Necromancer.convert("1.2a").to(:float) # => 1.2
281
386
  ```
282
387
 
283
388
  Conversion to numeric in strict mode raises `Necromancer::ConversionTypeError`:
284
389
 
285
390
  ```ruby
286
- converter.convert('1.2a').to(:float, strict: true) # => raises error
391
+ Necromancer.convert("1.2a").to(:float, strict: true) # => raises error
287
392
  ```
288
393
 
289
394
  To convert a string to an integer do:
290
395
 
291
396
  ```ruby
292
- converter.convert('1a').to(:integer) # => 1
397
+ Necromancer.convert("1a").to(:integer) # => 1
293
398
  ```
294
399
 
295
400
  However, if you want to convert string to an appropriate matching numeric type do:
296
401
 
297
402
  ```ruby
298
- converter.convert('1e1').to(:numeric) # => 10
403
+ Necromancer.convert("1e1").to(:numeric) # => 10
299
404
  ```
300
405
 
301
- ### 3.5 Range
406
+ ### 3.6 Range
302
407
 
303
408
  **Necromancer** is no stranger to figuring out ranges from strings. You can pass `,`, `-`, `..`, `...` characters to denote ranges:
304
409
 
305
410
  ```ruby
306
- converter.convert('1,10').to(:range) # => 1..10
411
+ Necromancer.convert("1,10").to(:range) # => 1..10
307
412
  ```
308
413
 
309
- or to create a range of letters:
414
+ Or to create a range of letters:
310
415
 
311
416
  ```ruby
312
- converter.convert('a-z').to(:range) # => 'a'..'z'
417
+ Necromancer.convert("a-z").to(:range) # => "a".."z"
313
418
  ```
314
419
 
315
- ### 3.6 Custom
420
+ It will handle space characters:
421
+
422
+ ```ruby
423
+ Necromancer.convert("1 . . 10") >> :range # => 1..10
424
+ Necromancer.convert("a . . . z") >> :range # => "a"..."z"
425
+ ````
426
+
427
+ ### 3.7 Custom
316
428
 
317
429
  In case where provided conversions do not match your needs you can create your own and `register` with **Necromancer** by using an `Object` or a `Proc`.
318
430
 
319
- #### 3.6.1 Using an Object
431
+ #### 3.7.1 Using an Object
320
432
 
321
433
  Firstly, you need to create a converter that at minimum requires to specify `call` method that will be invoked during conversion:
322
434
 
@@ -346,10 +458,10 @@ converter.register(upcase_converter) # => true if successfully registered
346
458
  Finally, by invoking `convert` method and specifying `:upcase` as the target for the conversion we achieve the result:
347
459
 
348
460
  ```ruby
349
- converter.convert('magic').to(:upcase) # => 'MAGIC'
461
+ converter.convert("magic").to(:upcase) # => "MAGIC"
350
462
  ```
351
463
 
352
- #### 3.6.2 Using a Proc
464
+ #### 3.7.2 Using a Proc
353
465
 
354
466
  Using a Proc object you can create and immediately register a converter. You need to pass `source` and `target` of the conversion that will be used later on to match the conversion. The `convert` allows you to specify the actual conversion in block form. For example:
355
467
 
@@ -366,17 +478,23 @@ end
366
478
  Then by invoking the `convert` method and passing the `:upcase` conversion type you can transform the string like so:
367
479
 
368
480
  ```ruby
369
- converter.convert('magic').to(:upcase) # => 'MAGIC'
481
+ converter.convert("magic").to(:upcase) # => "MAGIC"
370
482
  ```
371
483
 
372
484
  ## Contributing
373
485
 
374
- 1. Fork it ( https://github.com/peter-murach/necromancer/fork )
486
+ Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/necromancer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
487
+
488
+ 1. Fork it ( https://github.com/piotrmurach/necromancer/fork )
375
489
  2. Create your feature branch (`git checkout -b my-new-feature`)
376
490
  3. Commit your changes (`git commit -am 'Add some feature'`)
377
491
  4. Push to the branch (`git push origin my-new-feature`)
378
492
  5. Create a new Pull Request
379
493
 
494
+ ## License
495
+
496
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
497
+
380
498
  ## Copyright
381
499
 
382
500
  Copyright (c) 2014 Piotr Murach. See LICENSE for further details.