str2duck 1.1.1 → 1.2.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 +8 -8
- data/README.md +27 -5
- data/VERSION +1 -1
- data/examples/sample.rb +9 -7
- data/examples/test.rb +2 -34
- data/lib/str2duck/config.rb +27 -0
- data/lib/str2duck/parser.rb +3 -1
- data/lib/str2duck.rb +4 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NDU2ZTU1M2Y1YjIwYzRjY2I5OTM2MWY4ZDcyY2I2NDAyMDU2M2EzZg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NjU5ODJlMTA2MDM0MTA2NjYwOTQ2NDA2NjMxZjIwN2YxZWNlYTQzMg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZjUxNDM2Y2M2M2QyMjBjNjMzOTUzYjA5NDFkMGUwNjRkZTRlNzRiMzZmOGU3
|
10
|
+
YWQzODNiZDEzMjhlYTEwZDcxNzYwZGUxYTNiMDhiNGY2YWFjNzU3MjY0YjFh
|
11
|
+
YjllMDc5N2YwMWE1ZDJhNjk3ZDgyNmM2OWJkYmRiNGZhZDE1OWI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZmMyYjAzOWE2N2Y2ZTg2Mjc3NjM3ZDA0NmMyYjc4YjU1NDE4MmY3NmNhOGU3
|
14
|
+
ZTk5MzY0YWQ0YzYyZjRiMGI3ZThlOGZhNTI4NGQ5MjU2ODFiZGVhZjhiMjQ0
|
15
|
+
OTYwZTViNWU3MGJhZGI4YjJjNDhiNGJlOWI5MWU5YThjZGQyODc=
|
data/README.md
CHANGED
@@ -2,7 +2,17 @@ str2duck
|
|
2
2
|
========
|
3
3
|
|
4
4
|
String to Duck type parser
|
5
|
-
It can parse int, float, time, date . datetime, booleans etc from string
|
5
|
+
It can parse int, float, time, date . datetime, booleans ,json, yaml etc from string
|
6
|
+
|
7
|
+
The main Goal for this project to give flexibility when you work on REST protocol where,
|
8
|
+
most of the obj will be sent as string.
|
9
|
+
|
10
|
+
With this you can do simeple validations like
|
11
|
+
```ruby
|
12
|
+
|
13
|
+
params['hash_obj_key'].duck.class <= Hash
|
14
|
+
|
15
|
+
```
|
6
16
|
|
7
17
|
Possible bug source, not yet been tested is the american time format.
|
8
18
|
|
@@ -11,11 +21,14 @@ Possible bug source, not yet been tested is the american time format.
|
|
11
21
|
require 'str2duck'
|
12
22
|
|
13
23
|
"2011-03-12".duck #> Date obj
|
14
|
-
"false".
|
24
|
+
"false".to_duck #> False obj
|
15
25
|
"123".duck #> Integer obj
|
16
26
|
"123.123".duck #> Float obj
|
17
27
|
"2012-09-12T14:49:50+02:00".duck #> Time obj
|
18
28
|
|
29
|
+
# without sugar syntax, you can use this
|
30
|
+
Str2Duck.parse("123.123") #> Float obj
|
31
|
+
|
19
32
|
```
|
20
33
|
|
21
34
|
it is possible ot extend the Duck flexibility be require the Active Supports time extension,
|
@@ -36,9 +49,18 @@ like:
|
|
36
49
|
|
37
50
|
```
|
38
51
|
|
39
|
-
|
52
|
+
If you dont want one or more parser to be active on parse, you can simply config th str2duck parser like this
|
53
|
+
|
54
|
+
```ruby
|
40
55
|
|
56
|
+
# This will turn of yaml string parsing for example.
|
57
|
+
# From this point parsing will not check for yaml but return a string instead if none match
|
58
|
+
Str2Duck::Config.yaml = false
|
41
59
|
|
42
|
-
|
43
|
-
|
60
|
+
# This will return the implemented parsers, so you dont have to bingo
|
61
|
+
puts Str2Duck::Config.list
|
44
62
|
|
63
|
+
```
|
64
|
+
|
65
|
+
|
66
|
+
Happy parsing!
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
data/examples/sample.rb
CHANGED
@@ -11,8 +11,9 @@ require_relative "../lib/str2duck"
|
|
11
11
|
"true",
|
12
12
|
"false",
|
13
13
|
"some string data",
|
14
|
-
"{\"hello\":\"
|
15
|
-
"--- hello\n..."
|
14
|
+
"{\"hello\":\"json\"}",
|
15
|
+
"--- hello\n..." ,
|
16
|
+
"hello: yaml"
|
16
17
|
].each do |object|
|
17
18
|
puts object.duck,object.duck.class,""
|
18
19
|
end
|
@@ -21,7 +22,8 @@ begin
|
|
21
22
|
|
22
23
|
require File.join 'active_support','time'
|
23
24
|
|
24
|
-
puts "
|
25
|
+
puts "But if you load the Active support gem like when you do anyway in Rails,",
|
26
|
+
"-> the parser will be more flexible in the time formats"
|
25
27
|
|
26
28
|
[
|
27
29
|
"2011-03-12",
|
@@ -33,13 +35,13 @@ begin
|
|
33
35
|
"123.432",
|
34
36
|
"true",
|
35
37
|
"false",
|
36
|
-
"some string data"
|
38
|
+
"some string data",
|
39
|
+
"Sun, 28 Aug 2005",
|
40
|
+
"Fri, 25 Jan 2013 20:02:15 +0100"
|
41
|
+
|
37
42
|
].each do |object|
|
38
43
|
puts object.duck,object.duck.class,""
|
39
44
|
end
|
40
45
|
|
41
|
-
puts "Sun, 28 Aug 2005".duck.class
|
42
|
-
puts "Fri, 25 Jan 2013 20:02:15 +0100".duck.class
|
43
|
-
|
44
46
|
rescue LoadError
|
45
47
|
end
|
data/examples/test.rb
CHANGED
@@ -1,36 +1,4 @@
|
|
1
1
|
require_relative "../lib/str2duck"
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
[
|
6
|
-
"false"
|
7
|
-
].each do |object|
|
8
|
-
puts object.duck,object.duck.class,""
|
9
|
-
end
|
10
|
-
|
11
|
-
#begin
|
12
|
-
#
|
13
|
-
# require File.join 'active_support','time'
|
14
|
-
#
|
15
|
-
# puts "but if you load the Active support gem like when you do anyway in Rails, the duck will be more with time formats flexible"
|
16
|
-
#
|
17
|
-
# [
|
18
|
-
# "2011-03-12",
|
19
|
-
# "2007-07-20 18:59:27 +0200",
|
20
|
-
# "2010-10-30 18:02:56 +0200",
|
21
|
-
# "2012-09-12T14:49:50+02:00",
|
22
|
-
# "123",
|
23
|
-
# "asd",
|
24
|
-
# "123.432",
|
25
|
-
# "true",
|
26
|
-
# "false",
|
27
|
-
# "some string data"
|
28
|
-
# ].each do |object|
|
29
|
-
# puts object.duck,object.duck.class,""
|
30
|
-
# end
|
31
|
-
#
|
32
|
-
# puts "Sun, 28 Aug 2005".duck.class
|
33
|
-
# puts "Fri, 25 Jan 2013 20:02:15 +0100".duck.class
|
34
|
-
#
|
35
|
-
#rescue LoadError
|
36
|
-
#end
|
3
|
+
Str2Duck::Config.yaml = false
|
4
|
+
puts Str2Duck::Config.list
|
@@ -0,0 +1,27 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
module Str2Duck
|
3
|
+
module Config
|
4
|
+
|
5
|
+
::Str2Duck::Format.singleton_methods.each do |method|
|
6
|
+
|
7
|
+
self.define_singleton_method "#{method}=" do |boolean|
|
8
|
+
unless !!boolean == boolean
|
9
|
+
raise( ArgumentError,"invalid value given #{boolean.inspect},input must be a boolean!" )
|
10
|
+
end
|
11
|
+
self.class_variable_set("@@#{method.to_s}",boolean)
|
12
|
+
end
|
13
|
+
|
14
|
+
self.define_singleton_method(method) do
|
15
|
+
self.class_variable_get("@@#{method.to_s}")
|
16
|
+
end
|
17
|
+
|
18
|
+
self.class_variable_set("@@#{method.to_s}",true)
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.list
|
23
|
+
::Str2Duck::Format.singleton_methods
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
data/lib/str2duck/parser.rb
CHANGED
@@ -6,7 +6,9 @@ module Str2Duck
|
|
6
6
|
return_value= nil
|
7
7
|
|
8
8
|
[ :datetime, :date, :time, :true, :false, :float, :integer, :json, :yaml ].each do |method_name|
|
9
|
-
|
9
|
+
if ::Str2Duck::Config.__send__(method_name)
|
10
|
+
return_value ||= Str2Duck::Format.__send__ method_name, string_data_obj
|
11
|
+
end
|
10
12
|
end
|
11
13
|
|
12
14
|
return_value ||= string_data_obj
|
data/lib/str2duck.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
#encoding: UTF-8
|
2
2
|
|
3
3
|
require File.join File.dirname(__FILE__),'str2duck','support'
|
4
|
-
|
4
|
+
|
5
5
|
require File.join File.dirname(__FILE__),'str2duck','regexp'
|
6
|
+
require File.join File.dirname(__FILE__),'str2duck','format'
|
7
|
+
require File.join File.dirname(__FILE__),'str2duck','config'
|
8
|
+
|
6
9
|
require File.join File.dirname(__FILE__),'str2duck','parser'
|
7
10
|
require File.join File.dirname(__FILE__),'str2duck','string'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: str2duck
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Luzsi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-02 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ! ' Parse string into obj '
|
14
14
|
email:
|
@@ -28,6 +28,7 @@ files:
|
|
28
28
|
- examples/test.rb
|
29
29
|
- files.rb
|
30
30
|
- lib/str2duck.rb
|
31
|
+
- lib/str2duck/config.rb
|
31
32
|
- lib/str2duck/format.rb
|
32
33
|
- lib/str2duck/parser.rb
|
33
34
|
- lib/str2duck/regexp.rb
|