-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 242f428
Showing
15 changed files
with
339 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
*.gem | ||
*.rbc | ||
.bundle | ||
.config | ||
.yardoc | ||
Gemfile.lock | ||
InstalledFiles | ||
_yardoc | ||
coverage | ||
doc/ | ||
lib/bundler/man | ||
pkg | ||
rdoc | ||
spec/reports | ||
test/tmp | ||
test/version_tmp | ||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in client_variable.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2013 MQuy | ||
|
||
MIT License | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# ClientVariable | ||
|
||
TODO: Write a gem description | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
gem 'client_variable' | ||
|
||
And then execute: | ||
|
||
$ bundle | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install client_variable | ||
|
||
## Usage | ||
|
||
TODO: Write usage instructions here | ||
|
||
## Contributing | ||
|
||
1. Fork it | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create new Pull Request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require "bundler/gem_tasks" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'client_variable/version' | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "client_variable" | ||
spec.version = ClientVariable::VERSION | ||
spec.authors = ["MQuy"] | ||
spec.email = ["sugiacupit@gmail.com"] | ||
spec.description = %q{export variable from rails} | ||
spec.summary = %q{variable rails in client} | ||
spec.homepage = "" | ||
spec.license = "MIT" | ||
spec.rubyforge_project = "client_variable" | ||
|
||
spec.files = `git ls-files`.split($/) | ||
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } | ||
spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_development_dependency "bundler", "~> 1.3" | ||
spec.add_development_dependency "rake" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require "client_variable/version" | ||
|
||
module Client | ||
class Variable | ||
class << self | ||
def global | ||
Global | ||
end | ||
|
||
def get_variable(name) | ||
Request.values[name] | ||
end | ||
|
||
def set_variable(name, value) | ||
Request.values[name] = value | ||
end | ||
|
||
def values | ||
Request.values | ||
end | ||
|
||
def clear | ||
Request.clear | ||
end | ||
|
||
def method_missing(method, *args, &block) | ||
if ( method.to_s =~ /=$/ ) | ||
if public_methods? method | ||
raise "You can't use Gon public methods for storing data" | ||
end | ||
set_variable(method.to_s.delete('='), args[0]) | ||
else | ||
get_variable(method.to_s) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def self.generate | ||
data = YAML.load(ERB.new(File.new("#{Rails.root}/config/client_variable.yml").read).result)[Rails.env] | ||
data.merge([Global.values, Request.values].inject(:merge)).to_json | ||
end | ||
|
||
class Engine < ::Rails::Engine; end | ||
|
||
require 'client_variable/request' | ||
require 'client_variable/global' | ||
require 'client_variable/helper/view' | ||
require 'client_variable/helper/controller' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Client | ||
class Global < Variable | ||
class << self | ||
def values | ||
@datum ||= {} | ||
end | ||
|
||
def clear | ||
@datum = {} | ||
end | ||
|
||
def get_variable(name) | ||
values[name] | ||
end | ||
|
||
def set_variable(name, value) | ||
values[name] = value | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Client | ||
module Helpers | ||
module Instance | ||
def self.included base | ||
base.send(:include, InstanceMethods) | ||
end | ||
|
||
module InstanceMethods | ||
def client | ||
if new_request? | ||
Request.id = request.object_id | ||
Request.env = request.env | ||
end | ||
Variable | ||
end | ||
|
||
private | ||
def new_request? | ||
Request.env.blank? || Request.id != request.object_id | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
ActionController::Base.send :include, Client::Helpers::Instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module Client | ||
module Helpers | ||
module View | ||
extend ActionView::Helpers::JavaScriptHelper | ||
extend ActionView::Helpers::TagHelper | ||
|
||
def self.included base | ||
base.send(:include, InstanceMethods) | ||
end | ||
|
||
module InstanceMethods | ||
def include_variable | ||
render_data | ||
end | ||
|
||
private | ||
def render_data | ||
script = "window.rails = {};" | ||
script << formatted_data | ||
script = javascript_tag(script) | ||
script.html_safe | ||
end | ||
|
||
def formatted_data | ||
script = '' | ||
values.each do |key, val| | ||
js_key = key.to_s.camelize(:lower) | ||
script << "rails.#{js_key}=#{normalize(val).to_json};" | ||
end | ||
script | ||
end | ||
|
||
def normalize(value, depth=0) | ||
return value if depth > 1000 | ||
return value unless value.is_a? Hash | ||
|
||
Hash[value.map { |k, v| | ||
[ k.to_s.camelize(:lower), normalize(v, depth + 1) ] | ||
}] | ||
end | ||
|
||
def values | ||
[Global.values, Request.values].inject(:merge) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
ActionView::Base.send :include, Client::Helpers::View |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Client | ||
class Request | ||
class << self | ||
def env | ||
@request_env if defined? @request_env | ||
end | ||
|
||
def env=(environment) | ||
@request_env = environment | ||
@request_env['client'] ||= {} | ||
end | ||
|
||
def id | ||
@request_id if defined? @request_id | ||
end | ||
|
||
def id=(request_id) | ||
@request_id = request_id | ||
end | ||
|
||
def values | ||
env['client'] if env | ||
end | ||
|
||
def clear | ||
env && (env['client'] = {}) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module ClientVariable | ||
VERSION = "0.0.1" | ||
end |
13 changes: 13 additions & 0 deletions
13
lib/generators/client_variable/install/install_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module ClientVariable | ||
module Generators | ||
class InstallGenerator < Rails::Generators::Base | ||
source_root File.expand_path("../templates", __FILE__) | ||
|
||
desc "Generates a variable config" | ||
|
||
def create_config_file | ||
template "config.yml", "config/client_variable.yml" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
development: | ||
x: 1 | ||
|
||
production: | ||
x:1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
rails = (function(exports, global) { | ||
var datum = deepMerge(exports, <%= Client.generate%>); | ||
var defaultSeparator = '.'; | ||
|
||
function lookup(query, track) { | ||
var parts = separateQuery(query), | ||
part = null, | ||
messages = datum; | ||
while(messages) { | ||
part = parts.shift(); | ||
if (parts.length == 0) break; | ||
if (track && !messages[parts]) messages[part] = {}; | ||
messages = messages[part]; | ||
} | ||
return messages; | ||
} | ||
|
||
function deepMerge(target, source) { | ||
for(var key in source) { | ||
if (source.hasOwnProperty(key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
return target; | ||
} | ||
|
||
function separateQuery(query, n) { | ||
var parts = query.split(defaultSeparator); | ||
if (n < 0) n += parts.length | ||
return n ? parts[n] : parts; | ||
} | ||
|
||
return { | ||
set: function(name, value) { | ||
var parent = lookup(name, true); | ||
if (parent) parent[separateQuery(name, -1)] = value; | ||
}, | ||
get: function(name) { | ||
var parent = lookup(name); | ||
return (parent = lookup(name)) ? parent[separateQuery(name, -1)] : parent; | ||
}, | ||
values: datum | ||
} | ||
}(rails || {}, this)); |