saneitized 0.0.1
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 +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +49 -0
- data/Rakefile +8 -0
- data/lib/saneitized/hash.rb +42 -0
- data/lib/saneitized/version.rb +3 -0
- data/lib/saneitized.rb +2 -0
- data/saneitized.gemspec +26 -0
- data/spec/saneitized/hash_spec.rb +39 -0
- data/spec/spec_helper.rb +28 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a9939590236fcff2034eff57303b477d658dcfdb
|
4
|
+
data.tar.gz: 13a96f2ca58e3ec4ec5eaf73e2e3d15827a30aa8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1a71716d2d887422ba2643bd43ed6f5234ae2935ad5795bc82a9fef0d0ddaf92fc74a68dbbd197d30a35fe6a133d2ca70ec472360c74fbb421e45c1ef198e98d
|
7
|
+
data.tar.gz: 3ab450bb028dad258384ff6aff5a189f9c0cb626c858f68511951853cf36a5de4c45ddab2be2a5f7e807c266b8b7590c80d62cc766a77c48f69bfdcae488c0ed
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Benjamin Guest
|
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,49 @@
|
|
1
|
+
[](https://codeclimate.com/github/bguest/saneitized) [](https://travis-ci.org/bguest/saneitized) [](https://coveralls.io/r/bguest/saneitized)
|
2
|
+
|
3
|
+
# Saneitized
|
4
|
+
|
5
|
+
Saneitized takes strings turns those values into their sane ruby equivalents. For example how many times have you done something like the following
|
6
|
+
|
7
|
+
hash = JSON.parse("{\"should_explode\":\"false\"}")
|
8
|
+
|
9
|
+
hash['should_explode'] #=> 'false'
|
10
|
+
|
11
|
+
if hash['should_explode']
|
12
|
+
explode_all_the_bombs #=> 'Bombs are exploding'
|
13
|
+
end
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
gem 'saneitized'
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install saneitized
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
Use it like so
|
32
|
+
|
33
|
+
sane_hash = Saneitize::Hash.new({:false => 'false',
|
34
|
+
:number => '10',
|
35
|
+
:float => '42.4'})
|
36
|
+
|
37
|
+
sane_hash[:false] #=> false
|
38
|
+
sane_hash[:number] #=> 10
|
39
|
+
sane_hash[:float] #=> 42.4
|
40
|
+
|
41
|
+
See the specs for more examples.
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it ( http://github.com/<my-github-username>/saneitized_hash/fork )
|
46
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module Saneitized
|
2
|
+
|
3
|
+
class Hash < SimpleDelegator
|
4
|
+
|
5
|
+
def initialize(hash)
|
6
|
+
super(hash)
|
7
|
+
hash.each do |key, value|
|
8
|
+
next unless value.is_a? String # Only attempt to convert strings
|
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
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
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
|
+
end
|
41
|
+
|
42
|
+
end
|
data/lib/saneitized.rb
ADDED
data/saneitized.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'saneitized/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'saneitized'
|
8
|
+
spec.version = Saneitized::VERSION
|
9
|
+
spec.authors = ['Benjamin Guest']
|
10
|
+
spec.email = ['benguest@gmail.com']
|
11
|
+
spec.summary = %q{Sanely converts string values to their ruby equivalent}
|
12
|
+
spec.description = %q{Converts ruby hash values from strings to fixnums, floats, true and false values if it can sanely do so.}
|
13
|
+
spec.homepage = 'https://github.com/bguest/saneitized'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~>1.5'
|
22
|
+
spec.add_development_dependency 'rspec', '~>2.14'
|
23
|
+
spec.add_development_dependency 'rake', '~>10.1'
|
24
|
+
spec.add_development_dependency 'simplecov', '~>0.8'
|
25
|
+
spec.add_development_dependency 'coveralls', '~>0.7'
|
26
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Saneitized::Hash do
|
4
|
+
|
5
|
+
describe '#new' do
|
6
|
+
it "should change 'true' to true" do
|
7
|
+
Saneitized::Hash.new({:true => 'true'})[:true].should == true
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should change 'false' to false" do
|
11
|
+
Saneitized::Hash.new({:false => 'false'})[:false].should == false
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should change '12.34' to 12.34" do
|
15
|
+
Saneitized::Hash.new({value: '12.34'})[:value].should == 12.34
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should not change 0.5 to 0.0' do
|
19
|
+
Saneitized::Hash.new({value: 0.5})[:value].should == 0.5
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should change integer string to integer' do
|
23
|
+
Saneitized::Hash.new({int: '12'})[:int].kind_of?(Fixnum).should be_true
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should changer '12' to 12" do
|
27
|
+
Saneitized::Hash.new({int: '12'})[:int].should == 12
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should do nothing to strings' do
|
31
|
+
Saneitized::Hash.new({string: 'blah'})[:string].should == 'blah'
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should do nothing to nil' do
|
35
|
+
Saneitized::Hash.new({nill: nil})[:nill].should be_nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
3
|
+
|
4
|
+
require 'coveralls'
|
5
|
+
Coveralls.wear!
|
6
|
+
|
7
|
+
#require 'rubygems'
|
8
|
+
#require 'bundler/setup'
|
9
|
+
|
10
|
+
require 'saneitized'
|
11
|
+
#
|
12
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
13
|
+
RSpec.configure do |config|
|
14
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
15
|
+
config.run_all_when_everything_filtered = true
|
16
|
+
config.filter_run :focus
|
17
|
+
|
18
|
+
# Run specs in random order to surface order dependencies. If you find an
|
19
|
+
# order dependency and want to debug it, you can fix the order by providing
|
20
|
+
# the seed, which is printed after each run.
|
21
|
+
# --seed 1234
|
22
|
+
config.order = 'random'
|
23
|
+
end
|
24
|
+
|
25
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
26
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
27
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
28
|
+
# loaded once.
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: saneitized
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Benjamin Guest
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.14'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.14'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.8'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.8'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: coveralls
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.7'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.7'
|
83
|
+
description: Converts ruby hash values from strings to fixnums, floats, true and false
|
84
|
+
values if it can sanely do so.
|
85
|
+
email:
|
86
|
+
- benguest@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- lib/saneitized.rb
|
99
|
+
- lib/saneitized/hash.rb
|
100
|
+
- lib/saneitized/version.rb
|
101
|
+
- saneitized.gemspec
|
102
|
+
- spec/saneitized/hash_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
homepage: https://github.com/bguest/saneitized
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.2.0
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Sanely converts string values to their ruby equivalent
|
128
|
+
test_files:
|
129
|
+
- spec/saneitized/hash_spec.rb
|
130
|
+
- spec/spec_helper.rb
|