jinx-json 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ .DS_Store
2
+ *~
3
+ *.pdf
4
+ .project
5
+ .loadpath
6
+ .yardoc
7
+ *.gem
8
+ *.tar*
9
+ **/ext/bin
10
+ **/classes
11
+ /doc/api
12
+ **log
13
+ /test/results
14
+
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --backtrace
2
+ --format Fuubar
3
+ --color
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ -o doc/api --private --protected --name-tag 'quirk:Technology idiosyncracy' - History.md LEGAL LICENSE
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source :rubygems
2
+ gemspec
3
+
4
+ group :development do
5
+ gem 'ruby-debug'
6
+ # Uncomment to use a local gem.
7
+ gem 'jinx', :path => File.dirname(__FILE__) + '/../core'
8
+ gem 'jinx-json', :path => File.dirname(__FILE__), :require => 'jinx/json'
9
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ jinx-json (2.1.1)
5
+ bundler
6
+ jinx
7
+ json_pure
8
+
9
+ PATH
10
+ remote: /Users/loneyf/workspace/jinx/core
11
+ specs:
12
+ jinx (2.1.1)
13
+ bundler
14
+
15
+ GEM
16
+ remote: http://rubygems.org/
17
+ specs:
18
+ columnize (0.3.6)
19
+ diff-lcs (1.1.3)
20
+ json_pure (1.6.3)
21
+ rake (0.9.2.2)
22
+ rspec (2.9.0)
23
+ rspec-core (~> 2.9.0)
24
+ rspec-expectations (~> 2.9.0)
25
+ rspec-mocks (~> 2.9.0)
26
+ rspec-core (2.9.0)
27
+ rspec-expectations (2.9.1)
28
+ diff-lcs (~> 1.1.3)
29
+ rspec-mocks (2.9.0)
30
+ ruby-debug (0.10.4)
31
+ columnize (>= 0.1)
32
+ ruby-debug-base (~> 0.10.4.0)
33
+ ruby-debug-base (0.10.4-java)
34
+
35
+ PLATFORMS
36
+ java
37
+
38
+ DEPENDENCIES
39
+ jinx!
40
+ jinx-json!
41
+ rake
42
+ rspec (>= 2.6)
43
+ ruby-debug
data/History.md ADDED
@@ -0,0 +1,6 @@
1
+ This history lists major release themes. See the GitHub commits (https://github.com/jinx/json)
2
+ for change details.
3
+
4
+ 2.1.1 / 2012-04-13
5
+ ------------------
6
+ * Initial public release spun off from caruby/core.
data/LEGAL ADDED
@@ -0,0 +1,5 @@
1
+ LEGAL NOTICE INFORMATION
2
+ ------------------------
3
+
4
+ All the files in this distribution are covered under either the MIT
5
+ license (see the file LICENSE).
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2011 Oregon Health & Science University
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ Jinx JSON plug-in
2
+ =================
3
+
4
+ **Home**: [http://github.com/jinx/json](http://github.com/jinx/json)
5
+ **Git**: [http://github.com/jinx/json](http://github.com/jinx/json)
6
+ **Author**: OHSU Knight Cancer Institute
7
+ **Copyright**: 2012
8
+ **License**: MIT License
9
+
10
+ Synopsis
11
+ --------
12
+ Jinx JSON enables JSON serialization for [Jinx](http://github.com/jinx/core) resources.
13
+
14
+ Installing
15
+ ----------
16
+ Jinx JSON is installed as a JRuby gem:
17
+
18
+ [sudo] jgem install jinx-json
19
+
20
+ Usage
21
+ -----
22
+ Enable Jinx for a Java package, as described in the [Jinx](http://github.com/jinx/json/core) Usage.
23
+
24
+ require 'jinx/json'
25
+ jsoned = jinxed.to_json #=> the JSON representation of a jinxed resource
26
+ JSON[jsoned] #=> the deserialized jinxed resource
27
+
28
+ Copyright
29
+ ---------
30
+ Jinx © 2011 by [Oregon Health & Science University](http://www.ohsu.edu/xd/health/services/cancer/index.cfm).
31
+ Jinx is licensed under the MIT license. Please see the LICENSE and LEGAL files for more information.
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ require File.dirname(__FILE__) + '/lib/jinx/json/version'
2
+
3
+ # the gem name
4
+ GEM = 'jinx-json'
5
+ GEM_VERSION = Jinx::JSON::VERSION
6
+
7
+ WINDOWS = (Config::CONFIG['host_os'] =~ /mingw|win32|cygwin/ ? true : false) rescue false
8
+ SUDO = WINDOWS ? '' : 'sudo'
9
+
10
+ desc "Builds the gem"
11
+ task :gem do
12
+ sh "jgem build #{GEM}.gemspec"
13
+ end
14
+
15
+ desc "Installs the gem"
16
+ task :install => :gem do
17
+ sh "#{SUDO} jgem install #{GEM}-#{GEM_VERSION}.gem"
18
+ end
19
+
20
+ desc 'Documents the API'
21
+ task :doc do
22
+ FileUtils.rm_rf 'doc/api'
23
+ sh 'yardoc'
24
+ end
25
+
26
+ desc 'Runs the spec tests'
27
+ task :spec do
28
+ Dir['spec/**/*_spec.rb'].each { |f| sh "rspec #{f}" rescue nil }
29
+ end
30
+
31
+ desc 'Runs all tests'
32
+ task :test => :spec
data/jinx-json.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ require File.dirname(__FILE__) + '/lib/jinx/json/version'
2
+ require 'date'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'jinx-json'
6
+ s.summary = 'Jinx JSON plug-in.'
7
+ s.version = Jinx::JSON::VERSION
8
+ s.description = s.summary + '. See github.com/jinx/json for more information.'
9
+ s.date = Date.today
10
+ s.author = 'OHSU'
11
+ s.email = "jinx.ruby@gmail.com"
12
+ s.homepage = 'http://github.com/jinx/json'
13
+ s.platform = Gem::Platform::RUBY
14
+ s.require_path = 'lib'
15
+ s.bindir = 'bin'
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files test spec`.split("\n")
18
+ s.add_dependency 'bundler'
19
+ s.add_dependency 'jinx'
20
+ s.add_dependency 'json_pure'
21
+ s.add_development_dependency 'rake'
22
+ s.add_development_dependency 'rspec', '>= 2.6'
23
+ s.has_rdoc = 'yard'
24
+ s.license = 'MIT'
25
+ s.rubyforge_project = 'jinx'
26
+ end
@@ -0,0 +1,13 @@
1
+ require 'jinx/helpers/collection'
2
+
3
+ module Jinx
4
+ module Collection
5
+ # Adds JSON serialization to collections.
6
+ #
7
+ # @param args the JSON serialization options
8
+ # @return [String] the JSON representation of this Enumerable as an array
9
+ def to_json(*args)
10
+ to_a.to_json(*args)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ require 'jinx/helpers/hashable'
2
+
3
+ module Jinx
4
+ module JSON
5
+ # JSON => {Jinx::Resource} deserializer.
6
+ #
7
+ # This Deserializer reconstitutes a jinxed object from a JSON string built by a {Serializer}.
8
+ module Deserializer
9
+ # @param [String] json the JSON to deserialize
10
+ # @return [Jinx::Resource] the deserialized object
11
+ def json_create(json)
12
+ # Make the new object from the json data attribute => value hash.
13
+ new(json['data'])
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,90 @@
1
+ require 'json'
2
+ require 'jinx/json/collection'
3
+
4
+ module Jinx
5
+ module JSON
6
+ # {Jinx::Resource} => JSON serializer.
7
+ #
8
+ # This Serializer creates a JSON string from a jinxed object. The JSON payload is
9
+ # suitable for transfer to a remote {Deserializer}. This {Serializer} is included
10
+ # directly in the jinxed application domain module after including {Jinx::Resource},
11
+ # e.g.:
12
+ # module MyApp
13
+ # include Jinx::JSON::Serializer, Jinx:Resource
14
+ # which includes +Resource+ followed by the +Serializer+.
15
+ #
16
+ # The module which includes this Serializer is extended with the {Importer}, which
17
+ # enables deserialization of the serialized JSON.
18
+ #
19
+ # The JSON payload is built as follows:
20
+ # * non-domain properties are serialized
21
+ # * dependent references are serialized recursively
22
+ # * independent reference primary and secondary key content is serialized
23
+ module Serializer
24
+ # @param args the JSON serialization options
25
+ # @return [String] the JSON representation of this {Jinx::Resource}
26
+ def to_json(*args)
27
+ {
28
+ 'json_class' => json_class_name,
29
+ 'data' => json_value_hash
30
+ }.to_json(*args)
31
+ end
32
+
33
+ private
34
+
35
+ # The JSON class name must be scoped by the Resource package module, not the
36
+ # Java package, in order to recognize the Jinx::Resource JSON hooks.
37
+ #
38
+ # @return [String] the class name qualified by the Resource package module name context
39
+ def json_class_name
40
+ [self.class.domain_module, self.class.name.demodulize].join('::')
41
+ end
42
+
43
+ # Builds a serializable attribute => value hash. If _key_only _ is true, then only
44
+ # the key attributes are included in the hash. Otherwise, all properties are included
45
+ # in the hash.
46
+ #
47
+ # An independent or owner attribute value is a copy of the referenced domain object
48
+ # consisting of only the primary and secondary key attributes.
49
+ #
50
+ # @return [{Symbol => Object}] the serializable value hash
51
+ def json_value_hash
52
+ vh = value_hash(self.class.nondomain_attributes)
53
+ vh.merge!(value_hash(self.class.dependent_attributes))
54
+ self.class.independent_attributes.each do |ia|
55
+ ref = send(ia)
56
+ vh[ia] = json_independent_reference(ref) unless ref.nil_or_empty?
57
+ end
58
+ vh
59
+ end
60
+
61
+ # @param [Resource] the referenced object
62
+ # @return [Resource] a copy of the referenced object which only contains
63
+ # key property values
64
+ def json_independent_reference(ref)
65
+ return ref.map { |item| json_independent_reference(item) } if ref.collection?
66
+ vh = json_key_hash(ref)
67
+ ref.copy(vh) unless vh.empty?
68
+ end
69
+
70
+ # @param (see #json_independent_reference)
71
+ # @return [{Symbol => Object}, nil] the key attribute => value hash, or nil
72
+ # if there is no complete key
73
+ def json_key_hash(ref)
74
+ vh = ref.value_hash(ref.class.primary_key_attributes)
75
+ ref.class.secondary_key_attributes.each do |ka|
76
+ value = json_key_value(ref, ka)
77
+ vh[ka] = value if value
78
+ end
79
+ end
80
+
81
+ # @param (see #json_independent_reference)
82
+ # @param [Symbol] the key value
83
+ # @return the key attribute value
84
+ def json_key_value(ref, attribute)
85
+ value = ref.send(attribute)
86
+ Resource === value ? json_independent_reference(value) : value
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,5 @@
1
+ module Jinx
2
+ module JSON
3
+ VERSION = "2.1.1"
4
+ end
5
+ end
data/lib/jinx/json.rb ADDED
@@ -0,0 +1 @@
1
+ require 'json'
@@ -0,0 +1,46 @@
1
+ require 'spec/spec_helper'
2
+
3
+ module Family
4
+ describe Address do
5
+ it "should serialize an address" do
6
+ a = Address.new(:street1 => '16 Elm St', :city => 'Peoria', :state => 'IL')
7
+ j = JSON[a.to_json]
8
+ j.street1.should == a.street1
9
+ j.city.should == a.city
10
+ j.state.should == a.state
11
+ end
12
+ end
13
+
14
+ describe Household do
15
+ it "should serialize the address" do
16
+ a = Address.new(:street1 => '16 Elm St', :city => 'Peoria', :state => 'IL')
17
+ h = Household.new(:address => a)
18
+ j = JSON[h.to_json]
19
+ j.address.street1.should == a.street1
20
+ j.address.city.should == a.city
21
+ j.address.state.should == a.state
22
+ end
23
+ end
24
+
25
+ describe Parent do
26
+ it "should serialize the children" do
27
+ p = Parent.new(:name => 'Oscar')
28
+ c = Child.new(:name => 'Beauregard', :parents => [p])
29
+ p.children << c
30
+ j = JSON[p.to_json]
31
+ j.children.first.name.should == c.name
32
+ end
33
+ end
34
+
35
+ describe Child do
36
+ it "should only serialize the owner key" do
37
+ h = Household.new
38
+ p = Parent.new(:name => 'Oscar', :household => h)
39
+ c = Child.new(:name => 'Beauregard', :parents => [p])
40
+ p.children << c
41
+ j = JSON[c.to_json]
42
+ j.parents.first.name.should be nil
43
+ j.parents.first.household.should be nil
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ Bundler.require(:test, :development)
4
+
5
+ # Load the Family example.
6
+ require Bundler.environment.specs.detect { |s| s.name == 'jinx' }.full_gem_path + '/examples/family/lib/family'
7
+
8
+ # Add serialization
9
+ require 'jinx/json/serializer'
10
+
11
+ module Family
12
+ include Jinx::JSON::Serializer
13
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jinx-json
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 2.1.1
6
+ platform: ruby
7
+ authors:
8
+ - OHSU
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-04-13 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: bundler
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: jinx
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: json_pure
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rake
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: rspec
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "2.6"
69
+ type: :development
70
+ version_requirements: *id005
71
+ description: Jinx JSON plug-in.. See github.com/jinx/json for more information.
72
+ email: jinx.ruby@gmail.com
73
+ executables: []
74
+
75
+ extensions: []
76
+
77
+ extra_rdoc_files: []
78
+
79
+ files:
80
+ - .gitignore
81
+ - .rspec
82
+ - .yardopts
83
+ - Gemfile
84
+ - Gemfile.lock
85
+ - History.md
86
+ - LEGAL
87
+ - LICENSE
88
+ - README.md
89
+ - Rakefile
90
+ - jinx-json.gemspec
91
+ - lib/jinx/json.rb
92
+ - lib/jinx/json/collection.rb
93
+ - lib/jinx/json/deserializer.rb
94
+ - lib/jinx/json/serializer.rb
95
+ - lib/jinx/json/version.rb
96
+ - spec/serializer_spec.rb
97
+ - spec/spec_helper.rb
98
+ has_rdoc: yard
99
+ homepage: http://github.com/jinx/json
100
+ licenses:
101
+ - MIT
102
+ post_install_message:
103
+ rdoc_options: []
104
+
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: "0"
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: "0"
119
+ requirements: []
120
+
121
+ rubyforge_project: jinx
122
+ rubygems_version: 1.5.1
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: Jinx JSON plug-in.
126
+ test_files:
127
+ - spec/serializer_spec.rb
128
+ - spec/spec_helper.rb