simplifyapi 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +6 -0
- data/lib/simplifyapi.rb +10 -0
- data/lib/simplifyapi/builder.rb +31 -0
- data/lib/simplifyapi/collection.rb +56 -0
- data/lib/simplifyapi/collection_representer.rb +43 -0
- data/lib/simplifyapi/dsl_methods.rb +27 -0
- data/lib/simplifyapi/importer.rb +27 -0
- data/lib/simplifyapi/property.rb +44 -0
- data/lib/simplifyapi/representation.rb +22 -0
- data/lib/simplifyapi/representer.rb +58 -0
- data/lib/simplifyapi/version.rb +3 -0
- data/simplifyapi.gemspec +22 -0
- data/spec/simplifyapi/representer_spec.rb +399 -0
- data/spec/spec_helper.rb +4 -0
- metadata +130 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 JH. Chabran
|
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
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Simplifyapi
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'simplifyapi'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install simplifyapi
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/simplifyapi.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require "simplifyapi/version"
|
2
|
+
require "simplifyapi/property"
|
3
|
+
require "simplifyapi/collection"
|
4
|
+
require "simplifyapi/representation"
|
5
|
+
require "simplifyapi/representer"
|
6
|
+
require "simplifyapi/collection_representer"
|
7
|
+
|
8
|
+
module Simplifyapi
|
9
|
+
# Your code goes here...
|
10
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Simplifyapi
|
2
|
+
class Builder
|
3
|
+
include DSLMethods
|
4
|
+
|
5
|
+
def export representer, model
|
6
|
+
@store = {}
|
7
|
+
@representer = representer
|
8
|
+
@model = model
|
9
|
+
|
10
|
+
# Registering keys and properties
|
11
|
+
@block.call(self, @model) if @block
|
12
|
+
|
13
|
+
hash = {}
|
14
|
+
|
15
|
+
# Then we play the score accordingly
|
16
|
+
@store.each do |key, object|
|
17
|
+
if object.is_a? Builder
|
18
|
+
hash[key] = object.export(representer, model)
|
19
|
+
else
|
20
|
+
if object.respond_to? :export
|
21
|
+
hash[key] = object.export(@model)
|
22
|
+
else
|
23
|
+
hash[key] = object
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
hash
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Simplifyapi
|
2
|
+
class Collection
|
3
|
+
def initialize name, representer, &block
|
4
|
+
@name = name
|
5
|
+
@representer = representer
|
6
|
+
|
7
|
+
instance_eval &block if block_given?
|
8
|
+
end
|
9
|
+
|
10
|
+
def import
|
11
|
+
raise "TODO"
|
12
|
+
end
|
13
|
+
|
14
|
+
# Depending on how you use a collection, it can
|
15
|
+
# be invoked with an array or with a model
|
16
|
+
# if you're defining nested collections
|
17
|
+
# like a has_many relationship
|
18
|
+
def export model_or_collection
|
19
|
+
if getter_defined?
|
20
|
+
result = @getter.call model_or_collection
|
21
|
+
else
|
22
|
+
if model_or_collection.is_a? Array
|
23
|
+
result = model_or_collection
|
24
|
+
else
|
25
|
+
result = model_or_collection.send(@name)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
wrap_representers result
|
30
|
+
end
|
31
|
+
|
32
|
+
def wrap_representers collection
|
33
|
+
collection.map do |item|
|
34
|
+
@representer.export(item)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def getter_defined?
|
39
|
+
!!@getter
|
40
|
+
end
|
41
|
+
|
42
|
+
def setter_defined?
|
43
|
+
!!@setter
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def get &block
|
48
|
+
@getter = block
|
49
|
+
end
|
50
|
+
|
51
|
+
def set &block
|
52
|
+
@setter = block
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
|
4
|
+
module Simplifyapi
|
5
|
+
module CollectionRepresenter
|
6
|
+
extend ::ActiveSupport::Concern
|
7
|
+
|
8
|
+
def initialize collection
|
9
|
+
@collection = collection
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_json
|
13
|
+
self.class.export(@collection).to_json
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
attr_reader :collection
|
19
|
+
|
20
|
+
def representer representer_klass
|
21
|
+
@collection = Collection.new(name, representer_klass)
|
22
|
+
end
|
23
|
+
|
24
|
+
def representation &block
|
25
|
+
return @representation unless block_given?
|
26
|
+
|
27
|
+
@representation = Representation.new &block
|
28
|
+
end
|
29
|
+
|
30
|
+
def export collection
|
31
|
+
@representation.export(self, collection)
|
32
|
+
end
|
33
|
+
|
34
|
+
def represents_model?
|
35
|
+
false
|
36
|
+
end
|
37
|
+
|
38
|
+
def represents_collection?
|
39
|
+
true
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Simplifyapi
|
2
|
+
module DSLMethods
|
3
|
+
def initialize &block
|
4
|
+
@block = block
|
5
|
+
end
|
6
|
+
|
7
|
+
def method_missing method_id, *args, &block
|
8
|
+
if args.length > 0 && !block_given?
|
9
|
+
@store[method_id] = args.first
|
10
|
+
else
|
11
|
+
@store[method_id] = self.class.new(&block)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def property name
|
16
|
+
@store[name] = @representer.properties[name.to_sym]
|
17
|
+
end
|
18
|
+
|
19
|
+
def collection name
|
20
|
+
if @representer.represents_model?
|
21
|
+
@store[name] = @representer.collections[name.to_sym]
|
22
|
+
else
|
23
|
+
@store[name] = @representer.collection
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Simplifyapi
|
2
|
+
class Importer
|
3
|
+
include DSLMethods
|
4
|
+
|
5
|
+
def import representer, model, hash
|
6
|
+
@store = {}
|
7
|
+
@representer = representer
|
8
|
+
@model = model
|
9
|
+
|
10
|
+
# Registering keys and properties
|
11
|
+
@block.call(self, @model) if @block
|
12
|
+
|
13
|
+
# Then we play the score accordingly
|
14
|
+
@store.each do |key, object|
|
15
|
+
if object.is_a? Importer
|
16
|
+
object.import representer, model, hash[key.to_s]
|
17
|
+
else
|
18
|
+
if hash && hash.has_key?(key) && object.respond_to?(:import)
|
19
|
+
object.import model, hash[key]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
hash
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Simplifyapi
|
2
|
+
class Property
|
3
|
+
attr_reader :name, :getter, :setter
|
4
|
+
|
5
|
+
def initialize name, &block
|
6
|
+
@name = name
|
7
|
+
|
8
|
+
instance_eval &block if block_given?
|
9
|
+
end
|
10
|
+
|
11
|
+
def import model, value
|
12
|
+
if setter_defined?
|
13
|
+
@setter.call model, value
|
14
|
+
else
|
15
|
+
model.send "#{name}=", value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def export model
|
20
|
+
if getter_defined?
|
21
|
+
@getter.call model
|
22
|
+
else
|
23
|
+
model.send name
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def getter_defined?
|
28
|
+
!!@getter
|
29
|
+
end
|
30
|
+
|
31
|
+
def setter_defined?
|
32
|
+
!!@setter
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
def get &block
|
37
|
+
@getter = block
|
38
|
+
end
|
39
|
+
|
40
|
+
def set &block
|
41
|
+
@setter = block
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'simplifyapi/dsl_methods'
|
2
|
+
require 'simplifyapi/builder'
|
3
|
+
require 'simplifyapi/importer'
|
4
|
+
|
5
|
+
module Simplifyapi
|
6
|
+
class Representation
|
7
|
+
def initialize &block
|
8
|
+
@builder = Builder.new &block
|
9
|
+
@importer = Importer.new &block
|
10
|
+
end
|
11
|
+
|
12
|
+
def export representer, model
|
13
|
+
@builder.export representer, model
|
14
|
+
end
|
15
|
+
|
16
|
+
def import representer, model, hash
|
17
|
+
@importer.import representer, model, hash
|
18
|
+
|
19
|
+
model
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
|
4
|
+
module Simplifyapi
|
5
|
+
module Representer
|
6
|
+
extend ::ActiveSupport::Concern
|
7
|
+
|
8
|
+
def initialize model
|
9
|
+
@model = model
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_json
|
13
|
+
self.class.export(@model).to_json
|
14
|
+
end
|
15
|
+
|
16
|
+
def from_json json
|
17
|
+
self.class.import @model, ActiveSupport::JSON.decode(json)
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
attr_reader :properties, :collections
|
22
|
+
|
23
|
+
def property name, &block
|
24
|
+
@properties ||= {}
|
25
|
+
|
26
|
+
@properties[name] = Property.new(name, &block)
|
27
|
+
end
|
28
|
+
|
29
|
+
def collection name, representer, &block
|
30
|
+
@collections ||= {}
|
31
|
+
|
32
|
+
@collections[name] = Collection.new(name, representer, &block)
|
33
|
+
end
|
34
|
+
|
35
|
+
def representation &block
|
36
|
+
return @representation unless block_given?
|
37
|
+
|
38
|
+
@representation = Representation.new &block
|
39
|
+
end
|
40
|
+
|
41
|
+
def export model
|
42
|
+
@representation.export(self, model)
|
43
|
+
end
|
44
|
+
|
45
|
+
def import model, hash
|
46
|
+
@representation.import(self, model, hash.with_indifferent_access)
|
47
|
+
end
|
48
|
+
|
49
|
+
def represents_model?
|
50
|
+
true
|
51
|
+
end
|
52
|
+
|
53
|
+
def represents_collection?
|
54
|
+
false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/simplifyapi.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/simplifyapi/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["JH. Chabran"]
|
6
|
+
gem.email = ["jh@chabran.fr"]
|
7
|
+
gem.description = %q{Export and import json from a definable format}
|
8
|
+
gem.summary = gem.description
|
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 = "simplifyapi"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Simplifyapi::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'activesupport', '~> 3.2.6'
|
19
|
+
gem.add_dependency 'rake'
|
20
|
+
gem.add_dependency 'i18n'
|
21
|
+
gem.add_development_dependency 'rspec', '~> 2.6'
|
22
|
+
end
|
@@ -0,0 +1,399 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Simplifyapi
|
4
|
+
describe Representer do
|
5
|
+
it "should declare a simple property" do
|
6
|
+
class Dummy
|
7
|
+
include Simplifyapi::Representer
|
8
|
+
|
9
|
+
property :title
|
10
|
+
end
|
11
|
+
|
12
|
+
Dummy.properties.should include(:title)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should declare a property with a getter" do
|
16
|
+
class Dummy
|
17
|
+
include Simplifyapi::Representer
|
18
|
+
|
19
|
+
property :title do
|
20
|
+
get do |model|
|
21
|
+
model.formatted_title
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
Dummy.properties[:title].getter_defined?.should be(true)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should declare a property with a setter" do
|
31
|
+
class Dummy
|
32
|
+
include Simplifyapi::Representer
|
33
|
+
|
34
|
+
property :title do
|
35
|
+
set do |model, value|
|
36
|
+
model.formatted_title = value
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
Dummy.properties[:title].setter_defined?.should be(true)
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "Representation" do
|
45
|
+
before :each do
|
46
|
+
class Dummy
|
47
|
+
include Simplifyapi::Representer
|
48
|
+
|
49
|
+
property :title do
|
50
|
+
get do |model|
|
51
|
+
model.formatted_title
|
52
|
+
end
|
53
|
+
|
54
|
+
set do |model, value|
|
55
|
+
model.formatted_title = value
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
representation do |json|
|
60
|
+
json.meta do |meta|
|
61
|
+
meta.property :title
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should declare a representation" do
|
68
|
+
Dummy.representation.should_not be_nil
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should export json" do
|
72
|
+
hash = {meta:{title:"some title"}}
|
73
|
+
|
74
|
+
model = Struct.new(:formatted_title).new("some title")
|
75
|
+
Dummy.new(model).to_json.should ==(hash.to_json)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should import json" do
|
79
|
+
hash = {meta:{title:"some title"}}
|
80
|
+
model = Struct.new(:formatted_title).new
|
81
|
+
|
82
|
+
Dummy.new(model).from_json(hash.to_json).formatted_title.should ==("some title")
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "Complex representation" do
|
87
|
+
before :each do
|
88
|
+
class Dummy
|
89
|
+
include Simplifyapi::Representer
|
90
|
+
|
91
|
+
property :title do
|
92
|
+
get do |model|
|
93
|
+
model.formatted_title
|
94
|
+
end
|
95
|
+
|
96
|
+
set do |model, value|
|
97
|
+
model.formatted_title = value
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
property :position do
|
102
|
+
get do |model|
|
103
|
+
{:x => model.position_x, :y => model.position_y}
|
104
|
+
end
|
105
|
+
|
106
|
+
set do |model, value|
|
107
|
+
model.position_x = value['x']
|
108
|
+
model.position_y = value['y']
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
representation do |json|
|
113
|
+
json.meta do |meta|
|
114
|
+
meta.property :title
|
115
|
+
end
|
116
|
+
|
117
|
+
json.property :position
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should export json" do
|
123
|
+
hash = {:meta=>{:title => "test"}, :position => {:x => 4, :y => 5}}
|
124
|
+
|
125
|
+
model = Struct.new(:formatted_title, :position_x, :position_y).new("test", 4,5)
|
126
|
+
|
127
|
+
Dummy.new(model).to_json.should ==(hash.to_json)
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should import json" do
|
131
|
+
hash = {:meta=>{:title => "test"}, :position => {:x => 4, :y => 5}}
|
132
|
+
|
133
|
+
model = Struct.new(:formatted_title, :position_x, :position_y).new
|
134
|
+
|
135
|
+
@model = Dummy.new(model).from_json(hash.to_json)
|
136
|
+
@model.formatted_title.should ==("test")
|
137
|
+
@model.position_x.should ==(4)
|
138
|
+
@model.position_y.should ==(5)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
describe "Immediate values" do
|
143
|
+
before :each do
|
144
|
+
@hash = {:meta=>{:version => 4, :title => "test", :stuff => "stuff"}}
|
145
|
+
@model = Struct.new(:formatted_title, :stuff).new("test", "stuff")
|
146
|
+
|
147
|
+
class Dummy
|
148
|
+
include Simplifyapi::Representer
|
149
|
+
|
150
|
+
property :title do
|
151
|
+
get do |model|
|
152
|
+
model.formatted_title
|
153
|
+
end
|
154
|
+
|
155
|
+
set do |model, title|
|
156
|
+
model.formatted_title = title
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
representation do |json, model|
|
161
|
+
json.meta do |meta|
|
162
|
+
meta.version 4
|
163
|
+
meta.property :title
|
164
|
+
meta.stuff model.stuff
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should export immediate values " do
|
171
|
+
Dummy.new(@model).to_json.should ==(@hash.to_json)
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should ignore it when importing" do
|
175
|
+
Dummy.new(@model).from_json(@hash.to_json).formatted_title.should ==('test')
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should not whine about it if it is absent" do
|
179
|
+
Dummy.new(@model).from_json(@hash.except(:meta).to_json)
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should not import nil when a property isn't present" do
|
183
|
+
Dummy.import(@model, {:meta => {:version => 4}})
|
184
|
+
@model.formatted_title.should ==("test")
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
describe "Including a module" do
|
190
|
+
before :each do
|
191
|
+
@hash = {:meta=>{:title => "test", :stuff => "stuff"}}
|
192
|
+
@model = Struct.new(:formatted_title, :stuff).new("test", "stuff")
|
193
|
+
|
194
|
+
module Stuff
|
195
|
+
def stuff
|
196
|
+
"stuff"
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
class Dummy
|
201
|
+
include Simplifyapi::Representer
|
202
|
+
|
203
|
+
property :title do
|
204
|
+
get do |model|
|
205
|
+
model.formatted_title
|
206
|
+
end
|
207
|
+
|
208
|
+
set do |model, value|
|
209
|
+
model.formatted_title = value
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
representation do |json|
|
214
|
+
extend Stuff
|
215
|
+
|
216
|
+
json.meta do |meta|
|
217
|
+
meta.property :title
|
218
|
+
meta.stuff stuff
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should call stuff" do
|
225
|
+
Dummy.new(@model).to_json.should include("stuff")
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
describe "Nested collections" do
|
230
|
+
before :each do
|
231
|
+
class Dummy
|
232
|
+
include Simplifyapi::Representer
|
233
|
+
|
234
|
+
property :title do
|
235
|
+
get do |model|
|
236
|
+
model.formatted_title
|
237
|
+
end
|
238
|
+
|
239
|
+
set do |model, value|
|
240
|
+
model.formatted_title = value
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
representation do |json|
|
245
|
+
json.meta do |meta|
|
246
|
+
meta.property :title
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
class NestedDummies
|
252
|
+
include Simplifyapi::Representer
|
253
|
+
|
254
|
+
collection :users, Dummy do
|
255
|
+
get { |model| model.dummies }
|
256
|
+
end
|
257
|
+
|
258
|
+
representation do |json|
|
259
|
+
json.meta do |meta|
|
260
|
+
meta.page 0
|
261
|
+
meta.total_pages 4
|
262
|
+
end
|
263
|
+
|
264
|
+
json.collection :users
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
@a = {:meta=>{:title => "test"}}
|
269
|
+
@b = {:meta=>{:title => "test2"}}
|
270
|
+
|
271
|
+
@collection_klass = Struct.new(:dummies)
|
272
|
+
@model_klass = Struct.new(:formatted_title)
|
273
|
+
@collection = @collection_klass.new [@model_klass.new("test"), @model_klass.new("test2")]
|
274
|
+
|
275
|
+
@hash = {
|
276
|
+
:meta => {
|
277
|
+
:page => 0,
|
278
|
+
:total_pages => 4
|
279
|
+
},
|
280
|
+
:users => [@a, @b]
|
281
|
+
}
|
282
|
+
end
|
283
|
+
|
284
|
+
it "should export the collection" do
|
285
|
+
NestedDummies.new(@collection).to_json.should ==(@hash.to_json)
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
describe "Standalone collections" do
|
290
|
+
before :each do
|
291
|
+
class Dummy
|
292
|
+
include Simplifyapi::Representer
|
293
|
+
|
294
|
+
property :title do
|
295
|
+
get do |model|
|
296
|
+
model.formatted_title
|
297
|
+
end
|
298
|
+
|
299
|
+
set do |model, value|
|
300
|
+
model.formatted_title = value
|
301
|
+
end
|
302
|
+
end
|
303
|
+
|
304
|
+
representation do |json|
|
305
|
+
json.meta do |meta|
|
306
|
+
meta.property :title
|
307
|
+
end
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
class DummyCollection
|
312
|
+
include Simplifyapi::CollectionRepresenter
|
313
|
+
|
314
|
+
representer Dummy
|
315
|
+
|
316
|
+
representation do |json|
|
317
|
+
json.meta do |meta|
|
318
|
+
meta.page 0
|
319
|
+
meta.total_pages 4
|
320
|
+
end
|
321
|
+
|
322
|
+
json.collection :users
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
@a = {:meta=>{:title => "test"}}
|
327
|
+
@b = {:meta=>{:title => "test2"}}
|
328
|
+
|
329
|
+
@model_klass = Struct.new(:formatted_title)
|
330
|
+
@collection = [@model_klass.new("test"), @model_klass.new("test2")]
|
331
|
+
|
332
|
+
@hash = {
|
333
|
+
:meta => {
|
334
|
+
:page => 0,
|
335
|
+
:total_pages => 4
|
336
|
+
},
|
337
|
+
:users => [@a, @b]
|
338
|
+
}
|
339
|
+
end
|
340
|
+
|
341
|
+
it "should export the collection" do
|
342
|
+
DummyCollection.new(@collection).to_json.should ==(@hash.to_json)
|
343
|
+
end
|
344
|
+
end
|
345
|
+
|
346
|
+
describe "Issue" do
|
347
|
+
before :each do
|
348
|
+
@model_klass = Struct.new(:foo)
|
349
|
+
@another_klass = Struct.new(:bar)
|
350
|
+
|
351
|
+
class Dummy
|
352
|
+
include Simplifyapi::Representer
|
353
|
+
|
354
|
+
property :foo
|
355
|
+
representation do |json|
|
356
|
+
json.meta do |meta|
|
357
|
+
meta.property :foo
|
358
|
+
end
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
class Another
|
363
|
+
include Simplifyapi::Representer
|
364
|
+
|
365
|
+
property :bar
|
366
|
+
representation do |json|
|
367
|
+
json.meta do |meta|
|
368
|
+
meta.property :bar
|
369
|
+
end
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
class DummyCollection
|
374
|
+
include Simplifyapi::CollectionRepresenter
|
375
|
+
|
376
|
+
representer Dummy
|
377
|
+
|
378
|
+
representation do |json|
|
379
|
+
json.collection :dummies
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
383
|
+
class AnotherCollection
|
384
|
+
include Simplifyapi::CollectionRepresenter
|
385
|
+
|
386
|
+
representer Another
|
387
|
+
|
388
|
+
representation do |json|
|
389
|
+
json.collection :anothers
|
390
|
+
end
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
394
|
+
it "should not share representer" do
|
395
|
+
DummyCollection.new([@model_klass.new("test"), @model_klass.new("test2")]).to_json
|
396
|
+
AnotherCollection.new([@another_klass.new("another"), @another_klass.new("another2")]).to_json
|
397
|
+
end
|
398
|
+
end
|
399
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simplifyapi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- JH. Chabran
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.6
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.6
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: i18n
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '2.6'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '2.6'
|
78
|
+
description: Export and import json from a definable format
|
79
|
+
email:
|
80
|
+
- jh@chabran.fr
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- lib/simplifyapi.rb
|
91
|
+
- lib/simplifyapi/builder.rb
|
92
|
+
- lib/simplifyapi/collection.rb
|
93
|
+
- lib/simplifyapi/collection_representer.rb
|
94
|
+
- lib/simplifyapi/dsl_methods.rb
|
95
|
+
- lib/simplifyapi/importer.rb
|
96
|
+
- lib/simplifyapi/property.rb
|
97
|
+
- lib/simplifyapi/representation.rb
|
98
|
+
- lib/simplifyapi/representer.rb
|
99
|
+
- lib/simplifyapi/version.rb
|
100
|
+
- simplifyapi.gemspec
|
101
|
+
- spec/simplifyapi/representer_spec.rb
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
homepage: ''
|
104
|
+
licenses: []
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 1.8.24
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: Export and import json from a definable format
|
127
|
+
test_files:
|
128
|
+
- spec/simplifyapi/representer_spec.rb
|
129
|
+
- spec/spec_helper.rb
|
130
|
+
has_rdoc:
|