rufus-json 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.txt CHANGED
@@ -2,6 +2,12 @@
2
2
  = rufus-json CHANGELOG.txt
3
3
 
4
4
 
5
+ == rufus-json - 1.0.1 released 2011/06/01
6
+
7
+ - require 'rufus-json/automatic' ('one require' rufus-json)
8
+ - Rufus::Json.load_backend(*order)
9
+
10
+
5
11
  == rufus-json - 1.0.0 released 2011/05/25
6
12
 
7
13
  - reaching 1.0.0 finally
data/LICENSE.txt CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- Copyright (c) 2009-2010, John Mettraux, jmettraux@gmail.com
2
+ Copyright (c) 2009-2011, John Mettraux, jmettraux@gmail.com
3
3
 
4
4
  Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  of this software and associated documentation files (the "Software"), to deal
data/README.rdoc CHANGED
@@ -13,9 +13,11 @@ One interface for various JSON Ruby backends.
13
13
  require 'rufus-json' # gem install rufus-json
14
14
 
15
15
  p Rufus::Json.decode('{"a":2,"b":true}')
16
+ p Rufus::Json.load('{"a":2,"b":true}')
16
17
  # => { 'a' => 2, 'b' => true }
17
18
 
18
19
  p Rufus::Json.encode({ 'a' => 2, 'b' => true })
20
+ p Rufus::Json.dump({ 'a' => 2, 'b' => true })
19
21
  # => '{"a":2,"b":true}'
20
22
 
21
23
 
