gon-sinatra 0.0.6 → 0.1.0

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/README.md CHANGED
@@ -5,8 +5,27 @@ If you need to send some data to your js files and you don't want to do this wit
5
5
 
6
6
  Now with [Rabl](https://github.com/nesquena/rabl) support!
7
7
 
8
+ Now with [Padrino](https://github.com/padrino/padrino-framework) support as well!
9
+
10
+ For rails use [gon](https://github.com/gazay/gon).
11
+
8
12
  ## Usage
9
13
 
14
+ `my_sinatra_application.rb`
15
+
16
+ ``` ruby
17
+ register Gon::Sinatra
18
+ # and if you want to the use Rabl integration
19
+ register Gon::Sinatra::Rabl
20
+
21
+ #or, in a classy app:
22
+
23
+ class MySinatraApplication < Sinatra::Base #or Padrino::Application
24
+ register Gon::Sinatra
25
+ register Gon::Sinatra::Rabl
26
+ end
27
+ ```
28
+
10
29
  `views/application.erb`
11
30
 
12
31
  ``` erb
@@ -16,8 +16,9 @@ Gem::Specification.new do |s|
16
16
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
18
  s.require_paths = ["lib"]
19
- s.add_dependency "rabl"
20
19
  s.add_dependency "sinatra"
21
- s.add_dependency "json"
20
+
21
+ s.add_development_dependency "rabl"
22
+ s.add_development_dependency "json"
22
23
  s.add_development_dependency "rspec"
23
24
  end
@@ -1,76 +1,17 @@
1
1
  require 'sinatra'
2
+ require 'gon/sinatra/store'
2
3
  require 'gon/sinatra/helpers'
3
- require 'gon/sinatra/rabl'
4
4
 
5
5
  module Gon
6
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
7
+ def self.registered(base)
8
+ base.helpers(Gon::Sinatra::GonHelpers, Gon::Sinatra::Helpers)
9
+ end
71
10
 
72
- def jbuilder(view_path, options = {})
73
- raise NoMethodError.new("Not available for sinatra")
11
+ module Rabl
12
+ def self.registered(base)
13
+ require 'rabl'
14
+ require 'gon/sinatra/rabl'
74
15
  end
75
16
  end
76
17
  end
@@ -3,51 +3,33 @@ require 'json'
3
3
  module Gon
4
4
  module Sinatra
5
5
  module Helpers
6
- def self.included base
7
- base.send(:include, InstanceMethods)
8
- end
9
-
10
- module InstanceMethods
11
- def include_gon(options = {})
12
- if Gon::Sinatra.request_env && Gon::Sinatra.all_variables.present?
13
- data = Gon::Sinatra.all_variables
14
- namespace = options[:namespace] || 'gon'
15
- script = "<script>window." + namespace + " = {};"
16
- unless options[:camel_case]
17
- data.each do |key, val|
18
- script += namespace + "." + key.to_s + '=' + val.to_json + ";"
19
- end
20
- else
21
- data.each do |key, val|
22
- script += namespace + "." + key.to_s.camelize(:lower) + '=' + val.to_json + ";"
23
- end
6
+ def include_gon(options = {})
7
+ unless gon.all_variables.empty?
8
+ data = gon.all_variables
9
+ namespace = options[:namespace] || 'gon'
10
+ script = "<script>window." + namespace + " = {};"
11
+ unless options[:camel_case]
12
+ data.each do |key, val|
13
+ script += namespace + "." + key.to_s + '=' + val.to_json + ";"
24
14
  end
25
- script += "</script>"
26
- script
27
15
  else
28
- ""
16
+ data.each do |key, val|
17
+ script += namespace + "." + key.to_s.camelize(:lower) + '=' + val.to_json + ";"
18
+ end
29
19
  end
20
+ script += "</script>"
21
+ script
22
+ else
23
+ ""
30
24
  end
31
25
  end
32
26
  end
33
27
 
34
28
  module GonHelpers
35
- def self.included base
36
- base.send(:include, InstanceMethods)
37
- end
38
-
39
- module InstanceMethods
40
- def gon
41
- if !Gon::Sinatra.request_env || Gon::Sinatra.request != request.object_id
42
- Gon::Sinatra.request = request.object_id
43
- Gon::Sinatra.request_env = request.env
44
- end
45
- Gon::Sinatra
46
- end
29
+ def gon
30
+ env["gon"] ||= Gon::Sinatra::Store.new({})
31
+ @gon = env["gon"]
47
32
  end
48
33
  end
49
34
  end
50
- end
51
-
52
- Sinatra::Application.send :include, Gon::Sinatra::Helpers
53
- Sinatra::Base.send :include, Gon::Sinatra::GonHelpers
35
+ end
@@ -1,5 +1,4 @@
1
1
  require 'rabl'
2
- require 'json'
3
2
 
4
3
  module Gon
5
4
  module Sinatra
@@ -9,7 +8,7 @@ module Gon
9
8
  source = File.read(rabl_path)
10
9
  rabl_engine = ::Rabl::Engine.new(source, :format => 'json')
11
10
  output = rabl_engine.render(controller, {})
12
- JSON.parse(output)
11
+ ::Rabl.configuration.json_engine.decode(output)
13
12
  end
14
13
  end
15
14
  end
@@ -0,0 +1,65 @@
1
+
2
+ module Gon
3
+ module Sinatra
4
+ class Store
5
+ attr_accessor :request
6
+
7
+ def initialize(variables)
8
+ @env = variables
9
+ end
10
+
11
+ def all_variables
12
+ @env
13
+ end
14
+
15
+ def clear
16
+ @env.clear
17
+ end
18
+
19
+ def method_missing(m, *args, &block)
20
+ if ( m.to_s =~ /=$/ )
21
+ if public_methods.include? m.to_s[0..-2].to_sym
22
+ raise "You can't use Gon public methods for storing data"
23
+ end
24
+ set_variable(m.to_s.delete('='), args[0])
25
+ else
26
+ get_variable(m.to_s)
27
+ end
28
+ end
29
+
30
+ def get_variable(name)
31
+ @env[name]
32
+ end
33
+ alias :get :get_variable
34
+
35
+ def set_variable(name, value)
36
+ @env[name] = value
37
+ end
38
+ alias :set :set_variable
39
+
40
+ def rabl(view_path, options = {})
41
+ raise Exception.new("You must require rabl and register Gon::Sinatra::Rabl to use rabl") unless defined? Rabl
42
+
43
+ unless options[:instance]
44
+ raise ArgumentError.new("You should pass :instance in options: :instance => self")
45
+ end
46
+
47
+ rabl_data = Gon::Sinatra::Rabl.parse_rabl(view_path, options[:instance])
48
+
49
+ if options[:as]
50
+ set_variable(options[:as].to_s, rabl_data)
51
+ elsif rabl_data.is_a? Hash
52
+ rabl_data.each do |key, value|
53
+ set_variable(key, value)
54
+ end
55
+ else
56
+ set_variable('rabl', rabl_data)
57
+ end
58
+ end
59
+
60
+ def jbuilder(view_path, options = {})
61
+ raise NoMethodError.new("Not available for sinatra")
62
+ end
63
+ end
64
+ end
65
+ end
@@ -1,5 +1,5 @@
1
1
  module Gon
2
2
  module Sinatra
3
- VERSION = '0.0.6'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
@@ -1,53 +1,72 @@
1
1
  # gon_spec_rb
2
2
  require 'gon-sinatra'
3
3
 
4
+ class App < Sinatra::Base
5
+ register Gon::Sinatra
6
+ register Gon::Sinatra::Rabl
7
+ end
8
+
4
9
  describe Gon::Sinatra, '#all_variables' do
10
+ def app
11
+ app = App.new!
12
+ app.env = {}
13
+ app
14
+ end
5
15
 
6
16
  before(:each) do
7
- Gon::Sinatra.request_env = {}
17
+ @gon = Gon::Sinatra::Store.new({})
8
18
  end
9
19
 
10
20
  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}
21
+ @gon.a = 1
22
+ @gon.b = 2
23
+ @gon.c = @gon.a + @gon.b
24
+ @gon.c.should == 3
25
+ @gon.all_variables.should == {'a' => 1, 'b' => 2, 'c' => 3}
16
26
  end
17
27
 
18
28
  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
29
+ @gon.clear
30
+ @gon.int = 1
31
+ @gon.float = 1.1
32
+ @gon.string = 'string'
33
+ @gon.array = [ 1, 'string' ]
34
+ @gon.hash_var = { :a => 1, :b => '2'}
35
+ @gon.hash_w_array = { :a => [ 2, 3 ] }
36
+ @gon.klass = Hash
27
37
  end
28
38
 
29
39
  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
40
+ instance = app
41
+
42
+ instance.gon.int = 1
43
+ instance.methods.map(&:to_s).include?('include_gon').should == true
33
44
 
34
45
  # TODO: Make it work
35
- # base = Sinatra::Base.new
36
- # base.include_gon.should == "<script>window.gon = {};" +
37
- # "gon.int=1;" +
38
- # "</script>"
46
+ instance.include_gon.should == "<script>window.gon = {};" +
47
+ "gon.int=1;" +
48
+ "</script>"
39
49
  end
40
50
 
41
51
  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
52
+ @gon.clear
53
+ lambda { @gon.all_variables = 123 }.should raise_error
54
+ end
55
+
56
+ it 'should be threadsafe' do
57
+ instance1 = app()
58
+ instance2 = app()
59
+
60
+ instance1.gon.test = "foo"
61
+ instance2.gon.test = "bar"
62
+ instance1.gon.test.should == "foo"
44
63
  end
45
64
 
46
65
  it 'render json from rabl template' do
47
- Gon::Sinatra.clear
66
+ @gon.clear
48
67
  @objects = [1,2]
49
- Gon::Sinatra.rabl 'spec/test_data/sample.rabl', :instance => self
50
- Gon::Sinatra.objects.length.should == 2
68
+ @gon.rabl 'spec/test_data/sample.rabl', :instance => self
69
+ @gon.objects.length.should == 2
51
70
  end
52
71
 
53
72
  def request
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gon-sinatra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,8 +12,8 @@ cert_chain: []
12
12
  date: 2012-01-11 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rabl
16
- requirement: &2165738300 !ruby/object:Gem::Requirement
15
+ name: sinatra
16
+ requirement: &2164262960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,32 +21,32 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2165738300
24
+ version_requirements: *2164262960
25
25
  - !ruby/object:Gem::Dependency
26
- name: sinatra
27
- requirement: &2165737860 !ruby/object:Gem::Requirement
26
+ name: rabl
27
+ requirement: &2164262520 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
31
31
  - !ruby/object:Gem::Version
32
32
  version: '0'
33
- type: :runtime
33
+ type: :development
34
34
  prerelease: false
35
- version_requirements: *2165737860
35
+ version_requirements: *2164262520
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: json
38
- requirement: &2165737420 !ruby/object:Gem::Requirement
38
+ requirement: &2164262060 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
42
42
  - !ruby/object:Gem::Version
43
43
  version: '0'
44
- type: :runtime
44
+ type: :development
45
45
  prerelease: false
46
- version_requirements: *2165737420
46
+ version_requirements: *2164262060
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &2165736980 !ruby/object:Gem::Requirement
49
+ requirement: &2164261620 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2165736980
57
+ version_requirements: *2164261620
58
58
  description: If you need to send some data to your js files and you don't want to
59
59
  do this with long way trough views and parsing - use this force!
60
60
  email:
@@ -72,15 +72,8 @@ files:
72
72
  - lib/gon-sinatra.rb
73
73
  - lib/gon/sinatra/helpers.rb
74
74
  - lib/gon/sinatra/rabl.rb
75
+ - lib/gon/sinatra/store.rb
75
76
  - 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
77
  - spec/gon/gon_spec.rb
85
78
  - spec/test_data/sample.rabl
86
79
  homepage: https://github.com/gazay/gon-sinatra
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file