flatpack_core 1.0.pre

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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in flatpack_client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # FlatpackCore
2
+
3
+ FlatpackCore provides serialization / de-serialization support to FlatPack client libraries.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'flatpack_core'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install flatpack_core
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env rake
2
+
3
+ desc "Run tests"
4
+ task :test do
5
+ puts `bundle exec rspec spec`
6
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/flatpack/core/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Joe Stelmach"]
6
+ gem.email = ["joe@getperka.com"]
7
+ gem.description = %q{Write a gem description}
8
+ gem.summary = %q{Write a gem summary}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "flatpack_core"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Flatpack::Core::VERSION
17
+ gem.add_dependency('json', '~> 1.7')
18
+ gem.add_dependency('uuidtools', '~> 2.1')
19
+ gem.add_dependency('i18n', '~> 0.6')
20
+ gem.add_dependency('activesupport', "~> 3.0")
21
+ gem.add_development_dependency "rspec", "~> 2.6"
22
+ end
@@ -0,0 +1,57 @@
1
+ require 'uuidtools'
2
+
3
+ module Flatpack
4
+ module Core
5
+ class BaseHasUuid
6
+ include MapInitialize
7
+
8
+ PROPERTY_NAMES = [:uuid]
9
+
10
+ attr_accessor *PROPERTY_NAMES
11
+
12
+ # returns this entity's assigned uuid. If no uuid has been assigned,
13
+ # a new uuid will be generated and assigned before returning.
14
+ def uuid
15
+ @uuid = @uuid || UUIDTools::UUID.random_create
16
+ end
17
+
18
+ # returns an array of all flatpack property names for this entity
19
+ def property_names
20
+ names = []
21
+ klass=self.class
22
+ while (klass)
23
+ names += klass::PROPERTY_NAMES if defined? klass::PROPERTY_NAMES
24
+ klass = klass.superclass
25
+ end
26
+ names
27
+ end
28
+
29
+ # returns a hash of all flatpack property names => non-nil values for this entity
30
+ def properties
31
+ map = {}
32
+ property_names.each do |name|
33
+ value = self.send(name)
34
+ map[name] = value unless value == nil
35
+ end
36
+ map
37
+ end
38
+
39
+ # returns the flatpack entity name for this entity
40
+ def entity_name
41
+ name = self.class.name.gsub(/^.*::/,'')
42
+ name[0,1].downcase + name[1..-1]
43
+ end
44
+
45
+ def class_for_property(property)
46
+ klass=self.class
47
+ result = nil
48
+ while (klass and !result)
49
+ result = klass::TYPE_MAP[property.to_sym] if defined? klass::TYPE_MAP
50
+ klass = klass.superclass
51
+ end
52
+ result
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,16 @@
1
+ module Flatpack
2
+ module Core
3
+ class Flatpack
4
+
5
+ include MapInitialize
6
+
7
+ attr_reader :packer, :unpacker, :configuration
8
+
9
+ def initialize(map={})
10
+ set_properties(map)
11
+ @packer = Packer.new(map)
12
+ @unpacker = Unpacker.new(map)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ module Flatpack
2
+ module Core
3
+
4
+ module MapInitialize
5
+
6
+ def initialize(map={})
7
+ set_properties(map)
8
+ end
9
+
10
+ def set_properties(map)
11
+ map.each do |key, value|
12
+ method_key = "#{key}="
13
+ if(self.respond_to?(method_key))
14
+ self.send(method_key, value)
15
+ end
16
+ end
17
+ self
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,104 @@
1
+ require 'active_support/inflector'
2
+ require 'json'
3
+
4
+ module Flatpack
5
+ module Core
6
+ class Packer
7
+
8
+ include MapInitialize
9
+
10
+ attr_accessor :verbose, :pretty
11
+
12
+ # Generate a FlatPack JSON string from the given entity
13
+ def pack(entity)
14
+ return {} unless entity
15
+
16
+ @uuid_stack = []
17
+ @all_entities = {}
18
+ @data = {}
19
+
20
+ recursive_pack(entity)
21
+
22
+ json = {
23
+ :data => @data,
24
+ :value => entity.uuid
25
+ }
26
+ json = @pretty ? JSON.pretty_generate(json) : json.to_json
27
+
28
+ if(@verbose)
29
+ puts "*** Serializing #{entity.entity_name} to FlatPack ***"
30
+ puts json
31
+ end
32
+
33
+ json
34
+ end
35
+
36
+ private
37
+
38
+ def recursive_pack(entity)
39
+ # we only proceed if the entity has a uuid property,
40
+ # and we haven't seen it before
41
+ uuid = entity.uuid
42
+ return if !entity.respond_to?(:uuid) or @all_entities.keys.include?(uuid)
43
+
44
+ @all_entities[uuid] = entity
45
+ parent_uuid = @uuid_stack.length > 0 ? @uuid_stack.last : nil
46
+ @uuid_stack.push(uuid)
47
+
48
+ # walk through our properties
49
+ json = entity.properties
50
+ json.keys.each do |name|
51
+ camelName = name.to_s().camelcase(:lower)
52
+ value = json[name]
53
+ uuid_name = "#{camelName}Uuid"
54
+
55
+ # The name we're working with will be replaced with a
56
+ # properly cased name or uuid reference
57
+ json.delete(name)
58
+
59
+ # property is another entity
60
+ if(value.respond_to?(:uuid))
61
+ recursive_pack(value)
62
+ json[uuid_name] = value.uuid
63
+
64
+ # property is a collection of (potentially) other entities
65
+ elsif(value.is_a?(Array))
66
+
67
+ # we'll recurse down to each referenced value
68
+ value.each do |referenced_value|
69
+ recursive_pack(referenced_value)
70
+ referenced_value.uuid
71
+ end
72
+
73
+ # and add our uuid reference
74
+ json[uuid_name] = value.map {|v| v.uuid}
75
+
76
+ # property is a plain old property
77
+ else
78
+ # TODO how to handle local..At dates?
79
+ # Pass a timezone into the pack function as an option?
80
+
81
+ json[camelName] = value
82
+ end
83
+ end
84
+
85
+ # add our parent uuid reference if neccessary
86
+ if parent_uuid
87
+ parent = @all_entities[parent_uuid]
88
+ json["#{parent.entity_name}Uuid"] = parent_uuid
89
+ end
90
+
91
+ # add the resulting json to the correct bucket in our data hash
92
+ bucket = @data[entity.entity_name]
93
+ if(!bucket)
94
+ bucket = []
95
+ @data[entity.entity_name] = bucket
96
+ end
97
+ bucket.push(json)
98
+
99
+ @uuid_stack.pop
100
+ end
101
+
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,141 @@
1
+ require 'active_support/inflector'
2
+
3
+ module Flatpack
4
+ module Core
5
+ class Unpacker
6
+
7
+ include MapInitialize
8
+
9
+ attr_accessor :verbose, :entity_module
10
+
11
+ # Returns a Reified FlatPack entity from the given JSON data
12
+ def unpack(json_map)
13
+ @all_entities = {}
14
+
15
+ if(@verbose)
16
+ puts "** Deserializing entity from FlatPack **"
17
+ puts JSON.pretty_generate(json_map)
18
+ end
19
+
20
+ # Make a first pass through the data to ensure stub entities exist
21
+ uuid_to_data = {}
22
+ (json_map['data'] || []).each do |entity_name, entities|
23
+ if(entities.is_a?(Array))
24
+ entities.each do |entity_data|
25
+ uuid = entity_data['uuid']
26
+ if(allocate_or_get_entity(entity_name, uuid))
27
+ uuid_to_data[uuid] = entity_data
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ # Then ingest
34
+ uuid_to_data.each { |uuid, data| ingest_item(uuid, data) }
35
+
36
+ # Return the unpacked entities corresponding to those in the value section
37
+ value = json_map['value']
38
+ value.is_a?(Array) ? value.map{|v| @all_entities[v]} : @all_entities[value]
39
+ end
40
+
41
+ private
42
+
43
+ # ingests the given data into the entity associated with the given uuid
44
+ def ingest_item(uuid, data)
45
+ entity = @all_entities[uuid]
46
+ collection_key = entity.entity_name.underscore.pluralize
47
+ reified_properties = {}
48
+
49
+ data.each do |key, value|
50
+
51
+ # fooBarsUuid -> foo_bars
52
+ key = key.underscore
53
+
54
+ # If the key name ends in _date or _at, we assume this is a date property
55
+ if(key.end_with?('_date') or key.end_with?('_at'))
56
+ reified_properties[key] = Time.parse(value).utc
57
+ end
58
+
59
+ # The value is referencing another entity or collection of entities
60
+ if(key.end_with?('uuid'))
61
+
62
+ # foo_bar_uuid -> foo_bar
63
+ key = key[0..-6]
64
+
65
+ if(value.is_a?(Array))
66
+ reified_properties[key] = value.map { |uuid| @all_entities[uuid] }
67
+
68
+ else
69
+ referent = @all_entities[value]
70
+
71
+ # If the referent doesn't exist, it's because the server has sent us a sparse
72
+ # payload with a dangling fooUuid reference. We'll attempt to unpack this
73
+ # reference using the type informtion encoded within each entity description
74
+ unless referent
75
+ referent_class = entity.class_for_property(key)
76
+ referent = referent_class.new(:uuid => value) if referent_class
77
+ end
78
+
79
+ # If a referent was not included in the payload, and no type information exists
80
+ # for this property name, we simply drop the property
81
+ next unless referent
82
+
83
+ reified_properties[key] = referent
84
+
85
+ # Find or create a collection in the referent to establish a bidirectional mapping.
86
+ # For example a foo.bar property should contain a bar.foos collection.
87
+ if(referent.respond_to?(collection_key))
88
+ collection = referent.send(collection_key)
89
+ unless collection
90
+ collection = []
91
+ collection_setter = "#{collection_key}="
92
+ if(referent.respond_to?(collection_setter))
93
+ referent.send(collection_setter, collection)
94
+ end
95
+ end
96
+ collection.push(entity)
97
+ end
98
+ end
99
+
100
+ # The value is just a plain old property
101
+ else
102
+ reified_properties[key] = value
103
+ end
104
+ end
105
+
106
+ entity.set_properties(reified_properties)
107
+ end
108
+
109
+ # Finds the existing entity associated with the given uuid in the local
110
+ # entity store, or stores and returns a new one
111
+ def allocate_or_get_entity(entity_name, uuid)
112
+ entity = @all_entities[uuid]
113
+ return entity if entity
114
+
115
+ klass = entity_class_for_name(entity_name)
116
+ if(klass != nil)
117
+ @all_entities[uuid] = klass.new({:uuid => uuid})
118
+ end
119
+ klass
120
+ end
121
+
122
+ # Finds the Ruby class associated with the given entity name
123
+ def entity_class_for_name(name)
124
+ class_name = "#{name[0,1].capitalize}#{name[1..-1]}"
125
+ if @entity_module
126
+ class_name = "#{@entity_module}::#{class_name}"
127
+ end
128
+
129
+ result = nil
130
+ begin
131
+ result = class_name.constantize
132
+
133
+ rescue NameError
134
+ puts "could not create class for name #{class_name}"
135
+ end
136
+
137
+ result
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,5 @@
1
+ module Flatpack
2
+ module Core
3
+ VERSION = "1.0.pre"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ require "flatpack/core/map_initialize"
2
+ require "flatpack/core/base_has_uuid"
3
+ require "flatpack/core/flatpack"
4
+ require "flatpack/core/packer"
5
+ require "flatpack/core/unpacker"
data/spec/core_spec.rb ADDED
@@ -0,0 +1,195 @@
1
+ require 'flatpack_core'
2
+
3
+ class TestEntity < Flatpack::Core::BaseHasUuid
4
+ PROPERTY_NAMES = [
5
+ :one,
6
+ :two,
7
+ :test_sub_entities,
8
+ :test_entities,
9
+ :test_entity
10
+ ]
11
+ attr_accessor *PROPERTY_NAMES
12
+ end
13
+
14
+ class TestSubEntity < TestEntity
15
+ PROPERTY_NAMES = [
16
+ :three
17
+ ]
18
+ attr_accessor *PROPERTY_NAMES
19
+ end
20
+
21
+ describe Flatpack::Core do
22
+
23
+ before(:all) do
24
+ @flatpack = Flatpack::Core::Flatpack.new({
25
+ :pretty => true,
26
+ :verbose => true
27
+ })
28
+ end
29
+
30
+ it "serializes a simple entity" do
31
+
32
+ root = TestEntity.new({
33
+ :one => 'first',
34
+ :two => 'second'
35
+ })
36
+
37
+ json = @flatpack.packer.pack(root)
38
+
39
+ map = JSON.parse(json)
40
+ map['data'].keys.length.should eq(1)
41
+ entities = map['data']['testEntity']
42
+ entities.length.should eq(1)
43
+ entity = entities[0]
44
+
45
+ entity.keys.length.should eq(3)
46
+ entity['one'].should eq('first')
47
+ entity['two'].should eq('second')
48
+ entity['uuid'].should be_true
49
+ end
50
+
51
+ it "serializes an entity with forward references" do
52
+
53
+ root = TestEntity.new({
54
+ :one => 'first',
55
+ :two => [
56
+ TestSubEntity.new({:one => 'first', :three => 'third'}),
57
+ TestSubEntity.new({:one => 'first', :three => 'third'})
58
+ ]
59
+ })
60
+
61
+ json = @flatpack.packer.pack(root)
62
+
63
+ map = JSON.parse(json)
64
+
65
+ map['data'].keys.length.should eq(2)
66
+
67
+ # we should have 1 TestEntity at our root
68
+ root_entities = map['data']['testEntity']
69
+ root_entities.length.should eq(1)
70
+ root_entity = root_entities[0]
71
+ root_entity['one'].should eq('first')
72
+
73
+ # our collection reference at :two should have been flattened away
74
+ root_entity['two'].should be_nil
75
+ root_entity_uuid = root_entity['uuid']
76
+
77
+ # two TestSubEntities under our root
78
+ sub_entities = map['data']['testSubEntity']
79
+ sub_entities.length.should eq(2)
80
+ sub_entity_one = sub_entities[0]
81
+ sub_entity_two = sub_entities[1]
82
+
83
+ # sub entities should have a reference to the parent
84
+ sub_entity_one['testEntityUuid'].should eq(root_entity_uuid)
85
+ sub_entity_two['testEntityUuid'].should eq(root_entity_uuid)
86
+
87
+ # and 3 additional properties
88
+ sub_entity_one.length.should eq(4)
89
+ sub_entity_one['one'].should eq('first')
90
+ sub_entity_one['three'].should eq('third')
91
+ sub_entity_one['two'].should be_nil
92
+
93
+ sub_entity_two.length.should eq(4)
94
+ sub_entity_two['one'].should eq('first')
95
+ sub_entity_two['three'].should eq('third')
96
+ sub_entity_two['two'].should be_nil
97
+ end
98
+
99
+ it "serializes an entity with back reference" do
100
+
101
+ sibling_one = TestEntity.new({
102
+ :one => 'first_sibling'
103
+ })
104
+
105
+ sibling_two = TestEntity.new({
106
+ :one => 'second_sibling',
107
+ :two => sibling_one
108
+ })
109
+
110
+ json = @flatpack.packer.pack(sibling_two)
111
+
112
+ map = JSON.parse(json)
113
+
114
+ map['data'].keys.length.should eq(1)
115
+ entities = map['data']['testEntity']
116
+ entities.each do |e|
117
+ if(e['uuid'].eql?(sibling_one.uuid.to_s))
118
+ e['testEntityUuid'].should eq(sibling_two.uuid.to_s)
119
+ else
120
+ e['testEntityUuid'].should be_nil
121
+ end
122
+ end
123
+
124
+ end
125
+
126
+ it "de-serializes a simple entity" do
127
+
128
+ json = '{'\
129
+ '"value": "b2197fc8-42df-4d8c-9890-0f37a4f99fc7",'\
130
+ '"data": {'\
131
+ '"unknownEntity": [{"uuid":"b2197fc8-42df-4d8c-9890-0f37a4f998ae"}],'\
132
+ '"testEntity": ['\
133
+ '{'\
134
+ '"one": "first",'\
135
+ '"two": "second",'\
136
+ '"uuid": "b2197fc8-42df-4d8c-9890-0f37a4f99fc7"'\
137
+ '}'\
138
+ ']'\
139
+ '}'\
140
+ '}'
141
+
142
+ # the unpacking should succeed even when an unknown
143
+ # entity type (unknownEntity) is present
144
+ entity = @flatpack.unpacker.unpack(JSON.parse(json))
145
+
146
+ entity.one.should eq('first')
147
+ entity.two.should eq('second')
148
+ entity.uuid.should eq('b2197fc8-42df-4d8c-9890-0f37a4f99fc7')
149
+
150
+ end
151
+
152
+ it "de-serializes an entity with a collection reference" do
153
+ json = '{'\
154
+ '"value": "d1c958ba-e6ea-4b12-8335-347289e404ac",'\
155
+ '"data": {'\
156
+ '"testEntity": ['\
157
+ '{'\
158
+ '"uuid": "d1c958ba-e6ea-4b12-8335-347289e404ac",'\
159
+ '"one": "first"'\
160
+ '}'\
161
+ '],'\
162
+ '"testSubEntity": ['\
163
+ '{'\
164
+ '"three": "third",'\
165
+ '"uuid": "ae90cb47-5638-4529-9223-049ec89b6162",'\
166
+ '"testEntityUuid": "d1c958ba-e6ea-4b12-8335-347289e404ac",'\
167
+ '"one": "first"'\
168
+ '},'\
169
+ '{'\
170
+ '"three": "third",'\
171
+ '"uuid": "4f00da0c-6427-4d87-b691-bedad45329d7",'\
172
+ '"testEntityUuid": "d1c958ba-e6ea-4b12-8335-347289e404ac",'\
173
+ '"one": "first"'\
174
+ '}'\
175
+ ']'\
176
+ '}'\
177
+ '}'\
178
+
179
+ entity = @flatpack.unpacker.unpack(JSON.parse(json))
180
+
181
+ entity.uuid.should eq('d1c958ba-e6ea-4b12-8335-347289e404ac')
182
+ entity.one.should eq('first')
183
+
184
+ # entity should have both sub entities within the test_entities property
185
+ entity.test_sub_entities.length.should eq(2)
186
+ entity.test_sub_entities[0].three.should eq('third')
187
+ entity.test_sub_entities[0].one.should eq('first')
188
+ entity.test_sub_entities[1].three.should eq('third')
189
+ entity.test_sub_entities[1].one.should eq('first')
190
+
191
+ entity.test_sub_entities[0].test_entity.uuid.should eq('d1c958ba-e6ea-4b12-8335-347289e404ac')
192
+
193
+ end
194
+
195
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flatpack_core
3
+ version: !ruby/object:Gem::Version
4
+ hash: 737972668
5
+ prerelease: true
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - pre
10
+ version: 1.0.pre
11
+ platform: ruby
12
+ authors:
13
+ - Joe Stelmach
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-09-27 00:00:00 +05:30
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: json
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 1
30
+ segments:
31
+ - 1
32
+ - 7
33
+ version: "1.7"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: uuidtools
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 1
45
+ segments:
46
+ - 2
47
+ - 1
48
+ version: "2.1"
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: i18n
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 7
60
+ segments:
61
+ - 0
62
+ - 6
63
+ version: "0.6"
64
+ type: :runtime
65
+ version_requirements: *id003
66
+ - !ruby/object:Gem::Dependency
67
+ name: activesupport
68
+ prerelease: false
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ hash: 7
75
+ segments:
76
+ - 3
77
+ - 0
78
+ version: "3.0"
79
+ type: :runtime
80
+ version_requirements: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec
83
+ prerelease: false
84
+ requirement: &id005 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ hash: 15
90
+ segments:
91
+ - 2
92
+ - 6
93
+ version: "2.6"
94
+ type: :development
95
+ version_requirements: *id005
96
+ description: Write a gem description
97
+ email:
98
+ - joe@getperka.com
99
+ executables: []
100
+
101
+ extensions: []
102
+
103
+ extra_rdoc_files: []
104
+
105
+ files:
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - flatpack_core.gemspec
111
+ - lib/flatpack/core/base_has_uuid.rb
112
+ - lib/flatpack/core/flatpack.rb
113
+ - lib/flatpack/core/map_initialize.rb
114
+ - lib/flatpack/core/packer.rb
115
+ - lib/flatpack/core/unpacker.rb
116
+ - lib/flatpack/core/version.rb
117
+ - lib/flatpack_core.rb
118
+ - spec/core_spec.rb
119
+ has_rdoc: true
120
+ homepage: ""
121
+ licenses: []
122
+
123
+ post_install_message:
124
+ rdoc_options: []
125
+
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ hash: 3
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ">"
141
+ - !ruby/object:Gem::Version
142
+ hash: 25
143
+ segments:
144
+ - 1
145
+ - 3
146
+ - 1
147
+ version: 1.3.1
148
+ requirements: []
149
+
150
+ rubyforge_project:
151
+ rubygems_version: 1.3.7
152
+ signing_key:
153
+ specification_version: 3
154
+ summary: Write a gem summary
155
+ test_files:
156
+ - spec/core_spec.rb