saneitized 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +5 -0
- data/README.md +4 -4
- data/lib/saneitized.rb +2 -1
- data/lib/saneitized/converter.rb +29 -0
- data/lib/saneitized/hash.rb +1 -30
- data/lib/saneitized/version.rb +1 -1
- data/saneitized.gemspec +1 -0
- data/spec/saneitized/converter_spec.rb +43 -0
- data/spec/spec_helper.rb +2 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c94e5037a177dcba5a97198c0b29a9ebdf46013b
|
4
|
+
data.tar.gz: f140fd55b042f7255d908a42192ac21c8c4c5c22
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3347445524fa7cf0060ca9c915e9f23403dba14be1f42eb3bf1382b2342e1303126df8016c1ceb2c019e9846b9aefd873df3bbc0f8e9703c3da390245d664fb
|
7
|
+
data.tar.gz: 4ddd40e71917b2aeeb630534bfd2b7f78f2e357568f8e14923e2cc64424426f70caa418a9c293b8ad1643dd4bd21209267349fe7c634e2dd94d772f2a9ea010c
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
[![Code Climate](https://codeclimate.com/github/bguest/saneitized.png)](https://codeclimate.com/github/bguest/saneitized) [![Build Status](https://travis-ci.org/bguest/saneitized.png?branch=master)](https://travis-ci.org/bguest/saneitized) [![Coverage Status](https://coveralls.io/repos/bguest/saneitized/badge.png)](https://coveralls.io/r/bguest/saneitized)
|
1
|
+
[![Code Climate](https://codeclimate.com/github/bguest/saneitized.png)](https://codeclimate.com/github/bguest/saneitized) [![Build Status](https://travis-ci.org/bguest/saneitized.png?branch=master)](https://travis-ci.org/bguest/saneitized) [![Coverage Status](https://coveralls.io/repos/bguest/saneitized/badge.png)](https://coveralls.io/r/bguest/saneitized) [![Gem Version](https://badge.fury.io/rb/saneitized.png)](http://badge.fury.io/rb/saneitized) [![Dependency Status](https://gemnasium.com/bguest/saneitized.png)](https://gemnasium.com/bguest/saneitized)
|
2
2
|
|
3
3
|
# Saneitized
|
4
4
|
|
@@ -30,9 +30,9 @@ Or install it yourself as:
|
|
30
30
|
|
31
31
|
Use it like so
|
32
32
|
|
33
|
-
sane_hash =
|
34
|
-
|
35
|
-
|
33
|
+
sane_hash = Saneitized::Hash.new({:false => 'false',
|
34
|
+
:number => '10',
|
35
|
+
:float => '42.4'})
|
36
36
|
|
37
37
|
sane_hash[:false] #=> false
|
38
38
|
sane_hash[:number] #=> 10
|
data/lib/saneitized.rb
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Saneitized
|
2
|
+
def self.convert(unknown)
|
3
|
+
return Saneitized::Hash.new(unknown) if unknown.is_a? ::Hash
|
4
|
+
return unknown unless unknown.is_a? String #Only attempt to convert string
|
5
|
+
return true if unknown == 'true'
|
6
|
+
return false if unknown == 'false'
|
7
|
+
|
8
|
+
if value = Converter.integer?(unknown) then return value end
|
9
|
+
if value = Converter.float?(unknown) then return value end
|
10
|
+
|
11
|
+
unknown
|
12
|
+
end
|
13
|
+
|
14
|
+
module Converter
|
15
|
+
extend self
|
16
|
+
def integer?(unknown)
|
17
|
+
Integer(unknown)
|
18
|
+
rescue ArgumentError, TypeError
|
19
|
+
false
|
20
|
+
end
|
21
|
+
|
22
|
+
def float?(unknown)
|
23
|
+
Float(unknown)
|
24
|
+
rescue ArgumentError, TypeError
|
25
|
+
false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/lib/saneitized/hash.rb
CHANGED
@@ -5,38 +5,9 @@ module Saneitized
|
|
5
5
|
def initialize(hash)
|
6
6
|
super(hash)
|
7
7
|
hash.each do |key, value|
|
8
|
-
|
9
|
-
next if convert_to_true(key, value) # True
|
10
|
-
next if convert_to_false(key, value) # False
|
11
|
-
next if convert_to_integer(key,value) # Integer
|
12
|
-
next if convert_to_float(key, value) # Float
|
8
|
+
hash[key] = Saneitized.convert(value)
|
13
9
|
end
|
14
10
|
end
|
15
11
|
|
16
|
-
private
|
17
|
-
|
18
|
-
def convert_to_true(key, value)
|
19
|
-
(value == 'true' ? self[key] = true : false)
|
20
|
-
end
|
21
|
-
|
22
|
-
def convert_to_false(key, value)
|
23
|
-
(value == 'false' ? (self[key] = false; true) : false)
|
24
|
-
end
|
25
|
-
|
26
|
-
def convert_to_integer(key, value)
|
27
|
-
self[key] = Integer(value)
|
28
|
-
true
|
29
|
-
rescue ArgumentError, TypeError
|
30
|
-
false
|
31
|
-
end
|
32
|
-
|
33
|
-
def convert_to_float(key, value)
|
34
|
-
self[key] = Float(value)
|
35
|
-
true
|
36
|
-
rescue ArgumentError, TypeError
|
37
|
-
false
|
38
|
-
end
|
39
|
-
|
40
12
|
end
|
41
|
-
|
42
13
|
end
|
data/lib/saneitized/version.rb
CHANGED
data/saneitized.gemspec
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Saneitized do
|
4
|
+
describe '#convert' do
|
5
|
+
it 'should convert hash' do
|
6
|
+
unsullied = {:kick_ass => 'true', :cities_sacked => '3'}
|
7
|
+
sanitized = {:kick_ass => true, :cities_sacked => 3}
|
8
|
+
Saneitized.convert(unsullied).should == sanitized
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should change 'true' to true" do
|
12
|
+
Saneitized.convert('true').should == true
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should change 'false' to false" do
|
16
|
+
Saneitized.convert('false').should == false
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should change '12.34' to 12.34" do
|
20
|
+
Saneitized.convert('12.34').should == 12.34
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should not change 0.5 to 0.0' do
|
24
|
+
Saneitized.convert(0.5).should == 0.5
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should change integer string to integer' do
|
28
|
+
Saneitized.convert('12').kind_of?(Fixnum).should be_true
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should changer '12' to 12" do
|
32
|
+
Saneitized.convert('12').should == 12
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should do nothing to strings' do
|
36
|
+
Saneitized.convert('blah').should == 'blah'
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should do nothing to nil' do
|
40
|
+
Saneitized.convert(nil).should be_nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -8,6 +8,7 @@ Coveralls.wear!
|
|
8
8
|
#require 'bundler/setup'
|
9
9
|
|
10
10
|
require 'saneitized'
|
11
|
+
require 'pry'
|
11
12
|
#
|
12
13
|
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
13
14
|
RSpec.configure do |config|
|
@@ -25,4 +26,4 @@ end
|
|
25
26
|
# This file was generated by the `rspec --init` command. Conventionally, all
|
26
27
|
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
27
28
|
# Require this file using `require "spec_helper"` to ensure that it is only
|
28
|
-
# loaded once.
|
29
|
+
# loaded once.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: saneitized
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Guest
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-05-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -96,9 +96,11 @@ files:
|
|
96
96
|
- README.md
|
97
97
|
- Rakefile
|
98
98
|
- lib/saneitized.rb
|
99
|
+
- lib/saneitized/converter.rb
|
99
100
|
- lib/saneitized/hash.rb
|
100
101
|
- lib/saneitized/version.rb
|
101
102
|
- saneitized.gemspec
|
103
|
+
- spec/saneitized/converter_spec.rb
|
102
104
|
- spec/saneitized/hash_spec.rb
|
103
105
|
- spec/spec_helper.rb
|
104
106
|
homepage: https://github.com/bguest/saneitized
|
@@ -126,5 +128,6 @@ signing_key:
|
|
126
128
|
specification_version: 4
|
127
129
|
summary: Sanely converts string values to their ruby equivalent
|
128
130
|
test_files:
|
131
|
+
- spec/saneitized/converter_spec.rb
|
129
132
|
- spec/saneitized/hash_spec.rb
|
130
133
|
- spec/spec_helper.rb
|