Permalink
Browse files

Gon push syntax, use any object

  • Loading branch information...
1 parent 3079ce0 commit dfebdb933ab81ac265acd698e5d5ae66a3fdce7e Arsen Gasparyan committed Mar 20, 2013
Showing with 17 additions and 6 deletions.
  1. +4 −2 README.md
  2. +4 −2 lib/gon.rb
  3. +9 −2 spec/gon/basic_spec.rb
View
6 README.md
@@ -44,9 +44,11 @@ gem line to your Gemfile and do the following:
# or new syntax
gon.push({
- user_id: 1,
- user_role: 'admin'
+ :user_id => 1,
+ :user_role => "admin"
})
+
+ gon.push(any_object) # any_object with respond_to? :each_pair
```
2. In your js you get this by
View
6 lib/gon.rb
@@ -44,8 +44,10 @@ def set_variable(name, value)
Request.gon[name] = value
end
- def push(data)
- data.each do |name, value|
+ def push(data = {})
+ raise "Object must have each_pair method" unless data.respond_to? :each_pair
+
+ data.each_pair do |name, value|
set_variable(name, value)
end
end
View
11 spec/gon/basic_spec.rb
@@ -50,8 +50,15 @@
it 'can be support new push syntax' do
Gon.clear
- Gon.push(int: 1, string: 'string')
- Gon.all_variables.should == { int: 1, string: 'string' }
+ Gon.push({ :int => 1, :string => 'string' })
+ Gon.all_variables.should == { :int => 1, :string => 'string' }
+ end
+
+ it 'push with wrong object' do
+ expect {
+ Gon.clear
+ Gon.push(String.new("string object"))
+ }.to raise_error("Object must have each_pair method")
end
end

0 comments on commit dfebdb9

Please sign in to comment.