Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gazay committed Jan 10, 2012
0 parents commit 56a1e1b
Show file tree
Hide file tree
Showing 27 changed files with 504 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source "http://rubygems.org"

# Specify your gem's dependencies in gon.gemspec
gemspec
70 changes: 70 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,70 @@
PATH
remote: .
specs:
gon (2.0.4)
actionpack (>= 2.3.0)
jbuilder
json
rabl

GEM
remote: http://rubygems.org/
specs:
actionpack (3.1.1)
activemodel (= 3.1.1)
activesupport (= 3.1.1)
builder (~> 3.0.0)
erubis (~> 2.7.0)
i18n (~> 0.6)
rack (~> 1.3.2)
rack-cache (~> 1.1)
rack-mount (~> 0.8.2)
rack-test (~> 0.6.1)
sprockets (~> 2.0.2)
activemodel (3.1.1)
activesupport (= 3.1.1)
builder (~> 3.0.0)
i18n (~> 0.6)
activesupport (3.1.1)
multi_json (~> 1.0)
blankslate (2.1.2.4)
builder (3.0.0)
diff-lcs (1.1.3)
erubis (2.7.0)
hike (1.2.1)
i18n (0.6.0)
jbuilder (0.3)
activesupport (>= 3.0.0)
blankslate (>= 2.1.2.4)
json (1.6.1)
multi_json (1.0.4)
rabl (0.5.1)
activesupport (>= 2.3.14)
multi_json (~> 1.0.3)
rack (1.3.5)
rack-cache (1.1)
rack (>= 0.4)
rack-mount (0.8.3)
rack (>= 1.0.0)
rack-test (0.6.1)
rack (>= 1.0)
rspec (2.7.0)
rspec-core (~> 2.7.0)
rspec-expectations (~> 2.7.0)
rspec-mocks (~> 2.7.0)
rspec-core (2.7.1)
rspec-expectations (2.7.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.7.0)
sprockets (2.0.3)
hike (~> 1.2)
rack (~> 1.0)
tilt (!= 1.3.0, ~> 1.1)
tilt (1.3.3)

PLATFORMS
ruby

DEPENDENCIES
gon!
rspec
194 changes: 194 additions & 0 deletions README.md
@@ -0,0 +1,194 @@
# Gon-sinatra gem — get your Sinatra variables in your js


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!

