File tree Expand file tree Collapse file tree 3 files changed +33
-10
lines changed
Expand file tree Collapse file tree 3 files changed +33
-10
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,6 @@ Gem::Specification.new do |s|
1818 s . test_files = `git ls-files -- {test,spec,features}/*` . split ( "\n " )
1919 s . executables = `git ls-files -- bin/*` . split ( "\n " ) . map { |f | File . basename ( f ) }
2020 s . require_paths = [ "lib" ]
21- s . add_dependency "actionpack" , '~> 2.3.0'
21+ s . add_dependency "actionpack" , '>= 2.3.0'
2222 s . add_development_dependency "rspec"
2323end
Original file line number Diff line number Diff line change 11require 'action_view'
22require 'gon/helpers'
3- require 'ostruct'
43
5- Gon = OpenStruct . new
4+ module Gon
5+ def self . all_variables
6+ request . env [ :gon ]
7+ end
8+ def self . clear
9+ request . env [ :gon ] = { }
10+ end
11+
12+ def self . method_missing ( m , *args , &block )
13+ request . env [ :gon ] ||= { }
14+
15+ if ( m . to_s =~ /=/ )
16+ set_variable ( m . to_s . delete ( '=' ) , args [ 0 ] )
17+ else
18+ get_variable ( m . to_s )
19+ end
20+ end
621
7- class << Gon
8- def all_variables
9- @table
22+ def self . get_variable ( name )
23+ request . env [ :gon ] [ name ]
1024 end
11- def clear
12- @table = { }
25+
26+ def self . set_variable ( name , value )
27+ request . env [ :gon ] [ name ] = value
1328 end
1429end
Original file line number Diff line number Diff line change 22require 'gon'
33
44describe Gon , '#all_variables' do
5+ before ( :each ) do
6+ Gon . stub ( :request ) . and_return ( request )
7+ end
8+
59 it 'returns all variables in hash' do
610 Gon . a = 1
711 Gon . b = 2
812 Gon . c = Gon . a + Gon . b
913 Gon . c . should == 3
10- Gon . all_variables . should == { :a => 1 , :b => 2 , :c => 3 }
14+ Gon . all_variables . should == { 'a' => 1 , 'b' => 2 , 'c' => 3 }
1115 end
1216
1317 it 'supports all data types' do
1822 Gon . array = [ 1 , 'string' ]
1923 Gon . hash = { :a => 1 , :b => '2' }
2024 Gon . hash_w_array = { :a => [ 2 , 3 ] }
21- Gon . klass = OpenStruct . new
25+ Gon . klass = Hash
2226 end
2327
2428 it 'output as js correct' do
3034 "Gon.int=1;" +
3135 "</script>"
3236 end
37+
38+ def request
39+ @request ||= double 'request' , :env => { }
40+ end
3341end
You can’t perform that action at this time.
0 commit comments