simplifier 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +22 -0
- data/README.md +48 -0
- data/lib/simplifier.rb +77 -0
- data/script/bootstrap +9 -0
- data/script/release +39 -0
- data/script/test +11 -0
- data/simplifier.gemspec +16 -0
- data/test/simplifier_test.rb +89 -0
- metadata +72 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 John Barnette
|
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 NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Simplifier
|
2
|
+
|
3
|
+
A Ruby library for simplifying object graphs.
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
require "simplifier"
|
7
|
+
|
8
|
+
Simplifier.new.simplify({ :foo => [:bar, "baz", Time.now] })
|
9
|
+
# => {"foo"=>["bar", "baz", "2013-06-11T00:03:27Z"]}
|
10
|
+
```
|
11
|
+
|
12
|
+
## By default
|
13
|
+
|
14
|
+
- `nil`, `true`, and `false` are themselves.
|
15
|
+
- Symbols are simplified into interned, frozen Strings.
|
16
|
+
- Strings are UTF-8 with `\n` newlines.
|
17
|
+
- Strings in the `BINARY` encoding are themselves.
|
18
|
+
- Arrays and Sets are simplified into Arrays of simplified elements.
|
19
|
+
- Hashes are simplified into Hashes of simplified keys and values.
|
20
|
+
- Dates become an ISO 8601 `"yyyy-mm-dd"` String.
|
21
|
+
- DateTimes and Times become an ISO 8601 `"yyyy-mm-ddThh:mm:ssZ"` String.
|
22
|
+
- Any other types raise `Simplifier::Unknown`.
|
23
|
+
|
24
|
+
## Extending
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require "simplifier"
|
28
|
+
|
29
|
+
class APISimplifier < Simplifier
|
30
|
+
def simplify(object)
|
31
|
+
case object
|
32
|
+
when MyApp::User
|
33
|
+
simplify :id => object.id, :name => object.name
|
34
|
+
|
35
|
+
else
|
36
|
+
super
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
The constructor for `Simplifier` takes an optional configuration Hash and
|
43
|
+
assigns it to the `options` attribute. This can be useful if you need to
|
44
|
+
change the simplification of a type depending on embedding or other factors.
|
45
|
+
|
46
|
+
## Maintainer
|
47
|
+
|
48
|
+
[@jbarnette](https://github.com/jbarnette)
|
data/lib/simplifier.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require "set"
|
2
|
+
require "time"
|
3
|
+
|
4
|
+
# Public: Reduce Ruby objects to a more primitive representation.
|
5
|
+
class Simplifier
|
6
|
+
|
7
|
+
# Public: Raised when an object is simplified twice in one call.
|
8
|
+
class Circular < RuntimeError
|
9
|
+
def initialize(object)
|
10
|
+
super "Circular reference to #{object.class.name} #{object.inspect}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Public: Raised when an object can't be simplified.
|
15
|
+
class Unknown < RuntimeError
|
16
|
+
def initialize(object)
|
17
|
+
super "Can't simplify #{object.class.name} #{object.inspect}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Public: Get a Hash of Symbol-keyed options.
|
22
|
+
attr_reader :options
|
23
|
+
|
24
|
+
# Internal: A Hash cache of String representations of Symbols.
|
25
|
+
attr_reader :symbols
|
26
|
+
|
27
|
+
# Public: Create a new instance.
|
28
|
+
def initialize(options = nil)
|
29
|
+
@options = options || {}
|
30
|
+
@symbols = Hash.new { |h, k| h[k] = k.to_s.freeze }
|
31
|
+
end
|
32
|
+
|
33
|
+
# Internal.
|
34
|
+
def simplify(object)
|
35
|
+
case object
|
36
|
+
when Array, Set
|
37
|
+
object.map { |o| simplify o }
|
38
|
+
|
39
|
+
when DateTime
|
40
|
+
simplify object.to_time
|
41
|
+
|
42
|
+
when Date
|
43
|
+
object.iso8601
|
44
|
+
|
45
|
+
when Hash
|
46
|
+
{}.tap do |hash|
|
47
|
+
object.each do |key, value|
|
48
|
+
hash[simplify key] = simplify value
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
when NilClass
|
53
|
+
object
|
54
|
+
|
55
|
+
when Numeric
|
56
|
+
object
|
57
|
+
|
58
|
+
when String
|
59
|
+
return object if object.encoding == Encoding::BINARY
|
60
|
+
|
61
|
+
object.encode Encoding::UTF_8,
|
62
|
+
:invalid => :replace, :undef => :replace, :universal_newline => true
|
63
|
+
|
64
|
+
when Symbol
|
65
|
+
symbols[object]
|
66
|
+
|
67
|
+
when Time
|
68
|
+
object.utc.iso8601
|
69
|
+
|
70
|
+
when TrueClass, FalseClass
|
71
|
+
object
|
72
|
+
|
73
|
+
else
|
74
|
+
raise Unknown, object
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/script/bootstrap
ADDED
data/script/release
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
#!/bin/sh
|
2
|
+
# Tag and push a release.
|
3
|
+
|
4
|
+
set -e
|
5
|
+
|
6
|
+
# Make sure we're in the project root.
|
7
|
+
|
8
|
+
cd $(dirname "$0")/..
|
9
|
+
|
10
|
+
# Build a new gem archive.
|
11
|
+
|
12
|
+
rm -rf simplifier-*.gem
|
13
|
+
gem build -q simplifier.gemspec
|
14
|
+
|
15
|
+
# Make sure we're on the master branch.
|
16
|
+
|
17
|
+
(git branch | grep -q '* master') || {
|
18
|
+
echo "Only release from the master branch."
|
19
|
+
exit 1
|
20
|
+
}
|
21
|
+
|
22
|
+
# Figure out what version we're releasing.
|
23
|
+
|
24
|
+
tag=v`ls simplifier-*.gem | sed 's/^simplifier-\(.*\)\.gem$/\1/'`
|
25
|
+
|
26
|
+
# Make sure we haven't released this version before.
|
27
|
+
|
28
|
+
git fetch -t origin
|
29
|
+
|
30
|
+
(git tag -l | grep -q "$tag") && {
|
31
|
+
echo "Whoops, there's already a '${tag}' tag."
|
32
|
+
exit 1
|
33
|
+
}
|
34
|
+
|
35
|
+
# Tag it and bag it.
|
36
|
+
|
37
|
+
gem push simplifier-*.gem && git tag "$tag" &&
|
38
|
+
git push origin master && git push origin "$tag" &&
|
39
|
+
rm simplifier-*.gem
|
data/script/test
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/bin/sh
|
2
|
+
# Run the unit tests.
|
3
|
+
|
4
|
+
set -e
|
5
|
+
|
6
|
+
cd $(dirname "$0")/..
|
7
|
+
script/bootstrap && ruby -I lib -r rubygems \
|
8
|
+
-e 'require "bundler/setup"' \
|
9
|
+
-e 'require "minitest/autorun"' \
|
10
|
+
-e '(ARGV.empty? ? Dir["test/**/*_test.rb"] : ARGV).each { |f| load f }' \
|
11
|
+
-- "$@"
|
data/simplifier.gemspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |gem|
|
2
|
+
gem.name = "simplifier"
|
3
|
+
gem.version = "0.0.0"
|
4
|
+
gem.authors = ["John Barnette"]
|
5
|
+
gem.email = ["john@jbarnette.com"]
|
6
|
+
gem.description = "Reduce objects to a simpler representation."
|
7
|
+
gem.summary = "Reduce object graphs."
|
8
|
+
gem.homepage = "https://github.com/jbarnette/simplifier"
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split $/
|
11
|
+
gem.executables = []
|
12
|
+
gem.test_files = gem.files.grep /^test/
|
13
|
+
gem.require_paths = ["lib"]
|
14
|
+
|
15
|
+
gem.add_development_dependency "minitest"
|
16
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require "simplifier"
|
2
|
+
|
3
|
+
describe Simplifier do
|
4
|
+
before do
|
5
|
+
@simplifier = Simplifier.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it "can take initializer options" do
|
9
|
+
s = Simplifier.new :foo => :bar
|
10
|
+
assert_equal :bar, s.options[:foo]
|
11
|
+
end
|
12
|
+
|
13
|
+
it "raises when an object can't be simplified" do
|
14
|
+
assert_raises Simplifier::Unknown do
|
15
|
+
@simplifier.simplify Object.new
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "simplifies nil to nil" do
|
20
|
+
assert_nil @simplifier.simplify nil
|
21
|
+
end
|
22
|
+
|
23
|
+
it "simplifies false to false" do
|
24
|
+
assert_same false, @simplifier.simplify(false)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "simplifies true to true" do
|
28
|
+
assert_same true, @simplifier.simplify(true)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "simplifies Numerics to themselves" do
|
32
|
+
assert_equal 42, @simplifier.simplify(42)
|
33
|
+
assert_equal 4.2, @simplifier.simplify(4.2)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "simplifies Symbols to Strings" do
|
37
|
+
assert_equal "sym", @simplifier.simplify(:sym)
|
38
|
+
assert_same @simplifier.simplify(:sym), @simplifier.simplify(:sym)
|
39
|
+
assert @simplifier.simplify(:sym).frozen?
|
40
|
+
end
|
41
|
+
|
42
|
+
it "simplifies Strings to UTF-8" do
|
43
|
+
assert_equal "foo", @simplifier.simplify("foo")
|
44
|
+
refute_same "foo", @simplifier.simplify("foo")
|
45
|
+
end
|
46
|
+
|
47
|
+
it "normalizes line endings in Strings" do
|
48
|
+
assert_equal "foo\n", @simplifier.simplify("foo\r\n")
|
49
|
+
assert_equal "foo\n", @simplifier.simplify("foo\r")
|
50
|
+
end
|
51
|
+
|
52
|
+
it "doesn't mess with binary encoded Strings" do
|
53
|
+
blob = "foo".encode Encoding::BINARY
|
54
|
+
assert_same blob, @simplifier.simplify(blob)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "simplifies Arrays to Arrays of simplified objects" do
|
58
|
+
source = [:foo, [:bar, :baz], :quux]
|
59
|
+
expected = ["foo", ["bar", "baz"], "quux"]
|
60
|
+
|
61
|
+
assert_equal expected, @simplifier.simplify(source)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "simplifies Sets to Arrays of simplified objects" do
|
65
|
+
set = Set.new [:foo, :bar, :baz]
|
66
|
+
assert_equal %w(foo bar baz), @simplifier.simplify(set)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "simplifies Hashes by simplifying keys and values" do
|
70
|
+
source = { :foo => { :bar => [:baz, :quux] } }
|
71
|
+
expected = { "foo" => { "bar" => ["baz", "quux"] } }
|
72
|
+
|
73
|
+
assert_equal expected, @simplifier.simplify(source)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "simplifies Dates to ISO 8601 Strings" do
|
77
|
+
assert_equal "1999-03-03", @simplifier.simplify(Date.parse("1999-03-03"))
|
78
|
+
end
|
79
|
+
|
80
|
+
it "simplifies Times to UTC ISO 8601 Strings" do
|
81
|
+
now = Time.parse "1999-03-03 23:54:16 UTC"
|
82
|
+
assert_equal "1999-03-03T23:54:16Z", @simplifier.simplify(now)
|
83
|
+
end
|
84
|
+
|
85
|
+
it "simplifiest DateTimes to UTC ISO 8601 Strings" do
|
86
|
+
now = DateTime.parse "1999-03-03 23:54:16 UTC"
|
87
|
+
assert_equal "1999-03-03T23:54:16Z", @simplifier.simplify(now)
|
88
|
+
end
|
89
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simplifier
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- John Barnette
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: minitest
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
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: '0'
|
30
|
+
description: Reduce objects to a simpler representation.
|
31
|
+
email:
|
32
|
+
- john@jbarnette.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- lib/simplifier.rb
|
42
|
+
- script/bootstrap
|
43
|
+
- script/release
|
44
|
+
- script/test
|
45
|
+
- simplifier.gemspec
|
46
|
+
- test/simplifier_test.rb
|
47
|
+
homepage: https://github.com/jbarnette/simplifier
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.8.23
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Reduce object graphs.
|
71
|
+
test_files:
|
72
|
+
- test/simplifier_test.rb
|