@@ -45,6 +47,20 @@ There is a dup method, it may be useful in an all JSON system (flattening stuff
45
47
  o = Rufus::Json.dup(o)
46
48
 
47
49
 
50
+ === require 'rufus-json/automatic'
51
+
52
+ require 'rufus-json/automatic'
53
+
54
+ will require 'rufus-json' and load the first JSON lib available (in the order yajl, active_support, json, json/pure.
55
+
56
+ It is equivalent to
57
+
58
+ require 'rufus-json'
59
+ Rufus::Json.load_backend
60
+
61
+ (the .load_backend method accepts a list/order of backends to try).
62
+
63
+
48
64
  == rdoc
49
65
 
50
66
  http://rufus.rubyforge.org/rufus-json/
@@ -0,0 +1,3 @@
1
+
2
+ require 'rufus-json/automatic'
3
+
data/lib/rufus/json.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2009-2010, John Mettraux, jmettraux@gmail.com
2
+ # Copyright (c) 2009-2011, John Mettraux, jmettraux@gmail.com
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  # of this software and associated documentation files (the "Software"), to deal
@@ -26,7 +26,7 @@
26
26
  module Rufus
27
27
  module Json
28
28
 
29
- VERSION = '1.0.0'
29
+ VERSION = '1.0.1'
30
30
 
31
31
  # The JSON / JSON pure decoder
32
32
  #
@@ -69,6 +69,33 @@ module Json
69
69
  lambda { raise 'no JSON backend found' }
70
70
  ]
71
71
 
72
+ # In the given order, attempts to load a json lib and sets it as the
73
+ # backend of rufus-json.
74
+ #
75
+ # Returns the name of lib found if sucessful.
76
+ #
77
+ # Returns nil if no lib could be set.
78
+ #
79
+ # The default order / list of backends is yajl, active_support, json,
80
+ # json/pure. When specifying a custom order/list, unspecified backends
81
+ # won't be tried for.
82
+ #
83
+ def self.load_backend(*order)
84
+
85
+ order = %w[ yajl active_support json json/pure ] if order.empty?
86
+
87
+ order.each do |lib|
88
+ begin
89
+ require(lib)
90
+ Rufus::Json.backend = lib
91
+ return lib
92
+ rescue LoadError => le
93
+ end
94
+ end
95
+
96
+ nil
97
+ end
98
+
72
99
  # [Re-]Attempts to detect a JSON backend
73
100
  #
74
101
  def self.detect_backend
@@ -110,9 +137,12 @@ module Json
110
137
  #
111
138
  def self.backend=(b)
112
139
 
113
- if b.is_a?(Symbol)
114
- b = { :yajl => YAJL, :json => JSON, :active => ACTIVE, :none => NONE }[b]
115
- end
140
+ b = {
141
+ 'yajl' => YAJL, 'yajl-ruby' => YAJL,
142
+ 'json' => JSON, 'json-pure' => JSON,
143
+ 'active' => ACTIVE, 'active-support' => ACTIVE,
144
+ 'none' => NONE
145
+ }[b.to_s.gsub(/[_\/]/, '-')] if b.is_a?(String) or b.is_a?(Symbol)
116
146
 
117
147
  @backend = b
118
148
  end
@@ -0,0 +1,28 @@
1
+ #--
2
+ # Copyright (c) 2009-2011, John Mettraux, jmettraux@gmail.com
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ #
22
+ # Made in Japan.
23
+ #++
24
+
25
+
26
+ require 'rufus-json'
27
+ Rufus::Json.load_backend
28
+
data/test/backend_test.rb CHANGED
@@ -45,5 +45,37 @@ class BackendTest < Test::Unit::TestCase
45
45
 
46
46
  assert_equal :json, Rufus::Json.backend
47
47
  end
48
+
49
+ def test_set_backend_twist
50
+
51
+ require 'json/pure'
52
+
53
+ Rufus::Json.backend = 'json_pure'
54
+
55
+ assert_equal :json, Rufus::Json.backend
56
+ end
57
+
58
+ def test_load_backend
59
+
60
+ r = Rufus::Json.load_backend
61
+
62
+ assert_equal 'yajl', r
63
+ assert_equal :yajl, Rufus::Json.backend
64
+ end
65
+
66
+ def test_load_backend_with_different_order
67
+
68
+ r = Rufus::Json.load_backend('json', 'yajl')
69
+
70
+ assert_equal 'json', r
71
+ assert_equal :json, Rufus::Json.backend
72
+ end
73
+
74
+ def test_load_backend_with_missing_backend
75
+
76
+ r = Rufus::Json.load_backend('nada', 'nemo')
77
+
78
+ assert_nil r
79
+ end
48
80
  end
49
81
 
data/test/test.rb CHANGED
@@ -9,16 +9,26 @@ require 'rubygems'
9
9
 
10
10
  R = `which ruby`.strip
11
11
  P = File.dirname(__FILE__)
12
+ $result = ''
13
+
14
+ def do_test(command)
12
15
 
13
- %w[ json active_support yajl json/pure ].each do |lib|
14
16
  puts
15
17
  puts '-' * 80
16
- puts "#{R} #{P}/do_test.rb #{lib}"
17
- puts `#{R} #{P}/do_test.rb #{lib}`
18
+ puts command
19
+ puts `#{command}`
20
+
21
+ $result << ($?.exitstatus == 0 ? 'o' : 'X')
18
22
  end
19
23
 
24
+ %w[ json active_support yajl json/pure ].each do |lib|
25
+ do_test "#{R} #{P}/do_test.rb #{lib}"
26
+ end
27
+
28
+ do_test "#{R} #{P}/backend_test.rb"
29
+
30
+ puts
31
+ puts
32
+ puts $result
20
33
  puts
21
- puts '-' * 80
22
- puts "#{R} #{P}/backend_test.rb"
23
- puts `#{R} #{P}/backend_test.rb`
24
34
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rufus-json
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 0
10
- version: 1.0.0
9
+ - 1
10
+ version: 1.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Mettraux
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-05-25 00:00:00 +09:00
19
+ date: 2011-06-01 00:00:00 +09:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -100,7 +100,9 @@ extra_rdoc_files: []
100
100
 
101
101
  files:
102
102
  - Rakefile
103
+ - lib/rufus/json/automatic.rb
103
104
  - lib/rufus/json.rb
105
+ - lib/rufus-json/automatic.rb
104
106
  - lib/rufus-json.rb
105
107
  - test/backend_test.rb
106
108
  - test/do_test.rb