str2duck 1.6.0 → 1.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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/VERSION +1 -1
- data/lib/str2duck/format.rb +19 -27
- data/lib/str2duck/mpatch/object.rb +6 -16
- data/lib/str2duck/parser.rb +5 -8
- data/lib/str2duck/regexp.rb +12 -7
- data/lib/str2duck.rb +0 -7
- data/test/test_parse.rb +29 -0
- metadata +6 -6
- data/Gemfile.lock +0 -14
- data/lib/str2duck/mpatch/string.rb +0 -23
- data/lib/str2duck/support.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ed1679d8461e9701052f12181183d27973a6fd7
|
4
|
+
data.tar.gz: 17f617ef6025bf2693bf50b5d09a2fff1bce5c2c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e286704a3e04cd513e1d845ac4a627aeaa9e939bfd4afe39aeda42487fb3c2add2f24d9ff657c4160da54fece42ddeb08d30bcc01f85a99afd1931bcd45b2f8e
|
7
|
+
data.tar.gz: 7736bdcc6a50e469e64d488dd8fb1673d7ebf33854c8ca0501e1513b1d23fa6ba39738b7c450d74520f108e4154ae0ea7228c62352efbcb9e8a512114cdc2ae9
|
data/.gitignore
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.7.0
|
data/lib/str2duck/format.rb
CHANGED
@@ -1,19 +1,16 @@
|
|
1
1
|
#encoding: UTF-8
|
2
|
+
require 'yaml'
|
3
|
+
require 'json'
|
4
|
+
|
2
5
|
module Str2Duck
|
3
6
|
module Format
|
4
7
|
class << self
|
5
8
|
|
6
9
|
def datetime obj
|
7
10
|
if Str2Duck::Regexp.datetime?(obj)
|
8
|
-
if defined? DateTime
|
11
|
+
if defined?(DateTime) && DateTime.respond_to?(:parse)
|
9
12
|
return DateTime.parse obj
|
10
13
|
else
|
11
|
-
|
12
|
-
time_parts= obj.scan(/\d+/).map(&:to_i)
|
13
|
-
|
14
|
-
puts time_parts.inspect
|
15
|
-
Process.exit
|
16
|
-
|
17
14
|
if time_parts.count == 8
|
18
15
|
2.times{time_parts.pop}
|
19
16
|
elsif time_parts.count == 6
|
@@ -21,8 +18,7 @@ module Str2Duck
|
|
21
18
|
end
|
22
19
|
return Time.new(*time_parts)
|
23
20
|
end
|
24
|
-
end
|
25
|
-
nil
|
21
|
+
end;nil
|
26
22
|
end
|
27
23
|
|
28
24
|
def date obj
|
@@ -33,8 +29,7 @@ module Str2Duck
|
|
33
29
|
time_parts= obj.scan(/\d+/).map(&:to_i)
|
34
30
|
return Time.new(*time_parts)
|
35
31
|
end
|
36
|
-
end
|
37
|
-
nil
|
32
|
+
end;nil
|
38
33
|
end
|
39
34
|
|
40
35
|
def time obj
|
@@ -46,51 +41,48 @@ module Str2Duck
|
|
46
41
|
1.times{time_parts.pop}
|
47
42
|
return Time.new(*time_parts)
|
48
43
|
end
|
49
|
-
end
|
50
|
-
nil
|
44
|
+
end;nil
|
51
45
|
end
|
52
46
|
|
53
47
|
def true obj
|
54
48
|
if Str2Duck::Regexp.true?(obj)
|
55
49
|
return true
|
56
|
-
end
|
57
|
-
nil
|
50
|
+
end;nil
|
58
51
|
end
|
59
52
|
|
60
53
|
def false obj
|
61
54
|
if Str2Duck::Regexp.false?(obj)
|
62
55
|
return false
|
63
|
-
end
|
64
|
-
nil
|
56
|
+
end;nil
|
65
57
|
end
|
66
58
|
|
67
59
|
def float obj
|
68
60
|
if Str2Duck::Regexp.float?(obj)
|
69
|
-
return obj.to_f
|
70
|
-
end
|
71
|
-
nil
|
61
|
+
return obj.sub(',','.').to_f
|
62
|
+
end;nil
|
72
63
|
end
|
73
64
|
|
74
65
|
def integer obj
|
75
66
|
if Str2Duck::Regexp.integer?(obj)
|
76
67
|
return obj.to_i
|
77
|
-
end
|
78
|
-
nil
|
68
|
+
end;nil
|
79
69
|
end
|
80
70
|
|
81
71
|
def json obj
|
82
72
|
if Str2Duck::Regexp.json?(obj)
|
83
73
|
return JSON.parse(obj)
|
84
|
-
end
|
85
|
-
nil
|
74
|
+
end;nil
|
86
75
|
end
|
87
76
|
|
88
77
|
# damn, this thing eats almost everything...
|
89
78
|
def yaml obj
|
90
79
|
if Str2Duck::Regexp.yaml?(obj)
|
91
|
-
|
92
|
-
|
93
|
-
|
80
|
+
if YAML.respond_to?(:safe_load)
|
81
|
+
return YAML.safe_load(obj)
|
82
|
+
else
|
83
|
+
return YAML.load(obj)
|
84
|
+
end
|
85
|
+
end;nil
|
94
86
|
end
|
95
87
|
|
96
88
|
end
|
@@ -1,30 +1,20 @@
|
|
1
1
|
module Str2Duck
|
2
|
-
|
3
2
|
module MPatch
|
3
|
+
module ObjectEXT
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
if self_obj.class <= String
|
10
|
-
Str2Duck::MPatch::String.duck(self_obj)
|
5
|
+
def to_duck
|
6
|
+
if self.class <= String
|
7
|
+
Str2Duck.parse(self)
|
11
8
|
else
|
12
|
-
return
|
9
|
+
return self
|
13
10
|
end
|
14
11
|
|
15
12
|
end
|
16
13
|
|
17
14
|
alias :duck :to_duck
|
18
15
|
|
19
|
-
self.instance_methods.each do |symbol|
|
20
|
-
module_function symbol
|
21
|
-
public symbol
|
22
|
-
end
|
23
|
-
|
24
16
|
end
|
25
|
-
|
26
17
|
end
|
27
|
-
|
28
18
|
end
|
29
19
|
|
30
|
-
Object.__send__
|
20
|
+
Object.__send__ :include, Str2Duck::MPatch::ObjectEXT
|
data/lib/str2duck/parser.rb
CHANGED
@@ -2,20 +2,17 @@
|
|
2
2
|
module Str2Duck
|
3
3
|
|
4
4
|
def self.activesupport
|
5
|
-
require
|
5
|
+
require(File.join 'active_support','time')
|
6
6
|
rescue LoadError
|
7
|
-
return
|
7
|
+
return true
|
8
8
|
end
|
9
9
|
|
10
10
|
def self.parse str
|
11
11
|
raise(ArgumentError,"invalid input, must be string like") unless str.class <= String
|
12
|
-
|
13
|
-
@@activesupport ||= activesupport
|
14
|
-
|
15
|
-
var= nil
|
12
|
+
@activesupport ||= activesupport
|
16
13
|
[ :datetime, :date, :time, :true, :false, :float, :integer, :json, :yaml ].each do |method_name|
|
17
|
-
if ::Str2Duck::Config.
|
18
|
-
var= Str2Duck::Format.
|
14
|
+
if ::Str2Duck::Config.public_send(method_name)
|
15
|
+
var = Str2Duck::Format.public_send method_name, str
|
19
16
|
return var unless var.nil?
|
20
17
|
end
|
21
18
|
end
|
data/lib/str2duck/regexp.rb
CHANGED
@@ -16,7 +16,7 @@ module Str2Duck
|
|
16
16
|
answer_value ||= obj =~ regexp
|
17
17
|
end
|
18
18
|
|
19
|
-
return
|
19
|
+
return !!answer_value
|
20
20
|
|
21
21
|
end
|
22
22
|
|
@@ -30,7 +30,7 @@ module Str2Duck
|
|
30
30
|
answer_value ||= obj =~ regexp
|
31
31
|
end
|
32
32
|
|
33
|
-
return
|
33
|
+
return !!answer_value
|
34
34
|
|
35
35
|
end
|
36
36
|
|
@@ -43,24 +43,29 @@ module Str2Duck
|
|
43
43
|
answer_value ||= obj =~ regexp
|
44
44
|
end
|
45
45
|
|
46
|
-
return
|
46
|
+
return !!answer_value
|
47
47
|
|
48
48
|
end
|
49
49
|
|
50
50
|
def true? obj
|
51
|
-
return
|
51
|
+
return !!obj =~ /^true$/
|
52
52
|
end
|
53
53
|
|
54
54
|
def false? obj
|
55
|
-
return
|
55
|
+
return !!obj =~ /^false$/
|
56
56
|
end
|
57
57
|
|
58
58
|
def float? obj
|
59
|
-
|
59
|
+
case obj.to_s
|
60
|
+
when /^\d+\.\d+$/,/^\d+,\d+$/
|
61
|
+
return true
|
62
|
+
else
|
63
|
+
return false
|
64
|
+
end
|
60
65
|
end
|
61
66
|
|
62
67
|
def integer? obj
|
63
|
-
return
|
68
|
+
return !!obj =~ /^\d+$/
|
64
69
|
end
|
65
70
|
|
66
71
|
def json? obj
|
data/lib/str2duck.rb
CHANGED
@@ -1,15 +1,8 @@
|
|
1
1
|
#encoding: UTF-8
|
2
2
|
|
3
|
-
require 'json'
|
4
|
-
require 'yaml'
|
5
|
-
|
6
|
-
require 'str2duck/support'
|
7
|
-
|
8
3
|
require 'str2duck/regexp'
|
9
4
|
require 'str2duck/format'
|
10
5
|
require 'str2duck/config'
|
11
|
-
|
12
6
|
require 'str2duck/parser'
|
13
7
|
|
14
|
-
require 'str2duck/mpatch/string'
|
15
8
|
require 'str2duck/mpatch/object'
|
data/test/test_parse.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "str2duck"
|
2
|
+
require 'minitest/autorun'
|
3
|
+
describe 'StringParse' do
|
4
|
+
|
5
|
+
specify 'parse objects into the right format' do
|
6
|
+
|
7
|
+
{
|
8
|
+
"2011-03-12"=>Date,
|
9
|
+
"2007-07-20 18:59:27 +0200"=>Time,
|
10
|
+
"2010-10-30 18:02:56 +0200"=>Time,
|
11
|
+
"2012-09-12T14:49:50+02:00"=>DateTime,
|
12
|
+
"123"=>Fixnum,
|
13
|
+
"asd"=>String,
|
14
|
+
"123.432"=>Float,
|
15
|
+
"123,432"=>Float,
|
16
|
+
"true"=>TrueClass,
|
17
|
+
"false"=>FalseClass,
|
18
|
+
"some string data"=>String,
|
19
|
+
"{\"hello\":\"json\"}"=>Hash,
|
20
|
+
"--- hello\n..."=>String,
|
21
|
+
"hello: yaml"=>Hash
|
22
|
+
}.each_pair do |str,klass|
|
23
|
+
str.to_duck.must_be_instance_of klass
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
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.7.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-
|
11
|
+
date: 2014-10-17 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: " Parse string into obj "
|
14
14
|
email:
|
@@ -19,7 +19,6 @@ extra_rdoc_files: []
|
|
19
19
|
files:
|
20
20
|
- ".gitignore"
|
21
21
|
- Gemfile
|
22
|
-
- Gemfile.lock
|
23
22
|
- LICENSE
|
24
23
|
- README.md
|
25
24
|
- Rakefile
|
@@ -30,11 +29,10 @@ files:
|
|
30
29
|
- lib/str2duck/config.rb
|
31
30
|
- lib/str2duck/format.rb
|
32
31
|
- lib/str2duck/mpatch/object.rb
|
33
|
-
- lib/str2duck/mpatch/string.rb
|
34
32
|
- lib/str2duck/parser.rb
|
35
33
|
- lib/str2duck/regexp.rb
|
36
|
-
- lib/str2duck/support.rb
|
37
34
|
- str2duck.gemspec
|
35
|
+
- test/test_parse.rb
|
38
36
|
homepage: https://github.com/adamluzsi/str2duck
|
39
37
|
licenses: []
|
40
38
|
metadata: {}
|
@@ -58,4 +56,6 @@ rubygems_version: 2.2.2
|
|
58
56
|
signing_key:
|
59
57
|
specification_version: 4
|
60
58
|
summary: String to Obj, Duck type parser
|
61
|
-
test_files:
|
59
|
+
test_files:
|
60
|
+
- test/test_parse.rb
|
61
|
+
has_rdoc:
|
data/Gemfile.lock
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
module Str2Duck
|
2
|
-
|
3
|
-
module MPatch
|
4
|
-
|
5
|
-
module String
|
6
|
-
|
7
|
-
def to_duck(self_obj= self)
|
8
|
-
Str2Duck.parse(self_obj)
|
9
|
-
end
|
10
|
-
alias :duck :to_duck
|
11
|
-
|
12
|
-
self.instance_methods.each do |symbol|
|
13
|
-
module_function symbol
|
14
|
-
public symbol
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
String.__send__( :include, Str2Duck::MPatch::String )
|