str2duck 1.0.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 +15 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +18 -0
- data/LICENSE +20 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/VERSION +1 -0
- data/dump/find_regexp.rb +19 -0
- data/examples/sample.rb +43 -0
- data/files.rb +24 -0
- data/lib/str2duck/format.rb +78 -0
- data/lib/str2duck/parser.rb +16 -0
- data/lib/str2duck/regexp.rb +65 -0
- data/lib/str2duck/string.rb +8 -0
- data/lib/str2duck/support.rb +17 -0
- data/lib/str2duck.rb +7 -0
- data/str2duck.gemspec +24 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MmRlMGM0ZGZhNmI3ZWZkOTQ3ZTVhYjJjYjcxYTIxMWZhMWQxMTc1Ng==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NjViNmI0MjdjOWQ1ZTBmNjYyYmY4Nzc2N2MxZGMxNzJiMGNmN2U1YQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MDMzZTZkZDBjNzY2OTA1YjhhYzg0MDJjN2NjYWJjOWQ2ZmY5YzM5YTc0N2M4
|
10
|
+
MjM2M2Y4NGNkMDY1NmU0ZmY0ZTFhZTYwNTAxNTQ4OWFmY2VlYWJlZDEyNWJj
|
11
|
+
NDEwOWZmNjY1NmMxNTZmZGEzMDgxYTdmM2ZlMWRkYzFmZmM1YTE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
OWUyMGU4NTM4MzBjMDIxYjE0OTM2OTc3ZDI5ZGRmZjI0OTNjZTYxMzE3OTQ4
|
14
|
+
MzMzNzQ1YmI1OGI5YWVkN2RhNjBkOWQ5NzAxMWIyMzhhMTFlMDc0MGUwOTU0
|
15
|
+
NjRmOTFhM2UyNzQ1MjUwNDg2NGI0OTBkY2NmNTM4ZTM0YWJjNmE=
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Adam Luzsi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
str2duck
|
2
|
+
========
|
3
|
+
|
4
|
+
String to Duck type parser
|
5
|
+
It can parse int, float, time, date . datetime, booleans etc from string
|
6
|
+
|
7
|
+
Possible bug source, not yet been tested is the american time format.
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
|
11
|
+
require 'str2duck'
|
12
|
+
|
13
|
+
"2011-03-12".duck #> Date obj
|
14
|
+
"false".duck #> False obj
|
15
|
+
"123".duck #> Integer obj
|
16
|
+
"123.123".duck #> Float obj
|
17
|
+
"2012-09-12T14:49:50+02:00".duck #> Time obj
|
18
|
+
|
19
|
+
```
|
20
|
+
|
21
|
+
it is possible ot extend the Duck flexibility be require the Active Supports time extension,
|
22
|
+
|
23
|
+
simple like that:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require File.join 'active_support','time'
|
27
|
+
```
|
28
|
+
|
29
|
+
This will give you even more flexibility in terms of use :)
|
30
|
+
like:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
|
34
|
+
"Fri, 25 Jan 2013 20:02:15 +0100".duck #> DateTime obj
|
35
|
+
"Sun, 28 Aug 2005".duck #> Date obj
|
36
|
+
|
37
|
+
```
|
38
|
+
|
39
|
+
Happy parsing!
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join "bundler","gem_tasks"
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/dump/find_regexp.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#begin
|
2
|
+
# string_input= string_data_obj
|
3
|
+
# contain= nil
|
4
|
+
# ["[", "]","^","$","*","/"].each do |one_sym|
|
5
|
+
# if string_input.include? one_sym
|
6
|
+
# contain ||= true
|
7
|
+
# end
|
8
|
+
# end
|
9
|
+
# if contain == true
|
10
|
+
# string_regexp= Regexp.new(string_input).inspect#.gsub('\\','')
|
11
|
+
# string_regexp= string_regexp[1..(string_regexp.length-2)]
|
12
|
+
# if string_input == string_regexp
|
13
|
+
# return Regexp.new(string_input)
|
14
|
+
# else
|
15
|
+
# raise ArgumentError,"invalid input string"
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
#rescue ArgumentError
|
19
|
+
#end
|
data/examples/sample.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require_relative "../lib/str2duck"
|
2
|
+
|
3
|
+
[
|
4
|
+
"2011-03-12",
|
5
|
+
"2007-07-20 18:59:27 +0200",
|
6
|
+
"2010-10-30 18:02:56 +0200",
|
7
|
+
"2012-09-12T14:49:50+02:00",
|
8
|
+
"123",
|
9
|
+
"asd",
|
10
|
+
"123.432",
|
11
|
+
"true",
|
12
|
+
"false",
|
13
|
+
"some string data"
|
14
|
+
].each do |object|
|
15
|
+
puts object.duck,object.duck.class,""
|
16
|
+
end
|
17
|
+
|
18
|
+
begin
|
19
|
+
|
20
|
+
require File.join 'active_support','time'
|
21
|
+
|
22
|
+
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"
|
23
|
+
|
24
|
+
[
|
25
|
+
"2011-03-12",
|
26
|
+
"2007-07-20 18:59:27 +0200",
|
27
|
+
"2010-10-30 18:02:56 +0200",
|
28
|
+
"2012-09-12T14:49:50+02:00",
|
29
|
+
"123",
|
30
|
+
"asd",
|
31
|
+
"123.432",
|
32
|
+
"true",
|
33
|
+
"false",
|
34
|
+
"some string data"
|
35
|
+
].each do |object|
|
36
|
+
puts object.duck,object.duck.class,""
|
37
|
+
end
|
38
|
+
|
39
|
+
puts "Sun, 28 Aug 2005".duck.class
|
40
|
+
puts "Fri, 25 Jan 2013 20:02:15 +0100".duck.class
|
41
|
+
|
42
|
+
rescue LoadError
|
43
|
+
end
|
data/files.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
### Get Files from dir
|
2
|
+
begin
|
3
|
+
|
4
|
+
files_to_be_loaded = %w[version.rb]
|
5
|
+
|
6
|
+
SpecFiles= Array.new
|
7
|
+
|
8
|
+
Dir[File.expand_path(File.join(File.dirname(__FILE__),"**","*"))].sort.uniq.each do |one_file_name|
|
9
|
+
one_file_name = File.expand_path one_file_name
|
10
|
+
file_name = one_file_name[(File.expand_path(File.dirname(__FILE__)).to_s.length+1)..(one_file_name.length-1)]
|
11
|
+
|
12
|
+
if !one_file_name.include?("pkg")
|
13
|
+
if !File.directory? file_name
|
14
|
+
SpecFiles.push file_name
|
15
|
+
STDOUT.puts file_name if $DEBUG
|
16
|
+
if files_to_be_loaded.include? one_file_name.split(File::SEPARATOR).last
|
17
|
+
load one_file_name
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
module Str2Duck
|
3
|
+
module Format
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def datetime obj
|
7
|
+
if Str2Duck::Regexp.datetime?(obj)
|
8
|
+
if defined? DateTime
|
9
|
+
return DateTime.parse obj
|
10
|
+
else
|
11
|
+
time_parts= obj.scan(/\d+/).map(&:to_i)
|
12
|
+
if time_parts.count == 8
|
13
|
+
2.times{time_parts.pop}
|
14
|
+
elsif time_parts.count == 6
|
15
|
+
return nil
|
16
|
+
end
|
17
|
+
return Time.new(*time_parts)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def date obj
|
24
|
+
if Str2Duck::Regexp.date?(obj)
|
25
|
+
begin
|
26
|
+
return Date.parse obj
|
27
|
+
rescue NoMethodError
|
28
|
+
time_parts= obj.scan(/\d+/).map(&:to_i)
|
29
|
+
return Time.new(*time_parts)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
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
|
45
|
+
nil
|
46
|
+
end
|
47
|
+
|
48
|
+
def true obj
|
49
|
+
if Str2Duck::Regexp.true?(obj)
|
50
|
+
return true
|
51
|
+
end
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
|
55
|
+
def false obj
|
56
|
+
if Str2Duck::Regexp.false?(obj)
|
57
|
+
return false
|
58
|
+
end
|
59
|
+
nil
|
60
|
+
end
|
61
|
+
|
62
|
+
def float obj
|
63
|
+
if Str2Duck::Regexp.float?(obj)
|
64
|
+
return obj.to_f
|
65
|
+
end
|
66
|
+
nil
|
67
|
+
end
|
68
|
+
|
69
|
+
def integer obj
|
70
|
+
if Str2Duck::Regexp.integer?(obj)
|
71
|
+
return obj.to_i
|
72
|
+
end
|
73
|
+
nil
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
module Str2Duck
|
3
|
+
|
4
|
+
def self.parse string_data_obj
|
5
|
+
raise ArgumentError,"invalid input, must be string!" if string_data_obj.class != String
|
6
|
+
return_value= nil
|
7
|
+
|
8
|
+
Str2Duck::Format.singleton_methods.each do |method_name|
|
9
|
+
return_value ||= Str2Duck::Format.__send__ method_name, string_data_obj
|
10
|
+
end
|
11
|
+
|
12
|
+
return_value ||= string_data_obj
|
13
|
+
return return_value
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
module Str2Duck
|
3
|
+
module Regexp
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def datetime? obj
|
7
|
+
|
8
|
+
answer_value= nil
|
9
|
+
[
|
10
|
+
/^\w+, \d+ \w+ \d\d\d\d \d\d:\d\d:\d\d \+\d+$/,
|
11
|
+
/^\d\d\d\d-\d\d-\d\d\w\d\d:\d\d:\d\d\+\d\d:\d\d$/
|
12
|
+
].each do |regexp|
|
13
|
+
answer_value ||= obj =~ regexp
|
14
|
+
end
|
15
|
+
|
16
|
+
return Str2Duck.return_value_parse answer_value
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
def date? obj
|
21
|
+
|
22
|
+
answer_value= nil
|
23
|
+
[
|
24
|
+
/^\d\d\d\d-\d\d-\d\d$/,
|
25
|
+
/^\w+, \d+ \w+ \d\d\d\d$/
|
26
|
+
].each do |regexp|
|
27
|
+
answer_value ||= obj =~ regexp
|
28
|
+
end
|
29
|
+
|
30
|
+
return Str2Duck.return_value_parse answer_value
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
def time? obj
|
35
|
+
|
36
|
+
answer_value= nil
|
37
|
+
[
|
38
|
+
/^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d \+\d+$/
|
39
|
+
].each do |regexp|
|
40
|
+
answer_value ||= obj =~ regexp
|
41
|
+
end
|
42
|
+
|
43
|
+
return Str2Duck.return_value_parse answer_value
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
def true? obj
|
48
|
+
return Str2Duck.return_value_parse obj =~ /^true$/
|
49
|
+
end
|
50
|
+
|
51
|
+
def false? obj
|
52
|
+
return Str2Duck.return_value_parse obj =~ /^true$/
|
53
|
+
end
|
54
|
+
|
55
|
+
def float? obj
|
56
|
+
return Str2Duck.return_value_parse obj =~ /^\d+\.\d+$/
|
57
|
+
end
|
58
|
+
|
59
|
+
def integer? obj
|
60
|
+
return Str2Duck.return_value_parse obj =~ /^\d+$/
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/str2duck.rb
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
|
3
|
+
require File.join File.dirname(__FILE__),'str2duck','support'
|
4
|
+
require File.join File.dirname(__FILE__),'str2duck','format'
|
5
|
+
require File.join File.dirname(__FILE__),'str2duck','regexp'
|
6
|
+
require File.join File.dirname(__FILE__),'str2duck','parser'
|
7
|
+
require File.join File.dirname(__FILE__),'str2duck','string'
|
data/str2duck.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__),"files.rb"))
|
4
|
+
|
5
|
+
### Specification for the new Gem
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
|
8
|
+
spec.name = "str2duck"
|
9
|
+
spec.version = File.open(File.join(File.dirname(__FILE__),"VERSION")).read.split("\n")[0].chomp.gsub(' ','')
|
10
|
+
spec.authors = ["Adam Luzsi"]
|
11
|
+
spec.email = ["adamluzsi@gmail.com"]
|
12
|
+
spec.description = %q{ Parse string into obj }
|
13
|
+
spec.summary = %q{ String to Obj, Duck type parser }
|
14
|
+
spec.homepage = "https://github.com/adamluzsi/str2duck"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = SpecFiles
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
#spec.add_dependency 'active_support'
|
23
|
+
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: str2duck
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Luzsi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-27 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: ! ' Parse string into obj '
|
14
|
+
email:
|
15
|
+
- adamluzsi@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- Gemfile
|
21
|
+
- Gemfile.lock
|
22
|
+
- LICENSE
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- VERSION
|
26
|
+
- dump/find_regexp.rb
|
27
|
+
- examples/sample.rb
|
28
|
+
- files.rb
|
29
|
+
- lib/str2duck.rb
|
30
|
+
- lib/str2duck/format.rb
|
31
|
+
- lib/str2duck/parser.rb
|
32
|
+
- lib/str2duck/regexp.rb
|
33
|
+
- lib/str2duck/string.rb
|
34
|
+
- lib/str2duck/support.rb
|
35
|
+
- str2duck.gemspec
|
36
|
+
homepage: https://github.com/adamluzsi/str2duck
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata: {}
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 2.2.1
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: String to Obj, Duck type parser
|
60
|
+
test_files: []
|