necromancer 0.3.0 → 0.4.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.
- checksums.yaml +4 -4
- data/.travis.yml +14 -6
- data/CHANGELOG.md +35 -3
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +3 -4
- data/README.md +116 -61
- data/lib/necromancer.rb +16 -18
- data/lib/necromancer/context.rb +14 -1
- data/lib/necromancer/conversion_target.rb +27 -10
- data/lib/necromancer/conversions.rb +33 -13
- data/lib/necromancer/converter.rb +3 -1
- data/lib/necromancer/converters/array.rb +5 -1
- data/lib/necromancer/converters/boolean.rb +4 -1
- data/lib/necromancer/converters/date_time.rb +45 -1
- data/lib/necromancer/converters/numeric.rb +4 -1
- data/lib/necromancer/converters/range.rb +4 -1
- data/lib/necromancer/null_converter.rb +3 -1
- data/lib/necromancer/version.rb +2 -2
- data/necromancer.gemspec +3 -1
- data/spec/unit/can_spec.rb +1 -3
- data/spec/unit/config_spec.rb +1 -3
- data/spec/unit/configuration/new_spec.rb +1 -3
- data/spec/unit/conversions/fetch_spec.rb +16 -0
- data/spec/unit/conversions/register_spec.rb +12 -3
- data/spec/unit/conversions/to_hash_spec.rb +37 -0
- data/spec/unit/convert_spec.rb +29 -3
- data/spec/unit/converters/array/array_to_boolean_spec.rb +1 -3
- data/spec/unit/converters/array/array_to_numeric_spec.rb +1 -3
- data/spec/unit/converters/array/array_to_set_spec.rb +1 -3
- data/spec/unit/converters/array/object_to_array_spec.rb +1 -3
- data/spec/unit/converters/array/string_to_array_spec.rb +1 -3
- data/spec/unit/converters/boolean/boolean_to_integer_spec.rb +1 -3
- data/spec/unit/converters/boolean/integer_to_boolean_spec.rb +1 -3
- data/spec/unit/converters/boolean/string_to_boolean_spec.rb +1 -3
- data/spec/unit/converters/date_time/string_to_date_spec.rb +5 -3
- data/spec/unit/converters/date_time/string_to_datetime_spec.rb +1 -3
- data/spec/unit/converters/date_time/string_to_time_spec.rb +28 -0
- data/spec/unit/converters/numeric/string_to_float_spec.rb +1 -3
- data/spec/unit/converters/numeric/string_to_integer_spec.rb +1 -3
- data/spec/unit/converters/numeric/string_to_numeric_spec.rb +1 -3
- data/spec/unit/converters/range/string_to_range_spec.rb +1 -3
- data/spec/unit/inspect_spec.rb +14 -0
- data/spec/unit/new_spec.rb +1 -3
- data/spec/unit/register_spec.rb +1 -3
- metadata +48 -12
- data/.ruby-version +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8af7250cb5f7455175e816a1544b6ccbc5f9ea6f
|
4
|
+
data.tar.gz: 94ad6d982fbec3a81464cf916dd7cb08204bddc7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f7d99a2ec16d62bd24432d632bd93953065c010e78382024a101bfabc2d11adeadf134ad4586fb88f14aa9173b91721b62849a3f730ab2741923091d93ebfff
|
7
|
+
data.tar.gz: b306b1185bced349369a93672ae55aa6099fac218770698b4a46f1c24d458919031459627b95e28c95a903f1d0bb323c61574e4a8cbddbd4e7ac10ae9c828533
|
data/.travis.yml
CHANGED
@@ -1,19 +1,27 @@
|
|
1
|
+
---
|
2
|
+
language: ruby
|
3
|
+
sudo: false
|
4
|
+
cache: bundler
|
1
5
|
language: ruby
|
2
|
-
bundler_args: --without yard benchmarks
|
3
6
|
script: "bundle exec rake ci"
|
4
7
|
rvm:
|
5
8
|
- 1.9.3
|
6
9
|
- 2.0.0
|
7
|
-
- 2.1.
|
10
|
+
- 2.1.10
|
11
|
+
- 2.2.6
|
12
|
+
- 2.3.3
|
13
|
+
- 2.4.0
|
8
14
|
- ruby-head
|
9
|
-
- jruby-
|
10
|
-
-
|
15
|
+
- jruby-9000
|
16
|
+
- jruby-head
|
17
|
+
- rbx-3
|
11
18
|
matrix:
|
12
|
-
include:
|
13
|
-
- rvm: jruby-head
|
14
19
|
allow_failures:
|
15
20
|
- rvm: ruby-head
|
16
21
|
- rvm: jruby-head
|
22
|
+
- rvm: rbx-3
|
17
23
|
fast_finish: true
|
18
24
|
branches:
|
19
25
|
only: master
|
26
|
+
notifications:
|
27
|
+
email: false
|
data/CHANGELOG.md
CHANGED
@@ -1,11 +1,43 @@
|
|
1
|
-
|
1
|
+
# Change log
|
2
2
|
|
3
|
+
## [v0.4.0] - 2017-02-18
|
4
|
+
|
5
|
+
### Added
|
6
|
+
* Add :string -> :time conversion
|
7
|
+
* Add inspection methods to Context and ConversionTarget
|
8
|
+
* Add module level Necromancer#convert for convenience and more functional style
|
9
|
+
* Add ConversionTarget#>> call for functional style converions
|
10
|
+
|
11
|
+
### Changed
|
12
|
+
* Change fail to raise in ConversionTarget#for
|
13
|
+
* Change fail to raise in Conversions
|
14
|
+
* Change ConversionTarget#detect to handle Class type coercion
|
15
|
+
|
16
|
+
### Fixed
|
17
|
+
* Fix bug with type detection
|
18
|
+
* Fix Ruby 2.4.0 warning about Fixnum & Bignum type
|
19
|
+
|
20
|
+
## [v0.3.0] - 2014-12-14
|
21
|
+
|
22
|
+
### Added
|
3
23
|
* Add array converters for :hash, :set and :object conversions
|
4
24
|
* Add ability to configure global conversion settings per instance
|
5
25
|
|
6
|
-
|
26
|
+
## [v0.2.0] - 2014-12-07
|
7
27
|
|
28
|
+
### Added
|
8
29
|
* Add #fail_conversion_type to Converter and use in converters
|
9
30
|
* Add DateTimeConverters
|
10
|
-
* Change IntegerConverters & FloatConverters into Numeric Converters
|
11
31
|
* Add string to numeric type conversion
|
32
|
+
|
33
|
+
### Changed
|
34
|
+
* Change IntegerConverters & FloatConverters into Numeric Converters
|
35
|
+
|
36
|
+
## [v0.1.0] - 2014-11-30
|
37
|
+
|
38
|
+
* Initial implementation and release
|
39
|
+
|
40
|
+
[v0.4.0]: https://github.com/piotrmurach/necromancer/compare/v0.3.0...v0.4.0
|
41
|
+
[v0.3.0]: https://github.com/piotrmurach/necromancer/compare/v0.2.0...v0.3.0
|
42
|
+
[v0.2.0]: https://github.com/piotrmurach/necromancer/compare/v0.1.0...v0.2.0
|
43
|
+
[v0.1.0]: https://github.com/piotrmurach/necromancer/compare/v0.1.0
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at [email]. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
CHANGED
@@ -3,14 +3,13 @@ source 'https://rubygems.org'
|
|
3
3
|
gemspec
|
4
4
|
|
5
5
|
group :development do
|
6
|
-
gem 'rake', '~> 10.3.2'
|
7
|
-
gem 'rspec', '~> 3.1.0'
|
8
6
|
gem 'yard', '~> 0.8.7'
|
9
7
|
gem 'benchmark-ips', '~> 2.0.0'
|
10
8
|
end
|
11
9
|
|
12
10
|
group :metrics do
|
13
|
-
gem 'coveralls', '~> 0.
|
14
|
-
gem 'simplecov', '~> 0.
|
11
|
+
gem 'coveralls', '~> 0.8.2'
|
12
|
+
gem 'simplecov', '~> 0.10.0'
|
15
13
|
gem 'yardstick', '~> 0.9.9'
|
14
|
+
gem 'term-ansicolor', '=1.3.2'
|
16
15
|
end
|
data/README.md
CHANGED
@@ -1,17 +1,19 @@
|
|
1
1
|
# Necromancer
|
2
|
-
[][gem]
|
3
|
+
[][travis]
|
4
|
+
[][codeclimate]
|
5
|
+
[][coverage]
|
6
|
+
[][inchpages]
|
6
7
|
|
7
8
|
[gem]: http://badge.fury.io/rb/necromancer
|
8
|
-
[travis]: http://travis-ci.org/
|
9
|
-
[codeclimate]: https://codeclimate.com/github/
|
10
|
-
[coverage]: https://coveralls.io/
|
9
|
+
[travis]: http://travis-ci.org/piotrmurach/necromancer
|
10
|
+
[codeclimate]: https://codeclimate.com/github/piotrmurach/necromancer
|
11
|
+
[coverage]: https://coveralls.io/github/piotrmurach/necromancer
|
12
|
+
[inchpages]: http://inch-ci.org/github/piotrmurach/necromancer
|
11
13
|
|
12
14
|
> Conversion from one object type to another with a bit of black magic.
|
13
15
|
|
14
|
-
**Necromancer** provides independent type conversion component for [TTY](https://github.com/
|
16
|
+
**Necromancer** provides independent type conversion component for [TTY](https://github.com/piotrmurach/tty) toolkit.
|
15
17
|
|
16
18
|
## Motivation
|
17
19
|
|
@@ -53,51 +55,58 @@ Or install it yourself as:
|
|
53
55
|
* [3. Converters](#3-converters)
|
54
56
|
* [3.1 Array](#31-array)
|
55
57
|
* [3.2 Boolean](#32-boolean)
|
56
|
-
* [3.3
|
57
|
-
* [3.4
|
58
|
-
* [3.5
|
59
|
-
* [3.6
|
60
|
-
|
61
|
-
* [3.
|
58
|
+
* [3.3 DateTime](#33-datetime)
|
59
|
+
* [3.4 Hash](#34-hash)
|
60
|
+
* [3.5 Numeric](#35-numeric)
|
61
|
+
* [3.6 Range](#36-range)
|
62
|
+
* [3.7 Custom](#37-custom)
|
63
|
+
* [3.7.1 Using an Object](#371-using-an-object)
|
64
|
+
* [3.7.2 Using a Proc](#372-using-a-proc)
|
62
65
|
|
63
66
|
## 1. Usage
|
64
67
|
|
65
|
-
**Necromancer**
|
68
|
+
**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`.
|
69
|
+
|
70
|
+
For example, to convert a string to a [range](#36-range) type:
|
66
71
|
|
67
72
|
```ruby
|
68
|
-
|
73
|
+
Necromancer.convert('1-10').to(:range) # => 1..10
|
74
|
+
Necromancer.convert('1-10') >> :range # => 1..10
|
75
|
+
Necromancer.convert('1-10') >> Range # => 1..10
|
69
76
|
```
|
70
77
|
|
71
|
-
|
72
|
-
|
73
|
-
For example, to convert a string to a [range](#34-range) type:
|
78
|
+
In order to handle [boolean](#32-boolean) conversions:
|
74
79
|
|
75
80
|
```ruby
|
76
|
-
|
81
|
+
Necromancer.convert('t').to(:boolean) # => true
|
82
|
+
Necromancer.convert('t') >> true # => true
|
77
83
|
```
|
78
84
|
|
79
|
-
|
85
|
+
To convert string to [numeric](#35-numeric) value:
|
80
86
|
|
81
87
|
```ruby
|
82
|
-
|
88
|
+
Necromancer.convert('10e1').to(:numeric) # => 100
|
83
89
|
```
|
84
90
|
|
85
|
-
|
91
|
+
or convert [array](#31-array) of string values into numeric type:
|
86
92
|
|
87
93
|
```ruby
|
88
|
-
|
94
|
+
Necromancer.convert(['1', '2.3', '3.0']).to(:numeric) # => [1, 2.3, 3.0]
|
89
95
|
```
|
90
96
|
|
91
|
-
|
97
|
+
To provide extra information about the conversion value type you can use `from`.
|
92
98
|
|
93
99
|
```ruby
|
94
|
-
|
100
|
+
Necromancer.convert(['1', '2.3', '3.0']).from(:array).to(:numeric) # => [1, 2.3, 3.0]
|
95
101
|
```
|
96
102
|
|
97
|
-
|
103
|
+
**Necromancer** also allows you to add [custom](#37-custom) conversions.
|
104
|
+
|
105
|
+
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:
|
98
106
|
|
99
107
|
```ruby
|
100
|
-
|
108
|
+
Necromancer.convert(:foo).to(:float)
|
109
|
+
# => Necromancer::NoTypeConversionAvailableError: Conversion 'foo->float' unavailable.
|
101
110
|
```
|
102
111
|
|
103
112
|
## 2. Interface
|
@@ -109,13 +118,20 @@ converter.convert(['1', '2.3', '3.0']).from(:array).to(:numeric) # => [1, 2.3, 3
|
|
109
118
|
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
119
|
|
111
120
|
```ruby
|
112
|
-
|
121
|
+
Necromancer.convert('1,10').to(:range) # => 1..10
|
113
122
|
```
|
114
123
|
|
115
124
|
Alternatively, you can use block:
|
116
125
|
|
117
126
|
```ruby
|
118
|
-
|
127
|
+
Necromancer.convert { '1,10' }.to(:range) # => 1..10
|
128
|
+
```
|
129
|
+
|
130
|
+
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:
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
Necromancer.convert(:foo).to(:float)
|
134
|
+
# => Necromancer::NoTypeConversionAvailableError: Conversion 'foo->float' unavailable.
|
119
135
|
```
|
120
136
|
|
121
137
|
### 2.2 from
|
@@ -123,7 +139,7 @@ converter.convert { '1,10' }.to(:range) # => 1..10
|
|
123
139
|
To specify conversion source type use `from` method:
|
124
140
|
|
125
141
|
```ruby
|
126
|
-
|
142
|
+
Necromancer.convert('1.0').from(:string).to(:numeric)
|
127
143
|
```
|
128
144
|
|
129
145
|
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`.
|
@@ -132,24 +148,30 @@ The source parameters are:
|
|
132
148
|
|
133
149
|
* :array
|
134
150
|
* :boolean
|
151
|
+
* :date
|
152
|
+
* :datetime
|
135
153
|
* :float
|
136
154
|
* :integer
|
137
155
|
* :numeric
|
138
156
|
* :range
|
139
157
|
* :string
|
158
|
+
* :time
|
140
159
|
|
141
160
|
### 2.3 to
|
142
161
|
|
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
|
162
|
+
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
163
|
|
145
164
|
```ruby
|
146
|
-
|
165
|
+
Necromancer.convert('yes').to(:boolean) # => true
|
166
|
+
Necromancer.convert('yes') >> :boolean # => true
|
167
|
+
Necromancer.convert('yes') >> true # => true
|
168
|
+
Necromancer.convert('yes') >> TrueClass # => true
|
147
169
|
```
|
148
170
|
|
149
171
|
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:
|
150
172
|
|
151
173
|
```ruby
|
152
|
-
|
174
|
+
Necromancer.convert('1a').to(:integer, strict: true)
|
153
175
|
# => raises Necromancer::ConversionTypeError
|
154
176
|
```
|
155
177
|
|
@@ -157,11 +179,14 @@ The target parameters are:
|
|
157
179
|
|
158
180
|
* :array
|
159
181
|
* :boolean
|
182
|
+
* :date
|
183
|
+
* :datetime
|
160
184
|
* :float
|
161
185
|
* :integer
|
162
186
|
* :numeric
|
163
187
|
* :range
|
164
188
|
* :string
|
189
|
+
* :time
|
165
190
|
|
166
191
|
### 2.4 can?
|
167
192
|
|
@@ -199,46 +224,46 @@ Available configuration options are:
|
|
199
224
|
|
200
225
|
## 3. Converters
|
201
226
|
|
202
|
-
**Necromancer** flexibility means you can register your own converters or use the already defined converters for such types as
|
227
|
+
**Necromancer** flexibility means you can register your own converters or use the already defined converters for such types as `Array`, `Boolean`, `Hash`, `Numeric`, `Range`.
|
203
228
|
|
204
229
|
### 3.1 Array
|
205
230
|
|
206
231
|
The **Necromancer** allows you to transform arbitrary object into array:
|
207
232
|
|
208
233
|
```ruby
|
209
|
-
|
210
|
-
|
234
|
+
Necromancer.convert(nil).to(:array) # => []
|
235
|
+
Necromancer.convert({x: 1}).to(:array) # => [[:x, 1]]
|
211
236
|
```
|
212
237
|
|
213
238
|
In addition, **Necromancer** excels at converting `,` or `-` delimited string into an array object:
|
214
239
|
|
215
240
|
```ruby
|
216
|
-
|
241
|
+
Necromancer.convert('a, b, c').to(:array) # => ['a', 'b', 'c']
|
217
242
|
```
|
218
243
|
|
219
244
|
If the string is a list of `-` or `,` separated numbers, they will be converted to their respective numeric types:
|
220
245
|
|
221
246
|
```ruby
|
222
|
-
|
247
|
+
Necromancer.convert('1 - 2 - 3').to(:array) # => [1, 2, 3]
|
223
248
|
```
|
224
249
|
|
225
250
|
You can also convert array containing string objects to array containing numeric values:
|
226
251
|
|
227
252
|
```ruby
|
228
|
-
|
253
|
+
Necromancer.convert(['1', '2.3', '3.0']).to(:numeric)
|
229
254
|
```
|
230
255
|
|
231
256
|
When in `strict` mode the conversion will raise a `Necromancer::ConversionTypeError` error like so:
|
232
257
|
|
233
258
|
```ruby
|
234
|
-
|
259
|
+
Necromancer.convert(['1', '2.3', false]).to(:numeric, strict: true)
|
235
260
|
# => Necromancer::ConversionTypeError: false cannot be converted from `array` to `numeric`
|
236
261
|
```
|
237
262
|
|
238
263
|
However, in `non-strict` mode the value will be simply returned unchanged:
|
239
264
|
|
240
265
|
```ruby
|
241
|
-
|
266
|
+
Necromancer.convert(['1', '2.3', false]).to(:numeric, strict: false)
|
242
267
|
# => [1, 2.3, false]
|
243
268
|
```
|
244
269
|
|
@@ -247,76 +272,100 @@ converter.convert(['1', '2.3', false]).to(:numeric, strict: false)
|
|
247
272
|
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
273
|
|
249
274
|
```ruby
|
250
|
-
|
275
|
+
Necromancer.convert('yes').to(:boolean) # => true
|
251
276
|
```
|
252
277
|
|
253
278
|
Similarly, the `0`, `'0'`, `'f'`, `'F'`, `'false'`, `'FALSE'`, `'n'`, `'N'`, `'no'`, `'No'`, `'off'`, `'OFF'` values are converted to `FalseClass`.
|
254
279
|
|
255
280
|
```ruby
|
256
|
-
|
281
|
+
Necromancer.convert('no').to(:boolean) # => false
|
257
282
|
```
|
258
283
|
|
259
284
|
You can also convert an integer object to boolean:
|
260
285
|
|
261
286
|
```ruby
|
262
|
-
|
263
|
-
|
287
|
+
Necromancer.convert(1).to(:boolean) # => true
|
288
|
+
Necromancer.convert(0).to(:boolean) # => false
|
289
|
+
```
|
290
|
+
|
291
|
+
### 3.3 DateTime
|
292
|
+
|
293
|
+
**Necromancer** knows how to convert string to `date` object:
|
294
|
+
|
295
|
+
```ruby
|
296
|
+
Necromancer.convert('1-1-2015').to(:date) # => "2015-01-01"
|
297
|
+
Necromancer.convert('01/01/2015').to(:date) # => "2015-01-01"
|
264
298
|
```
|
265
299
|
|
266
|
-
|
300
|
+
You can also convert string to `datetime`:
|
267
301
|
|
268
302
|
```ruby
|
269
|
-
|
303
|
+
Necromancer.convert("1-1-2015").to(:datetime) # => "2015-01-01T00:00:00+00:00"
|
304
|
+
Necromancer.convert("1-1-2015 15:12:44").to(:datetime) # => "2015-01-01T15:12:44+00:00"
|
305
|
+
```
|
306
|
+
|
307
|
+
To convert a string to a time instance do:
|
308
|
+
|
309
|
+
```ruby
|
310
|
+
Necromancer.convert("01-01-2015").to(:time) # => 2015-01-01 00:00:00 +0100
|
311
|
+
Necromancer.convert("01-01-2015 08:35").to(:time) # => 2015-01-01 08:35:00 +0100
|
312
|
+
Necromancer.convert("12:35").to(:time) # => 2015-01-04 12:35:00 +0100
|
313
|
+
```
|
314
|
+
|
315
|
+
### 3.4 Hash
|
316
|
+
|
317
|
+
```ruby
|
318
|
+
Necromancer.convert({ x: '27.5', y: '4', z: '11'}).to(:numeric)
|
270
319
|
# => { x: 27.5, y: 4, z: 11}
|
271
320
|
```
|
272
321
|
|
273
|
-
### 3.
|
322
|
+
### 3.5 Numeric
|
274
323
|
|
275
324
|
**Necromancer** comes ready to convert all the primitive numeric values.
|
276
325
|
|
277
326
|
To convert a string to a float do:
|
278
327
|
|
279
328
|
```ruby
|
280
|
-
|
329
|
+
Necromancer.convert('1.2a').to(:float) # => 1.2
|
281
330
|
```
|
282
331
|
|
283
332
|
Conversion to numeric in strict mode raises `Necromancer::ConversionTypeError`:
|
284
333
|
|
285
334
|
```ruby
|
286
|
-
|
335
|
+
Necromancer.convert('1.2a').to(:float, strict: true) # => raises error
|
287
336
|
```
|
288
337
|
|
289
338
|
To convert a string to an integer do:
|
290
339
|
|
291
340
|
```ruby
|
292
|
-
|
341
|
+
Necromancer.convert('1a').to(:integer) # => 1
|
293
342
|
```
|
294
343
|
|
295
344
|
However, if you want to convert string to an appropriate matching numeric type do:
|
296
345
|
|
297
346
|
```ruby
|
298
|
-
|
347
|
+
Necromancer.convert('1e1').to(:numeric) # => 10
|
299
348
|
```
|
300
349
|
|
301
|
-
### 3.
|
350
|
+
### 3.6 Range
|
302
351
|
|
303
352
|
**Necromancer** is no stranger to figuring out ranges from strings. You can pass `,`, `-`, `..`, `...` characters to denote ranges:
|
304
353
|
|
305
354
|
```ruby
|
306
|
-
|
355
|
+
Necromancer.convert('1,10').to(:range) # => 1..10
|
307
356
|
```
|
308
357
|
|
309
358
|
or to create a range of letters:
|
310
359
|
|
311
360
|
```ruby
|
312
|
-
|
361
|
+
Necromancer.convert('a-z').to(:range) # => 'a'..'z'
|
313
362
|
```
|
314
363
|
|
315
|
-
### 3.
|
364
|
+
### 3.7 Custom
|
316
365
|
|
317
366
|
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
367
|
|
319
|
-
#### 3.
|
368
|
+
#### 3.7.1 Using an Object
|
320
369
|
|
321
370
|
Firstly, you need to create a converter that at minimum requires to specify `call` method that will be invoked during conversion:
|
322
371
|
|
@@ -349,7 +398,7 @@ Finally, by invoking `convert` method and specifying `:upcase` as the target for
|
|
349
398
|
converter.convert('magic').to(:upcase) # => 'MAGIC'
|
350
399
|
```
|
351
400
|
|
352
|
-
#### 3.
|
401
|
+
#### 3.7.2 Using a Proc
|
353
402
|
|
354
403
|
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
404
|
|
@@ -371,12 +420,18 @@ converter.convert('magic').to(:upcase) # => 'MAGIC'
|
|
371
420
|
|
372
421
|
## Contributing
|
373
422
|
|
374
|
-
|
423
|
+
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.
|
424
|
+
|
425
|
+
1. Fork it ( https://github.com/piotrmurach/necromancer/fork )
|
375
426
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
376
427
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
377
428
|
4. Push to the branch (`git push origin my-new-feature`)
|
378
429
|
5. Create a new Pull Request
|
379
430
|
|
431
|
+
## License
|
432
|
+
|
433
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
434
|
+
|
380
435
|
## Copyright
|
381
436
|
|
382
|
-
Copyright (c) 2014 Piotr Murach. See LICENSE for further details.
|
437
|
+
Copyright (c) 2014-2017 Piotr Murach. See LICENSE for further details.
|