gon-sinatra 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.travis.yml +11 -0
- data/Gemfile.lock +39 -25
- data/README.md +11 -5
- data/Rakefile +6 -1
- data/gon-sinatra.gemspec +2 -0
- data/lib/gon/sinatra/helpers.rb +14 -16
- data/lib/gon/sinatra/rabl.rb +10 -2
- data/lib/gon/sinatra/store.rb +3 -1
- data/lib/gon/sinatra/version.rb +1 -1
- data/spec/gon/gon_spec.rb +25 -11
- metadata +66 -26
- data/cache/gon-0.2.2.gem +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7e56e3d12d31768fbd700f74991c1c7b53ea0491
|
4
|
+
data.tar.gz: ca60e392e27d7a8e93692624a2c7fad8d67ff847
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f91ae8530d26fe0029006be8b46dcfc0e2b8a1e34516f52c772e3a0af69ebc0214d2cbf1879a00851850b1f984a7000e88d0d94897dfa1b608eb51716752d164
|
7
|
+
data.tar.gz: 51b964523843718864cd4817d78d5558c9ce6d4aa0639529bd35301180ad99ff281f0e83c3ef970f8d52cbdc705b67dd35d9bd7eff16d46bb99ab4434a745804
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.8.7
|
4
|
+
- 1.9.2
|
5
|
+
- 1.9.3
|
6
|
+
- jruby-18mode # JRuby in 1.8 mode
|
7
|
+
- jruby-19mode # JRuby in 1.9 mode
|
8
|
+
- rbx-18mode
|
9
|
+
- rbx-19mode
|
10
|
+
# uncomment this line if your project needs to run something other than `rake`:
|
11
|
+
# script: bundle exec rspec spec
|
data/Gemfile.lock
CHANGED
@@ -1,42 +1,56 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
gon-sinatra (0.0
|
5
|
-
json
|
6
|
-
rabl
|
4
|
+
gon-sinatra (0.1.0)
|
7
5
|
sinatra
|
8
6
|
|
9
7
|
GEM
|
10
8
|
remote: http://rubygems.org/
|
11
9
|
specs:
|
12
|
-
activesupport (
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
10
|
+
activesupport (4.1.7)
|
11
|
+
i18n (~> 0.6, >= 0.6.9)
|
12
|
+
json (~> 1.7, >= 1.7.7)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
thread_safe (~> 0.1)
|
15
|
+
tzinfo (~> 1.1)
|
16
|
+
diff-lcs (1.2.5)
|
17
|
+
i18n (0.6.11)
|
18
|
+
json (1.8.1)
|
19
|
+
minitest (5.4.2)
|
20
|
+
rabl (0.11.3)
|
18
21
|
activesupport (>= 2.3.14)
|
19
|
-
|
20
|
-
rack (1.
|
21
|
-
rack-protection (1.2.0)
|
22
|
+
rack (1.5.2)
|
23
|
+
rack-protection (1.5.3)
|
22
24
|
rack
|
23
|
-
|
24
|
-
|
25
|
-
rspec-
|
26
|
-
rspec-
|
27
|
-
|
28
|
-
rspec-
|
29
|
-
|
30
|
-
rspec-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
25
|
+
rake (10.3.2)
|
26
|
+
rspec (3.1.0)
|
27
|
+
rspec-core (~> 3.1.0)
|
28
|
+
rspec-expectations (~> 3.1.0)
|
29
|
+
rspec-mocks (~> 3.1.0)
|
30
|
+
rspec-core (3.1.7)
|
31
|
+
rspec-support (~> 3.1.0)
|
32
|
+
rspec-expectations (3.1.2)
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
34
|
+
rspec-support (~> 3.1.0)
|
35
|
+
rspec-mocks (3.1.3)
|
36
|
+
rspec-support (~> 3.1.0)
|
37
|
+
rspec-support (3.1.2)
|
38
|
+
sinatra (1.4.5)
|
39
|
+
rack (~> 1.4)
|
40
|
+
rack-protection (~> 1.4)
|
41
|
+
tilt (~> 1.3, >= 1.3.4)
|
42
|
+
thread_safe (0.3.4)
|
43
|
+
tilt (1.4.1)
|
44
|
+
tzinfo (1.2.2)
|
45
|
+
thread_safe (~> 0.1)
|
36
46
|
|
37
47
|
PLATFORMS
|
38
48
|
ruby
|
39
49
|
|
40
50
|
DEPENDENCIES
|
41
51
|
gon-sinatra!
|
52
|
+
i18n
|
53
|
+
json
|
54
|
+
rabl
|
55
|
+
rake
|
42
56
|
rspec
|
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# Gon-sinatra gem — get your Sinatra variables in your js
|
2
2
|
|
3
|
+
### Build Status ![http://travis-ci.org/gazay/gon-sinatra](https://secure.travis-ci.org/gazay/gon-sinatra.png)
|
3
4
|
|
4
5
|
If you need to send some data to your js files and you don't want to do this with long way through views and parsing - use this force!
|
5
6
|
|
@@ -13,12 +14,16 @@ For rails use [gon](https://github.com/gazay/gon).
|
|
13
14
|
|
14
15
|
`my_sinatra_application.rb`
|
15
16
|
|
17
|
+
**Note: For classic (non-modular) applications, you still have to explicitely register `Gon::Sinatra`**
|
18
|
+
|
16
19
|
``` ruby
|
17
|
-
|
20
|
+
# For classic applications:
|
21
|
+
|
22
|
+
Sinatra::register Gon::Sinatra
|
18
23
|
# and if you want to the use Rabl integration
|
19
|
-
register Gon::Sinatra::Rabl
|
24
|
+
Sinatra::register Gon::Sinatra::Rabl
|
20
25
|
|
21
|
-
#
|
26
|
+
# For modular applications:
|
22
27
|
|
23
28
|
class MySinatraApplication < Sinatra::Base #or Padrino::Application
|
24
29
|
register Gon::Sinatra
|
@@ -115,7 +120,7 @@ Profit of using Rabl with gon:
|
|
115
120
|
5. And so on
|
116
121
|
|
117
122
|
For using gon with Rabl you need to create new Rabl template and map gon
|
118
|
-
to it.
|
123
|
+
to it.
|
119
124
|
For example you have model Post with attributes :title and :body.
|
120
125
|
You want to get all your posts in your js as an Array.
|
121
126
|
That's what you need to do:
|
@@ -197,6 +202,7 @@ require 'gon-sinatra'
|
|
197
202
|
## Contributors
|
198
203
|
|
199
204
|
* @gazay
|
205
|
+
* @skade
|
200
206
|
|
201
207
|
Special thanks to @brainopia, @kossnocorp and @ai.
|
202
208
|
|
@@ -204,7 +210,7 @@ Special thanks to @brainopia, @kossnocorp and @ai.
|
|
204
210
|
|
205
211
|
The MIT License
|
206
212
|
|
207
|
-
Copyright (c)
|
213
|
+
Copyright (c) 2014 gazay
|
208
214
|
|
209
215
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
210
216
|
|
data/Rakefile
CHANGED
data/gon-sinatra.gemspec
CHANGED
@@ -19,6 +19,8 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.add_dependency "sinatra"
|
20
20
|
|
21
21
|
s.add_development_dependency "rabl"
|
22
|
+
s.add_development_dependency "i18n"
|
22
23
|
s.add_development_dependency "json"
|
23
24
|
s.add_development_dependency "rspec"
|
25
|
+
s.add_development_dependency "rake"
|
24
26
|
end
|
data/lib/gon/sinatra/helpers.rb
CHANGED
@@ -4,24 +4,22 @@ module Gon
|
|
4
4
|
module Sinatra
|
5
5
|
module Helpers
|
6
6
|
def include_gon(options = {})
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
else
|
16
|
-
data.each do |key, val|
|
17
|
-
script += namespace + "." + key.to_s.camelize(:lower) + '=' + val.to_json + ";"
|
18
|
-
end
|
7
|
+
return '' if gon.all_variables.empty?
|
8
|
+
|
9
|
+
data = gon.all_variables
|
10
|
+
namespace = options[:namespace] || 'gon'
|
11
|
+
script = "<script>window." + namespace + " = {};"
|
12
|
+
unless options[:camel_case]
|
13
|
+
data.each do |key, val|
|
14
|
+
script += namespace + "." + key.to_s + '=' + val.to_json + ";"
|
19
15
|
end
|
20
|
-
script += "</script>"
|
21
|
-
script
|
22
16
|
else
|
23
|
-
|
17
|
+
data.each do |key, val|
|
18
|
+
script += namespace + "." + key.to_s.camelize(:lower) + '=' + val.to_json + ";"
|
19
|
+
end
|
24
20
|
end
|
21
|
+
script += "</script>"
|
22
|
+
script
|
25
23
|
end
|
26
24
|
end
|
27
25
|
|
@@ -32,4 +30,4 @@ module Gon
|
|
32
30
|
end
|
33
31
|
end
|
34
32
|
end
|
35
|
-
end
|
33
|
+
end
|
data/lib/gon/sinatra/rabl.rb
CHANGED
@@ -1,14 +1,22 @@
|
|
1
1
|
require 'rabl'
|
2
|
+
require 'tilt'
|
2
3
|
|
3
4
|
module Gon
|
4
5
|
module Sinatra
|
5
6
|
module Rabl
|
6
7
|
class << self
|
8
|
+
def cache
|
9
|
+
@cache ||= Tilt::Cache.new
|
10
|
+
end
|
11
|
+
|
7
12
|
def parse_rabl(rabl_path, controller)
|
8
|
-
source =
|
13
|
+
source = cache.fetch(rabl_path) do
|
14
|
+
File.read(rabl_path)
|
15
|
+
end
|
16
|
+
|
9
17
|
rabl_engine = ::Rabl::Engine.new(source, :format => 'json')
|
10
18
|
output = rabl_engine.render(controller, {})
|
11
|
-
::Rabl.configuration.json_engine.
|
19
|
+
::Rabl.configuration.json_engine.parse(output)
|
12
20
|
end
|
13
21
|
end
|
14
22
|
end
|
data/lib/gon/sinatra/store.rb
CHANGED
@@ -38,7 +38,9 @@ module Gon
|
|
38
38
|
alias :set :set_variable
|
39
39
|
|
40
40
|
def rabl(view_path, options = {})
|
41
|
-
|
41
|
+
unless defined?(::Rabl)
|
42
|
+
raise Exception.new("You must require rabl and register Gon::Sinatra::Rabl to use rabl")
|
43
|
+
end
|
42
44
|
|
43
45
|
unless options[:instance]
|
44
46
|
raise ArgumentError.new("You should pass :instance in options: :instance => self")
|
data/lib/gon/sinatra/version.rb
CHANGED
data/spec/gon/gon_spec.rb
CHANGED
@@ -6,7 +6,7 @@ class App < Sinatra::Base
|
|
6
6
|
register Gon::Sinatra::Rabl
|
7
7
|
end
|
8
8
|
|
9
|
-
describe Gon::Sinatra, '#all_variables' do
|
9
|
+
describe Gon::Sinatra, '#all_variables' do
|
10
10
|
def app
|
11
11
|
app = App.new!
|
12
12
|
app.env = {}
|
@@ -15,14 +15,15 @@ describe Gon::Sinatra, '#all_variables' do
|
|
15
15
|
|
16
16
|
before(:each) do
|
17
17
|
@gon = Gon::Sinatra::Store.new({})
|
18
|
+
Gon::Sinatra::Rabl.cache.clear
|
18
19
|
end
|
19
20
|
|
20
21
|
it 'returns all variables in hash' do
|
21
22
|
@gon.a = 1
|
22
23
|
@gon.b = 2
|
23
24
|
@gon.c = @gon.a + @gon.b
|
24
|
-
@gon.c.
|
25
|
-
@gon.all_variables.
|
25
|
+
expect(@gon.c).to eq(3)
|
26
|
+
expect(@gon.all_variables).to eq({'a' => 1, 'b' => 2, 'c' => 3})
|
26
27
|
end
|
27
28
|
|
28
29
|
it 'supports all data types' do
|
@@ -40,17 +41,16 @@ describe Gon::Sinatra, '#all_variables' do
|
|
40
41
|
instance = app
|
41
42
|
|
42
43
|
instance.gon.int = 1
|
43
|
-
instance.methods.map(&:to_s).include
|
44
|
+
expect(instance.methods.map(&:to_s)).to include('include_gon')
|
44
45
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
"</script>"
|
46
|
+
expect(instance.include_gon).to eq("<script>window.gon = {};" +
|
47
|
+
"gon.int=1;" +
|
48
|
+
"</script>")
|
49
49
|
end
|
50
50
|
|
51
51
|
it 'returns exception if try to set public method as variable' do
|
52
52
|
@gon.clear
|
53
|
-
|
53
|
+
expect { @gon.all_variables = 123 }.to raise_error
|
54
54
|
end
|
55
55
|
|
56
56
|
it 'should be threadsafe' do
|
@@ -59,14 +59,28 @@ describe Gon::Sinatra, '#all_variables' do
|
|
59
59
|
|
60
60
|
instance1.gon.test = "foo"
|
61
61
|
instance2.gon.test = "bar"
|
62
|
-
instance1.gon.test.
|
62
|
+
expect(instance1.gon.test).to eq("foo")
|
63
63
|
end
|
64
64
|
|
65
65
|
it 'render json from rabl template' do
|
66
66
|
@gon.clear
|
67
67
|
@objects = [1,2]
|
68
68
|
@gon.rabl 'spec/test_data/sample.rabl', :instance => self
|
69
|
-
@gon.objects.length.
|
69
|
+
expect(@gon.objects.length).to eq(2)
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'caches the rabl template' do
|
73
|
+
@gon.clear
|
74
|
+
@objects = [1,2]
|
75
|
+
path = 'spec/test_data/sample.rabl'
|
76
|
+
source = File.read(path)
|
77
|
+
expect(File).to receive(:read).once.and_return(source)
|
78
|
+
@gon.rabl path, :instance => self
|
79
|
+
|
80
|
+
@gon.clear
|
81
|
+
@objects = [1,2,3]
|
82
|
+
@gon.rabl 'spec/test_data/sample.rabl', :instance => self
|
83
|
+
expect(@gon.objects.length).to eq(3)
|
70
84
|
end
|
71
85
|
|
72
86
|
def request
|
metadata
CHANGED
@@ -1,60 +1,99 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gon-sinatra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- gazay
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-11-04 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: sinatra
|
16
|
-
requirement:
|
17
|
-
none: false
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
25
27
|
- !ruby/object:Gem::Dependency
|
26
28
|
name: rabl
|
27
|
-
requirement:
|
28
|
-
none: false
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
29
30
|
requirements:
|
30
|
-
- -
|
31
|
+
- - ">="
|
31
32
|
- !ruby/object:Gem::Version
|
32
33
|
version: '0'
|
33
34
|
type: :development
|
34
35
|
prerelease: false
|
35
|
-
version_requirements:
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: i18n
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
36
55
|
- !ruby/object:Gem::Dependency
|
37
56
|
name: json
|
38
|
-
requirement:
|
39
|
-
none: false
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
40
58
|
requirements:
|
41
|
-
- -
|
59
|
+
- - ">="
|
42
60
|
- !ruby/object:Gem::Version
|
43
61
|
version: '0'
|
44
62
|
type: :development
|
45
63
|
prerelease: false
|
46
|
-
version_requirements:
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
47
69
|
- !ruby/object:Gem::Dependency
|
48
70
|
name: rspec
|
49
|
-
requirement:
|
50
|
-
none: false
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
51
72
|
requirements:
|
52
|
-
- -
|
73
|
+
- - ">="
|
53
74
|
- !ruby/object:Gem::Version
|
54
75
|
version: '0'
|
55
76
|
type: :development
|
56
77
|
prerelease: false
|
57
|
-
version_requirements:
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
58
97
|
description: If you need to send some data to your js files and you don't want to
|
59
98
|
do this with long way trough views and parsing - use this force!
|
60
99
|
email:
|
@@ -63,11 +102,12 @@ executables: []
|
|
63
102
|
extensions: []
|
64
103
|
extra_rdoc_files: []
|
65
104
|
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".travis.yml"
|
66
107
|
- Gemfile
|
67
108
|
- Gemfile.lock
|
68
109
|
- README.md
|
69
110
|
- Rakefile
|
70
|
-
- cache/gon-0.2.2.gem
|
71
111
|
- gon-sinatra.gemspec
|
72
112
|
- lib/gon-sinatra.rb
|
73
113
|
- lib/gon/sinatra/helpers.rb
|
@@ -78,28 +118,28 @@ files:
|
|
78
118
|
- spec/test_data/sample.rabl
|
79
119
|
homepage: https://github.com/gazay/gon-sinatra
|
80
120
|
licenses: []
|
121
|
+
metadata: {}
|
81
122
|
post_install_message:
|
82
123
|
rdoc_options: []
|
83
124
|
require_paths:
|
84
125
|
- lib
|
85
126
|
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
-
none: false
|
87
127
|
requirements:
|
88
|
-
- -
|
128
|
+
- - ">="
|
89
129
|
- !ruby/object:Gem::Version
|
90
130
|
version: '0'
|
91
131
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
-
none: false
|
93
132
|
requirements:
|
94
|
-
- -
|
133
|
+
- - ">="
|
95
134
|
- !ruby/object:Gem::Version
|
96
135
|
version: '0'
|
97
136
|
requirements: []
|
98
137
|
rubyforge_project:
|
99
|
-
rubygems_version:
|
138
|
+
rubygems_version: 2.2.2
|
100
139
|
signing_key:
|
101
|
-
specification_version:
|
140
|
+
specification_version: 4
|
102
141
|
summary: Get your Sinatra variables in your JS
|
103
142
|
test_files:
|
104
143
|
- spec/gon/gon_spec.rb
|
105
144
|
- spec/test_data/sample.rabl
|
145
|
+
has_rdoc:
|
data/cache/gon-0.2.2.gem
DELETED
Binary file
|