Now with [Rabl](https://github.com/nesquena/rabl) support!

## Usage

`views/application.erb`

``` erb
<head>
<title>some title</title>
<%= include_gon %>
<!-- include your action js code -->
...
```

To camelize your variables in js you can use:

``` erb
<head>
<title>some title</title>
<%= include_gon(:camel_case => true) %>
<!-- include your action js code with camelized variables -->
...
```

You can change the namespace of the variables:

``` erb
<head>
<title>some title</title>
<%= include_gon(:namespace => 'serverExports') %>
<!-- include your action js code with 'serverExports' namespace -->
...
```

You put something like this in the Sinatra action:

``` ruby
@your_int = 123
@your_array = [1,2]
@your_hash = {'a' => 1, 'b' => 2}
gon.your_int = @your_int
gon.your_other_int = 345 + gon.your_int
gon.your_array = @your_array
gon.your_array << gon.your_int
gon.your_hash = @your_hash

gon.all_variables # > {:your_int => 123, :your_other_int => 468, :your_array => [1, 2, 123], :your_hash => {'a' => 1, 'b' => 2}}
gon.your_array # > [1, 2, 123]

gon.clear # gon.all_variables now is {}
```

Access the varaibles from your JavaScript file:

``` js
alert(gon.your_int)
alert(gon.your_other_int)
alert(gon.your_array)
alert(gon.your_hash)
```

With camelize:

``` js
alert(gon.yourInt)
alert(gon.yourOtherInt)
alert(gon.yourArray)
alert(gon.yourHash)
```

With custom namespace and camelize:

``` js
alert(customNamespace.yourInt)
alert(customNamespace.yourOtherInt)
alert(customNamespace.yourArray)
alert(customNamespace.yourHash)
```

## Usage with Rabl

Now you can write your variables assign logic to templates with [Rabl](https://github.com/nesquena/rabl).
The way of writing Rabl templates is very clearly described in their repo.

Profit of using Rabl with gon:

1. You can clean your controllers now!
2. Work with database objects and collections clearly and easyly
3. All power of Rabl
4. You can still be lazy and don't use common way to transfer data in js
5. And so on

For using gon with Rabl you need to create new Rabl template and map gon
to it.
For example you have model Post with attributes :title and :body.
You want to get all your posts in your js as an Array.
That's what you need to do:

1. Create Rabl template. I prefer creating special directory for
templates which are not view templates.

`goners/posts/index.rabl`

``` rabl
collection @posts => 'posts'
attributes :id, :title, :body
```

2. All you need to do after that is only to map this template to gon.

``` ruby
get '/' do
# some logic
@posts = [some_objects] # Rabl works with instance variables of controller

gon.rabl 'goners/posts/index.rabl'
# some logic
end
```

Thats it! Now you will get in your js gon.posts variable which is Array of
post objects with attributes :id, :title and :body.

In javascript file for view of this action write call to your variable:

``` js
alert(gon.posts)
alert(gon.posts[0])
alert(gon.posts[0].post.body)
```

P.s. If you didn't put include_gon tag in your html head area - it
wouldn't work. You can read about this in common usage above.

### Some tips of usage Rabl with gon:

If you don't use alias in Rabl template:

``` rabl
collection @posts
....
```

instead of using that:

``` rabl
collection @posts => 'alias'
....
```

Rabl will return you an array and gon by default will put it to variable
gon.rabl

Two ways how you can change it - using aliases or you can add alias to
gon mapping method:

``` ruby
# your logic stuff here

gon.rabl 'path/to/rabl/file', :as => 'alias'
```

## Installation

Manually install gon-sinatra gem: `$ gem install gon-sinatra`

Add requirement to your app file

``` ruby
require 'gon-sinatra'
```

## Contributors

* @gazay

Special thanks to @brainopia, @kossnocorp and @ai.

## License

The MIT License

Copyright (c) 2011 gazay

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.
4 changes: 4 additions & 0 deletions Rakefile
@@ -0,0 +1,4 @@
require 'rubygems'
require 'rake'
require 'bundler'
Bundler::GemHelper.install_tasks
Binary file added cache/gon-0.2.2.gem
Binary file not shown.
Binary file added gon-2.0.2.gem
Binary file not shown.
Binary file added gon-2.0.3.gem
Binary file not shown.
Binary file added gon-2.0.4.gem
Binary file not shown.
Binary file added gon-2.0.5.gem
Binary file not shown.
23 changes: 23 additions & 0 deletions gon-sinatra.gemspec
@@ -0,0 +1,23 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "gon/sinatra/version"

Gem::Specification.new do |s|
s.name = "gon-sinatra"
s.version = Gon::Sinatra::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ['gazay']
s.email = ['alex.gaziev@gmail.com']
s.homepage = "https://github.com/gazay/gon-sinatra"
s.summary = %q{Get your Sinatra variables in your JS}
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!}

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
s.add_dependency "rabl"
s.add_dependency "sinatra"
s.add_dependency "json"
s.add_development_dependency "rspec"
end
77 changes: 77 additions & 0 deletions lib/gon-sinatra.rb
@@ -0,0 +1,77 @@
require 'sinatra'
require 'gon/sinatra/helpers'
require 'gon/sinatra/rabl'

module Gon
module Sinatra
class << self
def all_variables
@request_env[:gon]
end

def clear
@request_env[:gon] = {}
end

def request_env=(environment)
@request_env = environment
@request_env[:gon] ||= {}
end

def request_env
if defined?(@request_env)
return @request_env
end
end

def request
@request_id if defined? @request_id
end

def request=(request_id)
@request_id = request_id
end

def method_missing(m, *args, &block)
if ( m.to_s =~ /=$/ )
if public_methods.include? m.to_s[0..-2].to_sym
raise "You can't use Gon public methods for storing data"
end
set_variable(m.to_s.delete('='), args[0])
else
get_variable(m.to_s)
end
end

def get_variable(name)
@request_env[:gon][name]
end

def set_variable(name, value)
@request_env[:gon][name] = value
end

def rabl(view_path, options = {})
unless options[:instance]
raise ArgumentError.new("You should pass :instance in options: :instance => self")
end

rabl_data = Gon::Sinatra::Rabl.parse_rabl(view_path, options[:instance])

if options[:as]
set_variable(options[:as].to_s, rabl_data)
elsif rabl_data.is_a? Hash
rabl_data.each do |key, value|
set_variable(key, value)
end
else
set_variable('rabl', rabl_data)
end
end

def jbuilder(view_path, options = {})
raise NoMethodError.new("Not available for sinatra")
end
end
end
end

0 comments on commit 56a1e1b

Please sign in to comment.