str2hash 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +21 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +69 -0
- data/Rakefile +5 -0
- data/lib/str2hash/version.rb +4 -0
- data/lib/str2hash.rb +15 -3
- data/spec/str2hash_spec.rb +79 -0
- data/str2hash.gemspec +29 -0
- metadata +86 -5
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 karl l
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
str2hash
|
2
|
+
========
|
3
|
+
|
4
|
+
Tiny parser for converting strings to hashes (without using eval).
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
str2hash adds String#to_h
|
9
|
+
|
10
|
+
require 'str2hash'
|
11
|
+
|
12
|
+
"{ :foo => 123 }".to_h => {:foo=>123}
|
13
|
+
"{ :foo => -123 }".to_h => {:foo=>-123}
|
14
|
+
"{ :foo => :bar }".to_h => {:foo=>:bar}
|
15
|
+
"{ :foo => 123.456 }".to_h => {:foo=>123.456}
|
16
|
+
"{ :foo => -123.456 }".to_h => {:foo=>-123.456}
|
17
|
+
"{ :foo => 0x0123 }".to_h => {:foo=>291}
|
18
|
+
"{ :foo => -0x0123 }".to_h => {:foo=>-291}
|
19
|
+
"{ :foo => 0b010010010100010101}".to_h => {:foo=>75029}
|
20
|
+
"{ :foo => -0b010010010100010101}".to_h => {:foo=>-75029}
|
21
|
+
"{ 123 => 456 }".to_h => {123=>456}
|
22
|
+
"{ foo: 456 }".to_h => {:foo=>456}
|
23
|
+
"{ foo: :bar }".to_h => {:foo=>:bar}
|
24
|
+
"{ foo: '123' }".to_h => {:foo=>"123"}
|
25
|
+
"{ foo: "123" }".to_h => {:foo=>"123"}
|
26
|
+
"{ foo: /foo/ }".to_h => {:foo=>/foo/}
|
27
|
+
"{ foo: true }".to_h => {:foo=>true}
|
28
|
+
"{ :foo => :bar, :foo2 => :bar2}".to_h => {:foo=>:bar, :foo2=>:bar2}
|
29
|
+
":foo => :bar, :foo2 => :bar2".to_h => {:foo=>:bar, :foo2=>:bar2}
|
30
|
+
|
31
|
+
## Limitations
|
32
|
+
|
33
|
+
No nested hashes, no arrays (yet). No bindings, only values.
|
34
|
+
|
35
|
+
|
36
|
+
## Version information
|
37
|
+
|
38
|
+
* 0.1.0 - Gem setup, Documentation
|
39
|
+
* 0.0.2 - Minor fixes
|
40
|
+
* 0.0.1 - Initial version
|
41
|
+
|
42
|
+
|
43
|
+
## License
|
44
|
+
|
45
|
+
MIT License:
|
46
|
+
|
47
|
+
Copyright (c) 2013 karl l, <karl@ninjacontrol.com>
|
48
|
+
|
49
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
50
|
+
of this software and associated documentation files (the "Software"), to
|
51
|
+
deal in the Software without restriction, including without limitation the
|
52
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
53
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
54
|
+
furnished to do so, subject to the following conditions:
|
55
|
+
|
56
|
+
The above copyright notice and this permission notice shall be included in
|
57
|
+
all copies or substantial portions of the Software.
|
58
|
+
|
59
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
60
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
61
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
62
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
63
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
64
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
65
|
+
IN THE SOFTWARE.
|
66
|
+
|
67
|
+
## Author
|
68
|
+
|
69
|
+
karl l (karl@ninjacontrol.com), 2013
|
data/Rakefile
ADDED
data/lib/str2hash.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
require 'parslet'
|
2
2
|
|
3
|
-
#
|
4
|
-
# karlll <karl@ninjacontrol.com>, 2013-03-23
|
5
|
-
|
3
|
+
# @author karl l <karl@ninjacontrol.com>
|
6
4
|
class HashParse < Parslet::Parser
|
7
5
|
|
8
6
|
rule(:lcbracket) { str('{') >> sp? }
|
@@ -88,6 +86,7 @@ class HashParse < Parslet::Parser
|
|
88
86
|
|
89
87
|
end
|
90
88
|
|
89
|
+
# @author karl l <karl@ninjacontrol.com>
|
91
90
|
class HashTransform < Parslet::Transform
|
92
91
|
|
93
92
|
rule(:dec_int => simple(:i)) { Integer(i) }
|
@@ -120,13 +119,26 @@ class HashTransform < Parslet::Transform
|
|
120
119
|
end
|
121
120
|
|
122
121
|
|
122
|
+
# Defines String#to_h
|
123
|
+
# @author karl l <karl@ninjacontrol.com>
|
123
124
|
module Str2Hash
|
124
125
|
|
126
|
+
# @deprecated
|
125
127
|
def str_to_hash(str)
|
126
128
|
|
127
129
|
end
|
128
130
|
|
129
131
|
module String
|
132
|
+
|
133
|
+
#
|
134
|
+
# Converts the string to a hash representation
|
135
|
+
# @return [Hash] Hash representation of the string
|
136
|
+
# @raise [Parslet::ParseFailed] If parsing of the string fails
|
137
|
+
#
|
138
|
+
# @example
|
139
|
+
# "{ :foo => 'bar'}".to_h
|
140
|
+
# => {:foo=>"bar"}
|
141
|
+
#
|
130
142
|
def to_h
|
131
143
|
|
132
144
|
p = HashParse.new
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'str2hash'
|
3
|
+
require 'parslet/convenience'
|
4
|
+
|
5
|
+
|
6
|
+
PRODS = [
|
7
|
+
{s: "{ :foo => 123 }", h: {:foo => 123}},
|
8
|
+
{s: "{ :foo => -123 }", h: {:foo => -123}},
|
9
|
+
{s: "{ :foo => :bar }", h: {:foo => :bar}},
|
10
|
+
{s: "{ :foo => 123.456 }", h: {:foo => 123.456}},
|
11
|
+
{s: "{ :foo => -123.456 }", h: {:foo => -123.456}},
|
12
|
+
{s: "{ :foo => 0x0123 }", h: {:foo => 291}},
|
13
|
+
{s: "{ :foo => 0X0123 }", h: {:foo => 291}},
|
14
|
+
{s: "{ :foo => -0x0123 }", h: {:foo => -291}},
|
15
|
+
{s: "{ :foo => -0X0123 }", h: {:foo => -291}},
|
16
|
+
{s: "{ :foo => 0b010010010100010101}", h: {:foo => 75029}},
|
17
|
+
{s: "{ :foo => 0B010010010100010101}", h: {:foo => 75029}},
|
18
|
+
{s: "{ :foo => -0b010010010100010101}", h: {:foo => -75029}},
|
19
|
+
{s: "{ :foo => -0B010010010100010101}", h: {:foo => -75029}},
|
20
|
+
{s: "{ 123 => 456 }", h: {123 => 456}},
|
21
|
+
{s: "{ foo: 456 }", h: {:foo => 456}},
|
22
|
+
{s: "{ foo: :bar }", h: {:foo => :bar}},
|
23
|
+
{s: "{ foo: '123' }", h: {:foo => '123'}},
|
24
|
+
{s: "{ foo: \"123\" }", h: {:foo => "123"}},
|
25
|
+
{s: "{ foo: /foo/ }", h: {:foo => /foo/}},
|
26
|
+
{s: "{ foo: true }", h: {:foo => true}},
|
27
|
+
{s: "{ :foo => :bar, :foo2 => :bar2}", h: {:foo => :bar, :foo2 => :bar2}},
|
28
|
+
{s: ":foo => :bar, :foo2 => :bar2", h: {:foo => :bar, :foo2 => :bar2}}
|
29
|
+
]
|
30
|
+
|
31
|
+
describe HashParse do
|
32
|
+
|
33
|
+
|
34
|
+
it 'should parse a basic value hash' do
|
35
|
+
p = HashParse.new
|
36
|
+
|
37
|
+
PRODS.each do |item|
|
38
|
+
p.parse(item[:s])
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
describe HashTransform do
|
46
|
+
|
47
|
+
it 'should parse and transform a basic value hash' do
|
48
|
+
p = HashParse.new
|
49
|
+
t = HashTransform.new
|
50
|
+
|
51
|
+
PRODS.each do |item|
|
52
|
+
|
53
|
+
s = item[:s]
|
54
|
+
h = t.apply(p.parse(s))
|
55
|
+
h.should eq(item[:h])
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
describe "String to Hash" do
|
64
|
+
|
65
|
+
it 'should Convert a string to corresponding hash' do
|
66
|
+
|
67
|
+
PRODS.each do |item|
|
68
|
+
s = item[:s]
|
69
|
+
h = s.to_h
|
70
|
+
h.should eq(item[:h])
|
71
|
+
|
72
|
+
puts "\"#{s}\".to_h => #{h}"
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
|
data/str2hash.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'str2hash/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
|
8
|
+
spec.name = "str2hash"
|
9
|
+
spec.version = Str2Hash::VERSION
|
10
|
+
spec.authors = ["karl l"]
|
11
|
+
spec.email = ["karl@ninjacontrol.com"]
|
12
|
+
spec.date = '2013-03-29'
|
13
|
+
spec.summary = "Convert string to hash"
|
14
|
+
spec.description = "Tiny parser for converting strings to hashes (without using eval)."
|
15
|
+
spec.homepage = "https://github.com/karlll/str2hash/"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files`.split($/)
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_runtime_dependency "parslet"
|
27
|
+
|
28
|
+
end
|
29
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: str2hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,16 +10,90 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
date: 2013-03-29 00:00:00.000000000 Z
|
13
|
-
dependencies:
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: parslet
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
14
78
|
description: Tiny parser for converting strings to hashes (without using eval).
|
15
|
-
email:
|
79
|
+
email:
|
80
|
+
- karl@ninjacontrol.com
|
16
81
|
executables: []
|
17
82
|
extensions: []
|
18
83
|
extra_rdoc_files: []
|
19
84
|
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
20
90
|
- lib/str2hash.rb
|
91
|
+
- lib/str2hash/version.rb
|
92
|
+
- spec/str2hash_spec.rb
|
93
|
+
- str2hash.gemspec
|
21
94
|
homepage: https://github.com/karlll/str2hash/
|
22
|
-
licenses:
|
95
|
+
licenses:
|
96
|
+
- MIT
|
23
97
|
post_install_message:
|
24
98
|
rdoc_options: []
|
25
99
|
require_paths:
|
@@ -30,16 +104,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
30
104
|
- - ! '>='
|
31
105
|
- !ruby/object:Gem::Version
|
32
106
|
version: '0'
|
107
|
+
segments:
|
108
|
+
- 0
|
109
|
+
hash: -2288426739317148869
|
33
110
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
111
|
none: false
|
35
112
|
requirements:
|
36
113
|
- - ! '>='
|
37
114
|
- !ruby/object:Gem::Version
|
38
115
|
version: '0'
|
116
|
+
segments:
|
117
|
+
- 0
|
118
|
+
hash: -2288426739317148869
|
39
119
|
requirements: []
|
40
120
|
rubyforge_project:
|
41
121
|
rubygems_version: 1.8.24
|
42
122
|
signing_key:
|
43
123
|
specification_version: 3
|
44
124
|
summary: Convert string to hash
|
45
|
-
test_files:
|
125
|
+
test_files:
|
126
|
+
- spec/str2hash_spec.rb
|