gon-sinatra 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in gon.gemspec
4
+ gemspec
@@ -0,0 +1,70 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gon (2.0.4)
5
+ actionpack (>= 2.3.0)
6
+ jbuilder
7
+ json
8
+ rabl
9
+
10
+ GEM
11
+ remote: http://rubygems.org/
12
+ specs:
13
+ actionpack (3.1.1)
14
+ activemodel (= 3.1.1)
15
+ activesupport (= 3.1.1)
16
+ builder (~> 3.0.0)
17
+ erubis (~> 2.7.0)
18
+ i18n (~> 0.6)
19
+ rack (~> 1.3.2)
20
+ rack-cache (~> 1.1)
21
+ rack-mount (~> 0.8.2)
22
+ rack-test (~> 0.6.1)
23
+ sprockets (~> 2.0.2)
24
+ activemodel (3.1.1)
25
+ activesupport (= 3.1.1)
26
+ builder (~> 3.0.0)
27
+ i18n (~> 0.6)
28
+ activesupport (3.1.1)
29
+ multi_json (~> 1.0)
30
+ blankslate (2.1.2.4)
31
+ builder (3.0.0)
32
+ diff-lcs (1.1.3)
33
+ erubis (2.7.0)
34
+ hike (1.2.1)
35
+ i18n (0.6.0)
36
+ jbuilder (0.3)
37
+ activesupport (>= 3.0.0)
38
+ blankslate (>= 2.1.2.4)
39
+ json (1.6.1)
40
+ multi_json (1.0.4)
41
+ rabl (0.5.1)
42
+ activesupport (>= 2.3.14)
43
+ multi_json (~> 1.0.3)
44
+ rack (1.3.5)
45
+ rack-cache (1.1)
46
+ rack (>= 0.4)
47
+ rack-mount (0.8.3)
48
+ rack (>= 1.0.0)
49
+ rack-test (0.6.1)
50
+ rack (>= 1.0)
51
+ rspec (2.7.0)
52
+ rspec-core (~> 2.7.0)
53
+ rspec-expectations (~> 2.7.0)
54
+ rspec-mocks (~> 2.7.0)
55
+ rspec-core (2.7.1)
56
+ rspec-expectations (2.7.0)
57
+ diff-lcs (~> 1.1.2)
58
+ rspec-mocks (2.7.0)
59
+ sprockets (2.0.3)
60
+ hike (~> 1.2)
61
+ rack (~> 1.0)
62
+ tilt (!= 1.3.0, ~> 1.1)
63
+ tilt (1.3.3)
64
+
65
+ PLATFORMS
66
+ ruby
67
+
68
+ DEPENDENCIES
69
+ gon!
70
+ rspec
@@ -0,0 +1,194 @@
1
+ # Gon-sinatra gem — get your Sinatra variables in your js
2
+
3
+
4
+ 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
+ Now with [Rabl](https://github.com/nesquena/rabl) support!
7
+
8
+ ## Usage
9
+
10
+ `views/application.erb`
11
+
12
+ ``` erb
13
+ <head>
14
+ <title>some title</title>
15
+ <%= include_gon %>
16
+ <!-- include your action js code -->
17
+ ...
18
+ ```
19
+
20
+ To camelize your variables in js you can use:
21
+
22
+ ``` erb
23
+ <head>
24
+ <title>some title</title>
25
+ <%= include_gon(:camel_case => true) %>
26
+ <!-- include your action js code with camelized variables -->
27
+ ...
28
+ ```
29
+
30
+ You can change the namespace of the variables:
31
+
32
+ ``` erb
33
+ <head>
34
+ <title>some title</title>
35
+ <%= include_gon(:namespace => 'serverExports') %>
36
+ <!-- include your action js code with 'serverExports' namespace -->
37
+ ...
38
+ ```
39
+
40
+ You put something like this in the Sinatra action:
41
+
42
+ ``` ruby
43
+ @your_int = 123
44
+ @your_array = [1,2]
45
+ @your_hash = {'a' => 1, 'b' => 2}
46
+ gon.your_int = @your_int
47
+ gon.your_other_int = 345 + gon.your_int
48
+ gon.your_array = @your_array
49
+ gon.your_array << gon.your_int
50
+ gon.your_hash = @your_hash
51
+
52
+ gon.all_variables # > {:your_int => 123, :your_other_int => 468, :your_array => [1, 2, 123], :your_hash => {'a' => 1, 'b' => 2}}
53
+ gon.your_array # > [1, 2, 123]
54
+
55
+ gon.clear # gon.all_variables now is {}
56
+ ```
57
+
58
+ Access the varaibles from your JavaScript file:
59
+
60
+ ``` js
61
+ alert(gon.your_int)
62
+ alert(gon.your_other_int)
63
+ alert(gon.your_array)
64
+ alert(gon.your_hash)
65
+ ```
66
+
67
+ With camelize:
68
+
69
+ ``` js
70
+ alert(gon.yourInt)
71
+ alert(gon.yourOtherInt)
72
+ alert(gon.yourArray)
73
+ alert(gon.yourHash)
74
+ ```
75
+
76
+ With custom namespace and camelize:
77
+
78
+ ``` js
79
+ alert(customNamespace.yourInt)
80
+ alert(customNamespace.yourOtherInt)
81
+ alert(customNamespace.yourArray)
82
+ alert(customNamespace.yourHash)
83
+ ```
84
+
85
+ ## Usage with Rabl
86
+
87
+ Now you can write your variables assign logic to templates with [Rabl](https://github.com/nesquena/rabl).
88
+ The way of writing Rabl templates is very clearly described in their repo.
89
+
90
+ Profit of using Rabl with gon:
91
+
92
+ 1. You can clean your controllers now!
93
+ 2. Work with database objects and collections clearly and easyly
94
+ 3. All power of Rabl
95
+ 4. You can still be lazy and don't use common way to transfer data in js
96
+ 5. And so on
97
+
98
+ For using gon with Rabl you need to create new Rabl template and map gon
99
+ to it.
100
+ For example you have model Post with attributes :title and :body.
101
+ You want to get all your posts in your js as an Array.
102
+ That's what you need to do:
103
+
104
+ 1. Create Rabl template. I prefer creating special directory for
105
+ templates which are not view templates.
106
+
107
+ `goners/posts/index.rabl`
108
+
109
+ ``` rabl
110
+ collection @posts => 'posts'
111
+ attributes :id, :title, :body
112
+ ```
113
+
114
+ 2. All you need to do after that is only to map this template to gon.
115
+
116
+ ``` ruby
117
+ get '/' do
118
+ # some logic
119
+ @posts = [some_objects] # Rabl works with instance variables of controller
120
+
121
+ gon.rabl 'goners/posts/index.rabl'
122
+ # some logic
123
+ end
124
+ ```
125
+
126
+ Thats it! Now you will get in your js gon.posts variable which is Array of
127
+ post objects with attributes :id, :title and :body.
128
+
129
+ In javascript file for view of this action write call to your variable:
130
+
131
+ ``` js
132
+ alert(gon.posts)
133
+ alert(gon.posts[0])
134
+ alert(gon.posts[0].post.body)
135
+ ```
136
+
137
+ P.s. If you didn't put include_gon tag in your html head area - it
138
+ wouldn't work. You can read about this in common usage above.
139
+
140
+ ### Some tips of usage Rabl with gon:
141
+
142
+ If you don't use alias in Rabl template:
143
+
144
+ ``` rabl
145
+ collection @posts
146
+ ....
147
+ ```
148
+
149
+ instead of using that:
150
+
151
+ ``` rabl
152
+ collection @posts => 'alias'
153
+ ....
154
+ ```
155
+
156
+ Rabl will return you an array and gon by default will put it to variable
157
+ gon.rabl
158
+
159
+ Two ways how you can change it - using aliases or you can add alias to
160
+ gon mapping method:
161
+
162
+ ``` ruby
163
+ # your logic stuff here
164
+
165
+ gon.rabl 'path/to/rabl/file', :as => 'alias'
166
+ ```
167
+
168
+ ## Installation
169
+
170
+ Manually install gon-sinatra gem: `$ gem install gon-sinatra`
171
+
172
+ Add requirement to your app file
173
+
174
+ ``` ruby
175
+ require 'gon-sinatra'
176
+ ```
177
+
178
+ ## Contributors
179
+
180
+ * @gazay
181
+
182
+ Special thanks to @brainopia, @kossnocorp and @ai.
183
+
184
+ ## License
185
+
186
+ The MIT License
187
+
188
+ Copyright (c) 2011 gazay
189
+
190
+ 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:
191
+
192
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
193
+
194
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
Binary file
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "gon/sinatra/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "gon-sinatra"
7
+ s.version = Gon::Sinatra::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['gazay']
10
+ s.email = ['alex.gaziev@gmail.com']
11
+ s.homepage = "https://github.com/gazay/gon-sinatra"
12
+ s.summary = %q{Get your Sinatra variables in your JS}
13
+ s.description = %q{If you need to send some data to your js files and you don't want to do this with long way trough views and parsing - use this force!}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+ s.add_dependency "rabl"
20
+ s.add_dependency "sinatra"
21
+ s.add_dependency "json"
22
+ s.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1,77 @@
1
+ require 'sinatra'
2
+ require 'gon/sinatra/helpers'
3
+ require 'gon/sinatra/rabl'
4
+
5
+ module Gon
6
+ module Sinatra
7
+ class << self
8
+ def all_variables
9
+ @request_env[:gon]
10
+ end
11
+
12
+ def clear
13
+ @request_env[:gon] = {}
14
+ end
15
+
16
+ def request_env=(environment)
17
+ @request_env = environment
18
+ @request_env[:gon] ||= {}
19
+ end
20
+
21
+ def request_env
22
+ if defined?(@request_env)
23
+ return @request_env
24
+ end
25
+ end
26
+
27
+ def request
28
+ @request_id if defined? @request_id
29
+ end
30
+
31
+ def request=(request_id)
32
+ @request_id = request_id
33
+ end
34
+
35
+ def method_missing(m, *args, &block)
36
+ if ( m.to_s =~ /=$/ )
37
+ if public_methods.include? m.to_s[0..-2].to_sym
38
+ raise "You can't use Gon public methods for storing data"
39
+ end
40
+ set_variable(m.to_s.delete('='), args[0])
41
+ else
42
+ get_variable(m.to_s)
43
+ end
44
+ end
45
+
46
+ def get_variable(name)
47
+ @request_env[:gon][name]
48
+ end
49
+
50
+ def set_variable(name, value)
51
+ @request_env[:gon][name] = value
52
+ end
53
+
54
+ def rabl(view_path, options = {})
55
+ unless options[:instance]
56
+ raise ArgumentError.new("You should pass :instance in options: :instance => self")
57
+ end
58
+
59
+ rabl_data = Gon::Sinatra::Rabl.parse_rabl(view_path, options[:instance])
60
+
61
+ if options[:as]
62
+ set_variable(options[:as].to_s, rabl_data)
63
+ elsif rabl_data.is_a? Hash
64
+ rabl_data.each do |key, value|
65
+ set_variable(key, value)
66
+ end
67
+ else
68
+ set_variable('rabl', rabl_data)
69
+ end
70
+ end
71
+
72
+ def jbuilder(view_path, options = {})
73
+ raise NoMethodError.new("Not available for sinatra")
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,51 @@
1
+ module Gon
2
+ module Sinatra
3
+ module Helpers
4
+ def self.included base
5
+ base.send(:include, InstanceMethods)
6
+ end
7
+
8
+ module InstanceMethods
9
+ def include_gon(options = {})
10
+ if Gon::Sinatra.request_env && Gon::Sinatra.all_variables.present?
11
+ data = Gon::Sinatra.all_variables
12
+ namespace = options[:namespace] || 'gon'
13
+ script = "<script>window." + namespace + " = {};"
14
+ unless options[:camel_case]
15
+ data.each do |key, val|
16
+ script += namespace + "." + key.to_s + '=' + val.to_s + ";"
17
+ end
18
+ else
19
+ data.each do |key, val|
20
+ script += namespace + "." + key.to_s.camelize(:lower) + '=' + val.to_s + ";"
21
+ end
22
+ end
23
+ script += "</script>"
24
+ script
25
+ else
26
+ ""
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ module GonHelpers
33
+ def self.included base
34
+ base.send(:include, InstanceMethods)
35
+ end
36
+
37
+ module InstanceMethods
38
+ def gon
39
+ if !Gon::Sinatra.request_env || Gon::Sinatra.request != request.object_id
40
+ Gon::Sinatra.request = request.object_id
41
+ Gon::Sinatra.request_env = request.env
42
+ end
43
+ Gon::Sinatra
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ Sinatra::Application.send :include, Gon::Sinatra::Helpers
51
+ Sinatra::Base.send :include, Gon::Sinatra::GonHelpers
@@ -0,0 +1,17 @@
1
+ require 'rabl'
2
+ require 'json'
3
+
4
+ module Gon
5
+ module Sinatra
6
+ module Rabl
7
+ class << self
8
+ def parse_rabl(rabl_path, controller)
9
+ source = File.read(rabl_path)
10
+ rabl_engine = ::Rabl::Engine.new(source, :format => 'json')
11
+ output = rabl_engine.render(controller, {})
12
+ JSON.parse(output)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module Gon
2
+ module Sinatra
3
+ VERSION = '0.0.5'
4
+ end
5
+ end
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,57 @@
1
+ # gon_spec_rb
2
+ require 'gon-sinatra'
3
+
4
+ describe Gon::Sinatra, '#all_variables' do
5
+
6
+ before(:each) do
7
+ Gon::Sinatra.request_env = {}
8
+ end
9
+
10
+ it 'returns all variables in hash' do
11
+ Gon::Sinatra.a = 1
12
+ Gon::Sinatra.b = 2
13
+ Gon::Sinatra.c = Gon::Sinatra.a + Gon::Sinatra.b
14
+ Gon::Sinatra.c.should == 3
15
+ Gon::Sinatra.all_variables.should == {'a' => 1, 'b' => 2, 'c' => 3}
16
+ end
17
+
18
+ it 'supports all data types' do
19
+ Gon::Sinatra.clear
20
+ Gon::Sinatra.int = 1
21
+ Gon::Sinatra.float = 1.1
22
+ Gon::Sinatra.string = 'string'
23
+ Gon::Sinatra.array = [ 1, 'string' ]
24
+ Gon::Sinatra.hash_var = { :a => 1, :b => '2'}
25
+ Gon::Sinatra.hash_w_array = { :a => [ 2, 3 ] }
26
+ Gon::Sinatra.klass = Hash
27
+ end
28
+
29
+ it 'output as js correct' do
30
+ Gon::Sinatra.clear
31
+ Gon::Sinatra.int = 1
32
+ Sinatra::Application.instance_methods.map(&:to_s).include?('include_gon').should == true
33
+
34
+ # TODO: Make it work
35
+ # base = Sinatra::Base.new
36
+ # base.include_gon.should == "<script>window.gon = {};" +
37
+ # "gon.int=1;" +
38
+ # "</script>"
39
+ end
40
+
41
+ it 'returns exception if try to set public method as variable' do
42
+ Gon::Sinatra.clear
43
+ lambda { Gon::Sinatra.all_variables = 123 }.should raise_error
44
+ end
45
+
46
+ it 'render json from rabl template' do
47
+ Gon::Sinatra.clear
48
+ @objects = [1,2]
49
+ Gon::Sinatra.rabl 'spec/test_data/sample.rabl', :instance => self
50
+ Gon::Sinatra.objects.length.should == 2
51
+ end
52
+
53
+ def request
54
+ @request ||= double 'request', :env => {}
55
+ end
56
+
57
+ end
@@ -0,0 +1 @@
1
+ json.objects objects
@@ -0,0 +1 @@
1
+ json.objects @objects
@@ -0,0 +1,2 @@
1
+ collection @objects => 'objects'
2
+ attributes :id
@@ -0,0 +1 @@
1
+ json.partial! 'spec/test_data/_sample_partial.json.jbuilder', :objects => @objects
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gon-sinatra
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - gazay
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-10 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rabl
16
+ requirement: &2157583520 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2157583520
25
+ - !ruby/object:Gem::Dependency
26
+ name: sinatra
27
+ requirement: &2157887260 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2157887260
36
+ - !ruby/object:Gem::Dependency
37
+ name: json
38
+ requirement: &2158090780 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2158090780
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &2158329420 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2158329420
58
+ description: If you need to send some data to your js files and you don't want to
59
+ do this with long way trough views and parsing - use this force!
60
+ email:
61
+ - alex.gaziev@gmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - README.md
69
+ - Rakefile
70
+ - cache/gon-0.2.2.gem
71
+ - gon-sinatra.gemspec
72
+ - lib/gon-sinatra.rb
73
+ - lib/gon/sinatra/helpers.rb
74
+ - lib/gon/sinatra/rabl.rb
75
+ - lib/gon/sinatra/version.rb
76
+ - pkg/gon-0.1.0.gem
77
+ - pkg/gon-0.1.1.gem
78
+ - pkg/gon-0.1.2.gem
79
+ - pkg/gon-0.2.0.gem
80
+ - pkg/gon-0.2.1.gem
81
+ - pkg/gon-0.2.2.gem
82
+ - pkg/gon-0.3.0.gem
83
+ - pkg/gon-1.0.0.gem
84
+ - spec/gon/gon_spec.rb
85
+ - spec/test_data/_sample_partial.json.jbuilder
86
+ - spec/test_data/sample.json.jbuilder
87
+ - spec/test_data/sample.rabl
88
+ - spec/test_data/sample_with_partial.json.jbuilder
89
+ homepage: https://github.com/gazay/gon-sinatra
90
+ licenses: []
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 1.8.10
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: Get your Sinatra variables in your JS
113
+ test_files:
114
+ - spec/gon/gon_spec.rb
115
+ - spec/test_data/_sample_partial.json.jbuilder
116
+ - spec/test_data/sample.json.jbuilder
117
+ - spec/test_data/sample.rabl
118
+ - spec/test_data/sample_with_partial.json.jbuilder