str2duck 1.7.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -8
- data/VERSION +1 -1
- data/examples/sample.rb +1 -0
- data/lib/str2duck.rb +11 -6
- data/lib/str2duck/converter.rb +73 -0
- data/lib/str2duck/core_ext.rb +3 -0
- data/lib/str2duck/core_ext/object.rb +11 -0
- data/lib/str2duck/matcher.rb +79 -0
- data/lib/str2duck/parser.rb +27 -15
- data/spec/spec_helper.rb +2 -0
- data/spec/str2duc_spec.rb +22 -0
- data/spec/str2duck/converter_spec.rb +91 -0
- data/spec/str2duck/core_ext/object_spec.rb +19 -0
- data/spec/str2duck/matcher_spec.rb +87 -0
- data/str2duck.gemspec +1 -0
- data/test/test_parse.rb +21 -16
- metadata +30 -9
- data/examples/test.rb +0 -13
- data/lib/str2duck/config.rb +0 -27
- data/lib/str2duck/format.rb +0 -90
- data/lib/str2duck/mpatch/object.rb +0 -20
- data/lib/str2duck/regexp.rb +0 -90
- data/test/test_obj.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9407d3d02dc0761903619a86237ef5b7564c50f0
|
4
|
+
data.tar.gz: e6a69d843d5b3863e61ce43ab6f70f58412a4870
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85745683941860a35e9501d24e99a291eac6c378fc0de8dacb478e6b1c0f06d386e74332454c9cebe52a1ef18f1eb8fd309c15aa48c9f213d8d993d30537f775
|
7
|
+
data.tar.gz: 3ddd22c3dcbcc7722d6d4665474496749231b8c8a77d569afe97a38d291ed8bb2c3e39a0d9699d1148b5b4f3bdc2b090cbbc91a0905a97291205035de8bde399
|
data/README.md
CHANGED
@@ -47,17 +47,12 @@ like:
|
|
47
47
|
|
48
48
|
```
|
49
49
|
|
50
|
-
If you dont want one or more parser to be active on parse,
|
50
|
+
If you dont want one or more parser to be active on parse, pass the strategie names you want to be used on parsing
|
51
51
|
|
52
52
|
```ruby
|
53
53
|
|
54
|
-
|
55
|
-
|
56
|
-
Str2Duck::Config.yaml = false
|
57
|
-
|
58
|
-
# This will return the implemented parsers, so you dont have to bingo
|
59
|
-
puts Str2Duck::Config.list
|
60
|
-
|
54
|
+
Str2Duck.parse('string',:datetime,:date)
|
55
|
+
|
61
56
|
```
|
62
57
|
|
63
58
|
Now it has a new object syntax sugar patch that allow you to call the duck methods on any class if you not sure,
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
2.0.0
|
data/examples/sample.rb
CHANGED
data/lib/str2duck.rb
CHANGED
@@ -1,8 +1,13 @@
|
|
1
|
-
|
1
|
+
module Str2Duck
|
2
2
|
|
3
|
-
require 'str2duck/
|
4
|
-
require 'str2duck/
|
5
|
-
require 'str2duck/
|
6
|
-
require 'str2duck/parser'
|
3
|
+
require 'str2duck/matcher'
|
4
|
+
require 'str2duck/converter'
|
5
|
+
require 'str2duck/parser'
|
7
6
|
|
8
|
-
require 'str2duck/
|
7
|
+
require 'str2duck/core_ext'
|
8
|
+
|
9
|
+
def self.parse(str,*parse_strategies)
|
10
|
+
self::Parser.new(*parse_strategies).parse(str)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Str2Duck::Converter
|
2
|
+
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def datetime(obj)
|
6
|
+
if Str2Duck::Matcher.datetime?(obj)
|
7
|
+
if defined?(DateTime) && DateTime.respond_to?(:parse)
|
8
|
+
return DateTime.parse(obj)
|
9
|
+
# else
|
10
|
+
# if time_parts.count == 8
|
11
|
+
# 2.times { time_parts.pop }
|
12
|
+
# elsif time_parts.count == 6
|
13
|
+
# return nil
|
14
|
+
# end
|
15
|
+
# return Time.new(*time_parts)
|
16
|
+
end
|
17
|
+
end; nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def date(obj)
|
21
|
+
if Str2Duck::Matcher.date?(obj)
|
22
|
+
begin
|
23
|
+
return Date.parse(obj)
|
24
|
+
rescue NoMethodError
|
25
|
+
time_parts= obj.scan(/\d+/).map(&:to_i)
|
26
|
+
return Time.new(*time_parts)
|
27
|
+
end
|
28
|
+
end; nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def time(obj)
|
32
|
+
if Str2Duck::Matcher.time?(obj)
|
33
|
+
begin
|
34
|
+
return Time.parse(obj)
|
35
|
+
rescue NoMethodError
|
36
|
+
time_parts= obj.scan(/\d+/).map(&:to_i)
|
37
|
+
1.times { time_parts.pop }
|
38
|
+
return Time.new(*time_parts)
|
39
|
+
end
|
40
|
+
end; nil
|
41
|
+
end
|
42
|
+
|
43
|
+
def true(obj)
|
44
|
+
if Str2Duck::Matcher.true?(obj)
|
45
|
+
return true
|
46
|
+
end; nil
|
47
|
+
end
|
48
|
+
|
49
|
+
def false(obj)
|
50
|
+
if Str2Duck::Matcher.false?(obj)
|
51
|
+
return false
|
52
|
+
end; nil
|
53
|
+
end
|
54
|
+
|
55
|
+
def float(obj)
|
56
|
+
if Str2Duck::Matcher.float?(obj)
|
57
|
+
return obj.sub(',', '.').to_f
|
58
|
+
end; nil
|
59
|
+
end
|
60
|
+
|
61
|
+
def integer(obj)
|
62
|
+
if Str2Duck::Matcher.integer?(obj)
|
63
|
+
return obj.to_i
|
64
|
+
end; nil
|
65
|
+
end
|
66
|
+
|
67
|
+
def json(obj)
|
68
|
+
if Str2Duck::Matcher.json?(obj)
|
69
|
+
return JSON.parse(obj)
|
70
|
+
end; nil
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Str2Duck::Matcher
|
2
|
+
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def datetime?(obj)
|
6
|
+
|
7
|
+
answer_value= nil
|
8
|
+
[
|
9
|
+
/^\w+, \d+ \w+ \d+ \d\d:\d\d:\d\d \+\d+$/,
|
10
|
+
/^-?\d+-\d\d-\d\d\w\d\d:\d\d:\d\d\+\d\d:\d\d$/,
|
11
|
+
/\w+ \w+ \d+ \d+ \d+:\d+:\d+ \w+\+\d+ \(\w+\)/,
|
12
|
+
/^-?\d+-\d\d?-\d\d?\w\d\d?:\d\d?:\d\d?\w$/
|
13
|
+
].each do |regexp|
|
14
|
+
answer_value ||= obj =~ regexp
|
15
|
+
end
|
16
|
+
|
17
|
+
return !!answer_value
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
def date?(obj)
|
22
|
+
|
23
|
+
answer_value= nil
|
24
|
+
[
|
25
|
+
/^\d+-\d\d-\d\d$/,
|
26
|
+
/^\w+, \d+ \w+ \d+$/
|
27
|
+
].each do |regexp|
|
28
|
+
answer_value ||= obj =~ regexp
|
29
|
+
end
|
30
|
+
|
31
|
+
return !!answer_value
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
def time?(obj)
|
36
|
+
|
37
|
+
answer_value= nil
|
38
|
+
[
|
39
|
+
/^\d+-\d\d-\d\d \d\d:\d\d:\d\d \+\d+$/
|
40
|
+
].each do |regexp|
|
41
|
+
answer_value ||= obj =~ regexp
|
42
|
+
end
|
43
|
+
|
44
|
+
return !!answer_value
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
def true?(obj)
|
49
|
+
return !!(obj =~ /^true$/)
|
50
|
+
end
|
51
|
+
|
52
|
+
def false?(obj)
|
53
|
+
return !!(obj =~ /^false$/)
|
54
|
+
end
|
55
|
+
|
56
|
+
def float?(obj)
|
57
|
+
case obj.to_s
|
58
|
+
when /^\d+\.\d+$/, /^\d+,\d+$/
|
59
|
+
return true
|
60
|
+
else
|
61
|
+
return false
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def integer?(obj)
|
66
|
+
return !!(obj =~ /^\d+$/)
|
67
|
+
end
|
68
|
+
|
69
|
+
def json?(obj)
|
70
|
+
return false unless defined?(JSON)
|
71
|
+
begin
|
72
|
+
JSON.parse(obj)
|
73
|
+
return true
|
74
|
+
rescue ::JSON::ParserError
|
75
|
+
return false
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
data/lib/str2duck/parser.rb
CHANGED
@@ -1,24 +1,36 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
class Str2Duck::Parser
|
2
|
+
|
3
|
+
DEFAULT_STRATEGIES = [:datetime, :date, :time, :true, :false, :float, :integer, :json].freeze
|
4
|
+
|
5
|
+
def initialize(*parse_strategies)
|
6
|
+
|
7
|
+
@parse_strategies = parse_strategies.map(&:to_s).map(&:to_sym)
|
8
|
+
@parse_strategies.push(*DEFAULT_STRATEGIES) if @parse_strategies.empty?
|
9
|
+
|
10
|
+
require_activesupport
|
3
11
|
|
4
|
-
def self.activesupport
|
5
|
-
require(File.join 'active_support','time')
|
6
|
-
rescue LoadError
|
7
|
-
return true
|
8
12
|
end
|
9
13
|
|
10
|
-
def
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
14
|
+
def parse(str)
|
15
|
+
|
16
|
+
raise(ArgumentError, 'invalid input, must be string like') unless str.class <= String
|
17
|
+
|
18
|
+
@parse_strategies.each do |method_name|
|
19
|
+
var = Str2Duck::Converter.public_send(method_name, str)
|
20
|
+
return var unless var.nil?
|
18
21
|
end
|
19
22
|
|
20
23
|
return str
|
21
24
|
|
22
25
|
end
|
23
26
|
|
24
|
-
|
27
|
+
protected
|
28
|
+
|
29
|
+
def require_activesupport
|
30
|
+
require(File.join 'active_support', 'time') unless @activesupport_required
|
31
|
+
rescue LoadError
|
32
|
+
ensure
|
33
|
+
@activesupport_required = true
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
describe Str2Duck do
|
3
|
+
|
4
|
+
describe '.parse' do
|
5
|
+
|
6
|
+
let(:str){'123'}
|
7
|
+
let(:args){[]}
|
8
|
+
subject{ described_class.parse(str,*args) }
|
9
|
+
|
10
|
+
context 'when no strategy passed only object to be parsed' do
|
11
|
+
it { is_expected.to eq 123 }
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'when args is limited to datetime type' do
|
15
|
+
let(:args){[:datetime]}
|
16
|
+
|
17
|
+
it { is_expected.to eq '123' }
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
describe Str2Duck::Converter do
|
3
|
+
|
4
|
+
let(:str) { 'duck' }
|
5
|
+
|
6
|
+
describe '#datetime' do
|
7
|
+
subject { described_class.datetime(str) }
|
8
|
+
|
9
|
+
context 'when the format is like \w+, \d+ \w+ \d+ \d\d:\d\d:\d\d \+\d+' do
|
10
|
+
let(:str) { "Fri, 25 Jan 2013 20:02:15 +0100" }
|
11
|
+
|
12
|
+
it { is_expected.to be_a DateTime }
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when the format is like /^-?\d+-\d\d-\d\d\w\d\d:\d\d:\d\d\+\d\d:\d\d$/' do
|
16
|
+
let(:str) { "2012-09-12T14:49:50+02:00" }
|
17
|
+
|
18
|
+
it { is_expected.to be_a DateTime }
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'when the format is like /^-?\d+-\d\d?-\d\d?\w\d\d?:\d\d?:\d\d?\w$/' do
|
22
|
+
let(:str) { "2012-09-12T14:49:50" }
|
23
|
+
|
24
|
+
it { is_expected.to be_a DateTime }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'when invalid value given' do
|
28
|
+
let(:str) { 'not-a-validTdatetime' }
|
29
|
+
|
30
|
+
it { is_expected.to be nil }
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '#date' do
|
36
|
+
|
37
|
+
subject { described_class.date(str) }
|
38
|
+
|
39
|
+
context 'when the date format is like /^\d+-\d\d-\d\d$/' do
|
40
|
+
let(:str) { "2011-03-12" }
|
41
|
+
|
42
|
+
it { is_expected.to be_a Date }
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'when the date format is like /^\w+, \d+ \w+ \d+$/' do
|
46
|
+
let(:str) { "Sun, 28 Aug 2005" }
|
47
|
+
|
48
|
+
it { is_expected.to be_a Date }
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#time' do
|
54
|
+
|
55
|
+
subject { described_class.time(str) }
|
56
|
+
|
57
|
+
context 'when time format is like: /^\d+-\d\d-\d\d \d\d:\d\d:\d\d \+\d+$/' do
|
58
|
+
let(:str) { "2010-10-30 18:02:56 +0200" }
|
59
|
+
|
60
|
+
it { is_expected.to be_a Time }
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '#true' do
|
66
|
+
it { expect(subject.true('true')).to be_a TrueClass }
|
67
|
+
it { expect(subject.true('else')).to be nil }
|
68
|
+
end
|
69
|
+
|
70
|
+
describe '#false' do
|
71
|
+
it { expect(subject.false('false')).to be_a FalseClass }
|
72
|
+
it { expect(subject.false('else')).to be nil }
|
73
|
+
end
|
74
|
+
|
75
|
+
describe '#float' do
|
76
|
+
it { expect(subject.float('123.5')).to be_a Float }
|
77
|
+
it { expect(subject.float('123')).to be nil }
|
78
|
+
end
|
79
|
+
|
80
|
+
describe '#integer' do
|
81
|
+
it { expect(subject.integer('123')).to be_a Integer }
|
82
|
+
it { expect(subject.integer('123.1')).to be nil }
|
83
|
+
end
|
84
|
+
|
85
|
+
describe '#json' do
|
86
|
+
before { require 'json' }
|
87
|
+
it { expect(subject.json(JSON.dump({hello: :world}))).to be_a Hash }
|
88
|
+
it { expect(subject.json('{not: "json"}')).to be nil }
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
describe Str2Duck::CoreExt::Object do
|
3
|
+
subject { Object.new }
|
4
|
+
|
5
|
+
describe '#to_duck' do
|
6
|
+
context 'when string given' do
|
7
|
+
it { expect('123'.to_duck).to eq 123 }
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'when not string given' do
|
11
|
+
subject{ Object.new }
|
12
|
+
it 'should return the object back' do
|
13
|
+
expect(subject.to_duck).to equal subject
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
describe Str2Duck::Matcher do
|
3
|
+
|
4
|
+
let(:str) { 'duck' }
|
5
|
+
|
6
|
+
describe '#datetime?' do
|
7
|
+
subject { described_class.datetime?(str) }
|
8
|
+
|
9
|
+
context 'when the format is like \w+, \d+ \w+ \d+ \d\d:\d\d:\d\d \+\d+' do
|
10
|
+
let(:str) { "Fri, 25 Jan 2013 20:02:15 +0100" }
|
11
|
+
|
12
|
+
it { is_expected.to eq true }
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when the format is like /^-?\d+-\d\d-\d\d\w\d\d:\d\d:\d\d\+\d\d:\d\d$/' do
|
16
|
+
let(:str) { "2012-09-12T14:49:50+02:00" }
|
17
|
+
|
18
|
+
it { is_expected.to eq true }
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'when the format is like /^-?\d+-\d\d?-\d\d?\w\d\d?:\d\d?:\d\d?\w$/' do
|
22
|
+
let(:str) { "2012-09-12T14:49:50" }
|
23
|
+
|
24
|
+
it { is_expected.to eq true }
|
25
|
+
end
|
26
|
+
|
27
|
+
#todo clean up code
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#date?' do
|
32
|
+
|
33
|
+
subject { described_class.date?(str) }
|
34
|
+
|
35
|
+
context 'when the date format is like /^\d+-\d\d-\d\d$/' do
|
36
|
+
let(:str) { "2011-03-12" }
|
37
|
+
|
38
|
+
it { is_expected.to eq true }
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'when the date format is like /^\w+, \d+ \w+ \d+$/' do
|
42
|
+
let(:str) { "Sun, 28 Aug 2005" }
|
43
|
+
|
44
|
+
it { is_expected.to eq true }
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#time?' do
|
50
|
+
|
51
|
+
subject { described_class.time?(str) }
|
52
|
+
|
53
|
+
context 'when time format is like: /^\d+-\d\d-\d\d \d\d:\d\d:\d\d \+\d+$/' do
|
54
|
+
let(:str) { "2010-10-30 18:02:56 +0200" }
|
55
|
+
|
56
|
+
it { is_expected.to eq true }
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
describe '#true?' do
|
62
|
+
it { expect(subject.true?('true')).to eq true }
|
63
|
+
it { expect(subject.true?('else')).to eq false }
|
64
|
+
end
|
65
|
+
|
66
|
+
describe '#false?' do
|
67
|
+
it { expect(subject.false?('false')).to eq true }
|
68
|
+
it { expect(subject.false?('else')).to eq false }
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '#float?' do
|
72
|
+
it { expect(subject.float?('123.5')).to eq true }
|
73
|
+
it { expect(subject.float?('123')).to eq false }
|
74
|
+
end
|
75
|
+
|
76
|
+
describe '#integer?' do
|
77
|
+
it { expect(subject.integer?('123')).to eq true }
|
78
|
+
it { expect(subject.integer?('123.1')).to eq false }
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '#json?' do
|
82
|
+
before { require 'json' }
|
83
|
+
it { expect(subject.json?(JSON.dump({hello: :world}))).to eq true }
|
84
|
+
it { expect(subject.json?('{not: "json"}')).to eq false }
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
data/str2duck.gemspec
CHANGED
data/test/test_parse.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(__dir__, '..', 'lib'))
|
1
2
|
require "str2duck"
|
2
3
|
require 'minitest/autorun'
|
3
4
|
describe 'StringParse' do
|
@@ -5,26 +6,30 @@ describe 'StringParse' do
|
|
5
6
|
specify 'parse objects into the right format' do
|
6
7
|
|
7
8
|
{
|
8
|
-
"2011-03-12"
|
9
|
-
"2007-07-20 18:59:27 +0200" =>
|
10
|
-
"2010-10-30 18:02:56 +0200" =>
|
11
|
-
"2012-09-12T14:49:50+02:00" =>
|
12
|
-
"123"
|
13
|
-
"asd"
|
14
|
-
"123.432"
|
15
|
-
"123,432"
|
16
|
-
"true"
|
17
|
-
"false"
|
18
|
-
"some string data"
|
19
|
-
"
|
20
|
-
"
|
21
|
-
|
22
|
-
"2014-11-04T15:46:06Z" => DateTime
|
23
|
-
}.each_pair do |str,klass|
|
9
|
+
"2011-03-12" => Date,
|
10
|
+
"2007-07-20 18:59:27 +0200" => Time,
|
11
|
+
"2010-10-30 18:02:56 +0200" => Time,
|
12
|
+
"2012-09-12T14:49:50+02:00" => DateTime,
|
13
|
+
"123" => Fixnum,
|
14
|
+
"asd" => String,
|
15
|
+
"123.432" => Float,
|
16
|
+
"123,432" => Float,
|
17
|
+
"true" => TrueClass,
|
18
|
+
"false" => FalseClass,
|
19
|
+
"some string data" => String,
|
20
|
+
"--- hello\n..." => String,
|
21
|
+
"2014-11-04T15:46:06Z" => DateTime
|
22
|
+
}.each_pair do |str, klass|
|
24
23
|
str.to_duck.must_be_instance_of klass
|
25
24
|
end
|
26
25
|
|
27
26
|
end
|
28
27
|
|
28
|
+
specify 'when checking json' do
|
29
|
+
require 'json'
|
30
|
+
"{\"hello\":\"json\"}".to_duck.must_be_instance_of Hash
|
31
|
+
'["dog","cat"]'.to_duck.must_be_instance_of Array
|
32
|
+
end
|
33
|
+
|
29
34
|
end
|
30
35
|
|
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:
|
4
|
+
version: 2.0.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:
|
11
|
+
date: 2015-07-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
description: " Parse string into obj "
|
28
42
|
email:
|
29
43
|
- adamluzsi@gmail.com
|
@@ -38,15 +52,18 @@ files:
|
|
38
52
|
- Rakefile
|
39
53
|
- VERSION
|
40
54
|
- examples/sample.rb
|
41
|
-
- examples/test.rb
|
42
55
|
- lib/str2duck.rb
|
43
|
-
- lib/str2duck/
|
44
|
-
- lib/str2duck/
|
45
|
-
- lib/str2duck/
|
56
|
+
- lib/str2duck/converter.rb
|
57
|
+
- lib/str2duck/core_ext.rb
|
58
|
+
- lib/str2duck/core_ext/object.rb
|
59
|
+
- lib/str2duck/matcher.rb
|
46
60
|
- lib/str2duck/parser.rb
|
47
|
-
-
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
- spec/str2duc_spec.rb
|
63
|
+
- spec/str2duck/converter_spec.rb
|
64
|
+
- spec/str2duck/core_ext/object_spec.rb
|
65
|
+
- spec/str2duck/matcher_spec.rb
|
48
66
|
- str2duck.gemspec
|
49
|
-
- test/test_obj.rb
|
50
67
|
- test/test_parse.rb
|
51
68
|
homepage: https://github.com/adamluzsi/str2duck
|
52
69
|
licenses: []
|
@@ -72,6 +89,10 @@ signing_key:
|
|
72
89
|
specification_version: 4
|
73
90
|
summary: String to Obj, Duck type parser
|
74
91
|
test_files:
|
75
|
-
-
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
- spec/str2duc_spec.rb
|
94
|
+
- spec/str2duck/converter_spec.rb
|
95
|
+
- spec/str2duck/core_ext/object_spec.rb
|
96
|
+
- spec/str2duck/matcher_spec.rb
|
76
97
|
- test/test_parse.rb
|
77
98
|
has_rdoc:
|
data/examples/test.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
require "str2duck"
|
2
|
-
|
3
|
-
puts "false".to_duck.class
|
4
|
-
|
5
|
-
# puts 123.duck.class
|
6
|
-
# puts ["hello","world"].duck.class
|
7
|
-
# puts '{"hello":"world"}'.duck.class
|
8
|
-
# puts "hello: world".duck.class,"","---"
|
9
|
-
#
|
10
|
-
#
|
11
|
-
# Str2Duck::Config.yaml = false
|
12
|
-
# puts Str2Duck::Config.list,"","---"
|
13
|
-
# puts "hello: world".duck.class
|
data/lib/str2duck/config.rb
DELETED
@@ -1,27 +0,0 @@
|
|
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/format.rb
DELETED
@@ -1,90 +0,0 @@
|
|
1
|
-
#encoding: UTF-8
|
2
|
-
require 'yaml'
|
3
|
-
require 'json'
|
4
|
-
|
5
|
-
module Str2Duck
|
6
|
-
module Format
|
7
|
-
class << self
|
8
|
-
|
9
|
-
def datetime obj
|
10
|
-
if Str2Duck::Regexp.datetime?(obj)
|
11
|
-
if defined?(DateTime) && DateTime.respond_to?(:parse)
|
12
|
-
return DateTime.parse obj
|
13
|
-
else
|
14
|
-
if time_parts.count == 8
|
15
|
-
2.times{time_parts.pop}
|
16
|
-
elsif time_parts.count == 6
|
17
|
-
return nil
|
18
|
-
end
|
19
|
-
return Time.new(*time_parts)
|
20
|
-
end
|
21
|
-
end;nil
|
22
|
-
end
|
23
|
-
|
24
|
-
def date obj
|
25
|
-
if Str2Duck::Regexp.date?(obj)
|
26
|
-
begin
|
27
|
-
return Date.parse obj
|
28
|
-
rescue NoMethodError
|
29
|
-
time_parts= obj.scan(/\d+/).map(&:to_i)
|
30
|
-
return Time.new(*time_parts)
|
31
|
-
end
|
32
|
-
end;nil
|
33
|
-
end
|
34
|
-
|
35
|
-
def time obj
|
36
|
-
if Str2Duck::Regexp.time?(obj)
|
37
|
-
begin
|
38
|
-
return Time.parse obj
|
39
|
-
rescue NoMethodError
|
40
|
-
time_parts= obj.scan(/\d+/).map(&:to_i)
|
41
|
-
1.times{time_parts.pop}
|
42
|
-
return Time.new(*time_parts)
|
43
|
-
end
|
44
|
-
end;nil
|
45
|
-
end
|
46
|
-
|
47
|
-
def true obj
|
48
|
-
if Str2Duck::Regexp.true?(obj)
|
49
|
-
return true
|
50
|
-
end;nil
|
51
|
-
end
|
52
|
-
|
53
|
-
def false obj
|
54
|
-
if Str2Duck::Regexp.false?(obj)
|
55
|
-
return false
|
56
|
-
end;nil
|
57
|
-
end
|
58
|
-
|
59
|
-
def float obj
|
60
|
-
if Str2Duck::Regexp.float?(obj)
|
61
|
-
return obj.sub(',','.').to_f
|
62
|
-
end;nil
|
63
|
-
end
|
64
|
-
|
65
|
-
def integer obj
|
66
|
-
if Str2Duck::Regexp.integer?(obj)
|
67
|
-
return obj.to_i
|
68
|
-
end;nil
|
69
|
-
end
|
70
|
-
|
71
|
-
def json obj
|
72
|
-
if Str2Duck::Regexp.json?(obj)
|
73
|
-
return JSON.parse(obj)
|
74
|
-
end;nil
|
75
|
-
end
|
76
|
-
|
77
|
-
# damn, this thing eats almost everything...
|
78
|
-
def yaml obj
|
79
|
-
if Str2Duck::Regexp.yaml?(obj)
|
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
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
module Str2Duck
|
2
|
-
module MPatch
|
3
|
-
module ObjectEXT
|
4
|
-
|
5
|
-
def to_duck
|
6
|
-
if self.class <= String
|
7
|
-
Str2Duck.parse(self)
|
8
|
-
else
|
9
|
-
return self
|
10
|
-
end
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
alias :duck :to_duck
|
15
|
-
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
Object.__send__ :include, Str2Duck::MPatch::ObjectEXT
|
data/lib/str2duck/regexp.rb
DELETED
@@ -1,90 +0,0 @@
|
|
1
|
-
#encoding: UTF-8
|
2
|
-
module Str2Duck
|
3
|
-
module Regexp
|
4
|
-
|
5
|
-
@@year = '\d+'
|
6
|
-
class << self
|
7
|
-
|
8
|
-
def datetime? obj
|
9
|
-
|
10
|
-
answer_value= nil
|
11
|
-
[
|
12
|
-
/^\w+, \d+ \w+ #{@@year} \d\d:\d\d:\d\d \+\d+$/,
|
13
|
-
/^-?#{@@year}-\d\d-\d\d\w\d\d:\d\d:\d\d\+\d\d:\d\d$/,
|
14
|
-
/\w+ \w+ \d+ #{@@year} \d+:\d+:\d+ \w+\+\d+ \(\w+\)/,
|
15
|
-
/^-?\d+-\d\d?-\d\d?\w\d\d?:\d\d?:\d\d?\w$/
|
16
|
-
].each do |regexp|
|
17
|
-
answer_value ||= obj =~ regexp
|
18
|
-
end
|
19
|
-
|
20
|
-
return !!answer_value
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
def date? obj
|
25
|
-
|
26
|
-
answer_value= nil
|
27
|
-
[
|
28
|
-
/^#{@@year}-\d\d-\d\d$/,
|
29
|
-
/^\w+, \d+ \w+ #{@@year}$/
|
30
|
-
].each do |regexp|
|
31
|
-
answer_value ||= obj =~ regexp
|
32
|
-
end
|
33
|
-
|
34
|
-
return !!answer_value
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
def time? obj
|
39
|
-
|
40
|
-
answer_value= nil
|
41
|
-
[
|
42
|
-
/^#{@@year}-\d\d-\d\d \d\d:\d\d:\d\d \+\d+$/
|
43
|
-
].each do |regexp|
|
44
|
-
answer_value ||= obj =~ regexp
|
45
|
-
end
|
46
|
-
|
47
|
-
return !!answer_value
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
def true? obj
|
52
|
-
return !!obj =~ /^true$/
|
53
|
-
end
|
54
|
-
|
55
|
-
def false? obj
|
56
|
-
return !!obj =~ /^false$/
|
57
|
-
end
|
58
|
-
|
59
|
-
def float? obj
|
60
|
-
case obj.to_s
|
61
|
-
when /^\d+\.\d+$/,/^\d+,\d+$/
|
62
|
-
return true
|
63
|
-
else
|
64
|
-
return false
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def integer? obj
|
69
|
-
return !!obj =~ /^\d+$/
|
70
|
-
end
|
71
|
-
|
72
|
-
def json? obj
|
73
|
-
begin
|
74
|
-
JSON.parse(obj)
|
75
|
-
return true
|
76
|
-
rescue ::JSON::ParserError
|
77
|
-
return false
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def yaml? obj
|
82
|
-
YAML.safe_load(obj)
|
83
|
-
return true
|
84
|
-
rescue ::Exception
|
85
|
-
return false
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|