jsont 0.1.0

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.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-11-25
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/jsont.rb
7
+ script/console
8
+ script/destroy
9
+ script/generate
10
+ test/test_helper.rb
11
+ test/test_jsont.rb
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on jsont, see http://jsont.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.rdoc ADDED
@@ -0,0 +1,188 @@
1
+ = jsont
2
+
3
+ http://jsonp.rubyforge.org
4
+
5
+ == DESCRIPTION:
6
+
7
+ Port of javascript JsonT transformation library. http://goessner.net/articles/jsont/
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ Conforms to most of the JsonT spec minus the javascript function ability. Need something better there.
12
+
13
+ == SYNOPSIS:
14
+
15
+ Jsont.new({'self' => '<div>{$.test}</div>', 'test' => '<p>{$.x} {$.y}</p>'}).transform({'test' => {'x' => 1, 'y' => 2}})
16
+ => <div><p>1 2</p></div>
17
+
18
+ == INSTALL:
19
+
20
+ sudo gem install jsont
21
+
22
+ == LICENSE:
23
+
24
+ GNU LESSER GENERAL PUBLIC LICENSE
25
+ Version 3, 29 June 2007
26
+
27
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
28
+ Everyone is permitted to copy and distribute verbatim copies
29
+ of this license document, but changing it is not allowed.
30
+
31
+
32
+ This version of the GNU Lesser General Public License incorporates
33
+ the terms and conditions of version 3 of the GNU General Public
34
+ License, supplemented by the additional permissions listed below.
35
+
36
+ 0. Additional Definitions.
37
+
38
+ As used herein, "this License" refers to version 3 of the GNU Lesser
39
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
40
+ General Public License.
41
+
42
+ "The Library" refers to a covered work governed by this License,
43
+ other than an Application or a Combined Work as defined below.
44
+
45
+ An "Application" is any work that makes use of an interface provided
46
+ by the Library, but which is not otherwise based on the Library.
47
+ Defining a subclass of a class defined by the Library is deemed a mode
48
+ of using an interface provided by the Library.
49
+
50
+ A "Combined Work" is a work produced by combining or linking an
51
+ Application with the Library. The particular version of the Library
52
+ with which the Combined Work was made is also called the "Linked
53
+ Version".
54
+
55
+ The "Minimal Corresponding Source" for a Combined Work means the
56
+ Corresponding Source for the Combined Work, excluding any source code
57
+ for portions of the Combined Work that, considered in isolation, are
58
+ based on the Application, and not on the Linked Version.
59
+
60
+ The "Corresponding Application Code" for a Combined Work means the
61
+ object code and/or source code for the Application, including any data
62
+ and utility programs needed for reproducing the Combined Work from the
63
+ Application, but excluding the System Libraries of the Combined Work.
64
+
65
+ 1. Exception to Section 3 of the GNU GPL.
66
+
67
+ You may convey a covered work under sections 3 and 4 of this License
68
+ without being bound by section 3 of the GNU GPL.
69
+
70
+ 2. Conveying Modified Versions.
71
+
72
+ If you modify a copy of the Library, and, in your modifications, a
73
+ facility refers to a function or data to be supplied by an Application
74
+ that uses the facility (other than as an argument passed when the
75
+ facility is invoked), then you may convey a copy of the modified
76
+ version:
77
+
78
+ a) under this License, provided that you make a good faith effort to
79
+ ensure that, in the event an Application does not supply the
80
+ function or data, the facility still operates, and performs
81
+ whatever part of its purpose remains meaningful, or
82
+
83
+ b) under the GNU GPL, with none of the additional permissions of
84
+ this License applicable to that copy.
85
+
86
+ 3. Object Code Incorporating Material from Library Header Files.
87
+
88
+ The object code form of an Application may incorporate material from
89
+ a header file that is part of the Library. You may convey such object
90
+ code under terms of your choice, provided that, if the incorporated
91
+ material is not limited to numerical parameters, data structure
92
+ layouts and accessors, or small macros, inline functions and templates
93
+ (ten or fewer lines in length), you do both of the following:
94
+
95
+ a) Give prominent notice with each copy of the object code that the
96
+ Library is used in it and that the Library and its use are
97
+ covered by this License.
98
+
99
+ b) Accompany the object code with a copy of the GNU GPL and this license
100
+ document.
101
+
102
+ 4. Combined Works.
103
+
104
+ You may convey a Combined Work under terms of your choice that,
105
+ taken together, effectively do not restrict modification of the
106
+ portions of the Library contained in the Combined Work and reverse
107
+ engineering for debugging such modifications, if you also do each of
108
+ the following:
109
+
110
+ a) Give prominent notice with each copy of the Combined Work that
111
+ the Library is used in it and that the Library and its use are
112
+ covered by this License.
113
+
114
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
115
+ document.
116
+
117
+ c) For a Combined Work that displays copyright notices during
118
+ execution, include the copyright notice for the Library among
119
+ these notices, as well as a reference directing the user to the
120
+ copies of the GNU GPL and this license document.
121
+
122
+ d) Do one of the following:
123
+
124
+ 0) Convey the Minimal Corresponding Source under the terms of this
125
+ License, and the Corresponding Application Code in a form
126
+ suitable for, and under terms that permit, the user to
127
+ recombine or relink the Application with a modified version of
128
+ the Linked Version to produce a modified Combined Work, in the
129
+ manner specified by section 6 of the GNU GPL for conveying
130
+ Corresponding Source.
131
+
132
+ 1) Use a suitable shared library mechanism for linking with the
133
+ Library. A suitable mechanism is one that (a) uses at run time
134
+ a copy of the Library already present on the user's computer
135
+ system, and (b) will operate properly with a modified version
136
+ of the Library that is interface-compatible with the Linked
137
+ Version.
138
+
139
+ e) Provide Installation Information, but only if you would otherwise
140
+ be required to provide such information under section 6 of the
141
+ GNU GPL, and only to the extent that such information is
142
+ necessary to install and execute a modified version of the
143
+ Combined Work produced by recombining or relinking the
144
+ Application with a modified version of the Linked Version. (If
145
+ you use option 4d0, the Installation Information must accompany
146
+ the Minimal Corresponding Source and Corresponding Application
147
+ Code. If you use option 4d1, you must provide the Installation
148
+ Information in the manner specified by section 6 of the GNU GPL
149
+ for conveying Corresponding Source.)
150
+
151
+ 5. Combined Libraries.
152
+
153
+ You may place library facilities that are a work based on the
154
+ Library side by side in a single library together with other library
155
+ facilities that are not Applications and are not covered by this
156
+ License, and convey such a combined library under terms of your
157
+ choice, if you do both of the following:
158
+
159
+ a) Accompany the combined library with a copy of the same work based
160
+ on the Library, uncombined with any other library facilities,
161
+ conveyed under the terms of this License.
162
+
163
+ b) Give prominent notice with the combined library that part of it
164
+ is a work based on the Library, and explaining where to find the
165
+ accompanying uncombined form of the same work.
166
+
167
+ 6. Revised Versions of the GNU Lesser General Public License.
168
+
169
+ The Free Software Foundation may publish revised and/or new versions
170
+ of the GNU Lesser General Public License from time to time. Such new
171
+ versions will be similar in spirit to the present version, but may
172
+ differ in detail to address new problems or concerns.
173
+
174
+ Each version is given a distinguishing version number. If the
175
+ Library as you received it specifies that a certain numbered version
176
+ of the GNU Lesser General Public License "or any later version"
177
+ applies to it, you have the option of following the terms and
178
+ conditions either of that published version or of any later version
179
+ published by the Free Software Foundation. If the Library as you
180
+ received it does not specify a version number of the GNU Lesser
181
+ General Public License, you may choose any version of the GNU Lesser
182
+ General Public License ever published by the Free Software Foundation.
183
+
184
+ If the Library as you received it specifies that a proxy can decide
185
+ whether future versions of the GNU Lesser General Public License shall
186
+ apply, that proxy's public statement of acceptance of any version is
187
+ permanent authorization for you to choose that version for the
188
+ Library.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/jsont'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('jsont', Jsont::VERSION) do |p|
7
+ p.developer('FIXME full name', 'FIXME email')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
+ p.rubyforge_name = p.name # TODO this is default value
11
+ p.extra_deps = [
12
+ ['json'],
13
+ ]
14
+ p.extra_dev_deps = [
15
+ ['newgem', ">= #{::Newgem::VERSION}"]
16
+ ]
17
+
18
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
19
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
20
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
21
+ p.rsync_args = '-av --delete --ignore-errors'
22
+ end
23
+
24
+ require 'newgem/tasks' # load /tasks/*.rake
25
+ Dir['tasks/**/*.rake'].each { |t| load t }
26
+
27
+ # TODO - want other tests/tasks run by default? Add them to the list
28
+ # task :default => [:spec, :features]
data/lib/jsont.rb ADDED
@@ -0,0 +1,64 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'rubygems'
5
+ require 'json'
6
+
7
+ class Jsont
8
+ VERSION = '0.1.0'
9
+
10
+ def initialize(rules)
11
+ @rules = rules.is_a?(String) ? JSON.parse(rules) : rules.dup
12
+ @rules.each do |r,v|
13
+ if r[0,4] != 'self'
14
+ @rules["self.#{r}"] = @rules.delete(r)
15
+ end
16
+ end
17
+ end
18
+
19
+ def transform(target)
20
+ @parent = target.is_a?(String) ? JSON.parse(target) : target
21
+ apply!('self',@parent)
22
+ end
23
+
24
+ def apply!(target_path, target)
25
+ case target
26
+ when Array
27
+ target.each_index do |i|
28
+ target[i] = apply!("#{target_path}[*]", apply!("#{target_path}[#{i}]", target[i]))
29
+ end
30
+ when Hash
31
+ target.each do |k, v|
32
+ target[k] = apply!("#{target_path}.?", apply!("#{target_path}.#{k}", target[k]))
33
+ end
34
+ end
35
+
36
+ if (rule = @rules[target_path])
37
+ rule.gsub(/\{([a-z0-9\$\.]+)\}/) do |match|
38
+ path = match[1,match.size - 2]
39
+ n = case path[0]
40
+ when ?$
41
+ target
42
+ else
43
+ @parent
44
+ end
45
+ path.gsub!(/^\$\.?/,'')
46
+
47
+ if path.size > 0
48
+ path.split('.').each do |piece|
49
+ n = case piece
50
+ when /^[0-9]+$/
51
+ n[Integer(piece)]
52
+ else
53
+ n[piece]
54
+ end
55
+ end
56
+ end
57
+ n
58
+ end
59
+ else
60
+ target
61
+ end
62
+ end
63
+ end
64
+
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/jsont.rb'}"
9
+ puts "Loading jsont gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/jsont'
@@ -0,0 +1,30 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestJsont < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_expressions
9
+ assert_equal(Jsont.new('{ "self": "<table>{pnt}</table>", "pnt": "<tr><td>{pnt.x}</td><td>{pnt.y}</td></tr>" }').transform('{"pnt": { "x":2, "y":3 }}'), '<table><tr><td>2</td><td>3</td></tr></table>')
10
+
11
+ assert_equal(Jsont.new('{ "self": "<table><tr>{$}</tr></table>","self[*]": "<td>{$}</td>" }').transform('[1,2]'), '<table><tr><td>1</td><td>2</td></tr></table>')
12
+
13
+ assert_equal(Jsont.new('{ "self": "<table>\n{$}\n</table>",
14
+ "self[*]": "<tr>{$}</tr>\n",
15
+ "self[*][*]": "<td>{$}</td>" }').transform('[[1,2],[3,4]]'), '<table>
16
+ <tr><td>1</td><td>2</td></tr>
17
+ <tr><td>3</td><td>4</td></tr>
18
+
19
+ </table>')
20
+
21
+ assert_equal(Jsont.new('{"self": "<div>\n{p}\n</div>", "p": "<table><tr>{$}</tr></table>\n", "p[*]": "<td>{$.x}</td><td>{$.y}</td>"}').transform('{"a":"hello", "p":[{"x":1, "y":2},{"x":3, "y":4}]}'),'<div>
22
+ <table><tr><td>1</td><td>2</td><td>3</td><td>4</td></tr></table>
23
+
24
+ </div>')
25
+
26
+ assert_equal(Jsont.new('{ "self": "<a href=\"{uri}\" title=\'{title}\'>{$.title}</a>" }').transform('{ "uri":"http://somewhere.org", "title":"somewhere homepage" }'), '<a href="http://somewhere.org" title=\'somewhere homepage\'>somewhere homepage</a>')
27
+
28
+ assert_equal(Jsont.new({'self' => '<div>{$.test}</div>', 'test' => '<p>{$.x} {$.y}</p>'}).transform({'test' => {'x' => 1, 'y' => 2}}), '<div><p>1 2</p></div>')
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ required_ruby_version: !ruby/object:Gem::Requirement
3
+ requirements:
4
+ - - '>='
5
+ - !ruby/object:Gem::Version
6
+ version: !str 0
7
+ version:
8
+ email:
9
+ - FIXME email
10
+ cert_chain: []
11
+ summary: Port of javascript JsonT transformation library
12
+ post_install_message: PostInstall.txt
13
+ extra_rdoc_files:
14
+ - History.txt
15
+ - Manifest.txt
16
+ - PostInstall.txt
17
+ - README.rdoc
18
+ homepage: http://jsonp.rubyforge.org
19
+ signing_key:
20
+ name: jsont
21
+ rdoc_options:
22
+ - --main
23
+ - README.rdoc
24
+ autorequire:
25
+ rubyforge_project: jsont
26
+ executables: []
27
+ description: Port of javascript JsonT transformation library. http://goessner.net/articles/jsont/
28
+ specification_version: 2
29
+ default_executable:
30
+ files:
31
+ - History.txt
32
+ - Manifest.txt
33
+ - PostInstall.txt
34
+ - README.rdoc
35
+ - Rakefile
36
+ - lib/jsont.rb
37
+ - script/console
38
+ - script/destroy
39
+ - script/generate
40
+ - test/test_helper.rb
41
+ - test/test_jsont.rb
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: !str 0
47
+ version:
48
+ extensions: []
49
+ rubygems_version: 1.2.0
50
+ requirements: []
51
+ authors:
52
+ - FIXME full name
53
+ date: 2008-11-25 05:00:00 +00:00
54
+ platform: ruby
55
+ test_files:
56
+ - test/test_helper.rb
57
+ - test/test_jsont.rb
58
+ version: !ruby/object:Gem::Version
59
+ version: 0.1.0
60
+ require_paths:
61
+ - lib
62
+ dependencies:
63
+ - !ruby/object:Gem::Dependency
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: !str 0
69
+ version:
70
+ type: :runtime
71
+ version_requirement:
72
+ name: json
73
+ - !ruby/object:Gem::Dependency
74
+ version_requirements: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: 1.1.0
79
+ version:
80
+ type: :development
81
+ version_requirement:
82
+ name: newgem
83
+ - !ruby/object:Gem::Dependency
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: 1.8.0
89
+ version:
90
+ type: :development
91
+ version_requirement:
92
+ name: hoe
93
+ bindir: bin
94
+ has_rdoc: true