dusty_rails_renderer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 96a5b2c4d7d645980609114ba0f3a028809f236e
4
+ data.tar.gz: efc59e3e7b2fdce9413de47e3790c619483e84ba
5
+ SHA512:
6
+ metadata.gz: fd42c98a9ff1442ff9eb8762c5f7de09be0c086a1e1d5cc3af945da38fb97e4e0ffa8fbcd25711fe6945988e846912540a9fa38d6fcaf7a061c5ee37d90b0da7
7
+ data.tar.gz: 0267b8ace8aa716cd1d77592857947609eba1be046f9410230b188a2fefc79c7d9c3c56545e2ccbef0651469fbf58e8c1c3834c77a81621f2c237819c4f1d59c
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dusty_rails_renderer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # Dusty Rails Renderer
2
+
3
+ Dusty Rails Renderer was written to make it easy to render dust.js templates server side. Please refer to <a href="www.dustjs.com">www.dustjs.com</a> for documentation on using Dust.js. This gem requires Dust.js by LinkedIn
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'dusty_rails_renderer'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dusty_rails_renderer
20
+
21
+ ## Configuration
22
+
23
+ Run:
24
+
25
+ $ rails generate dusty_rails_renderer:install
26
+
27
+ This will create an initializer for dusty_rails_renderer at 'config/initializers/dust_initializer.rb'.
28
+
29
+ ## Defaults
30
+
31
+ dust_initializer.rb
32
+
33
+ $ DustyRailsRenderer.configure do |config|
34
+ $ config.dust_config_path = 'config/dust_initializer.yml'
35
+ $ config.dust_js_library_path = 'app/assets/javascripts/libraries/dust/dust-full.js'
36
+ $ config.dust_template_base_path = 'app/assets/javascripts/dust/'
37
+ $ config.production = false
38
+ $ end
39
+
40
+ $ DustyRailsRenderer.initialize
41
+
42
+
43
+ By configuring the initializer you can update the location of your Dust templates and Dust.js library. For this gem to work property you must use the full Dust library that includes the compiler. Lastly, setting config.production = false will enable loading from disk and precompiling during request time. This
44
+ feature is great for debugging and creating new templates but not for production. Setting config.production = true will precompile the templates once
45
+ during the server initialization process and load the templates into memory.
46
+
47
+ ## Usage
48
+
49
+ Add reference to all your Dust.js templates in config.dust/initializer.yml. Templates names must be unique. file_path is the reference to your Dust.js templates
50
+ inside the dust_template_base_path. For example using the default dusty_rails_renderer configuration 'common/header.dust' would be in 'app/assets/javascripts/dust/common/header.dust'
51
+
52
+ dust_initializer.yml
53
+
54
+ header:
55
+ file_path: common/header.dust
56
+ name: common_header
57
+ home:
58
+ file_path: home/home.dust
59
+ name: home
60
+
61
+ Lastly, to render Dust.js templates server side in your controllers, call
62
+
63
+ $ DustyRailsRenderer.render('{TEMPLATE_NAME_HERE}',"{JSON_HERE}").
64
+
65
+ Example
66
+
67
+ $ render :text => DustyRailsRenderer.render('home',"{name: 'Louis'}") , :layout => "application"
68
+
69
+ Client-Side
70
+
71
+ To use your precompiled templates on the client-side you can pass the precompiled templates into javascript.
72
+
73
+ $ <script type="text/javascript">
74
+ $ var precompiled_templates = <%= raw DustyRailsRenderer.templates %>;
75
+ $ </script>
76
+
77
+ Include the dust.js(full library by LinkedIn) then register and render templates
78
+
79
+ $ <script type="text/javascript">
80
+ $ var Utils = {
81
+ $ load_templates: function() {
82
+ $ for(var key in precompile_templates) {
83
+ $ dust.loadSource(precompile_templates[key], key);
84
+ $ }
85
+ $ }
86
+ $ , render_template: function(template_name, json) {
87
+ $ return (function() {
88
+ $ var result = '';
89
+ $ dust.render(template_name, json, function(err,out) {
90
+ $ result = out;
91
+ $ });
92
+ $ return result;
93
+ $ })();
94
+ $ }
95
+ $ }
96
+ $
97
+ $ Utils.load_templates(precompiled_templates);
98
+ $ Utils.render_template('{TEMPLATE_NAME_HERE}',"{JSON_HERE}");
99
+ $ </script>
100
+
101
+ ## Who wrote Dusty Rails Renderer?
102
+
103
+ My name is <a href="http://louisellis.com/">Louis Ellis</a> and I wrote Dust Rails Renderer in October 2015.
104
+
105
+ ## Contributing
106
+
107
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ellisapps/dusty_rails_renderer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the <a href="http://contributor-covenant.org">Contributor Covenant</a> code of conduct.
108
+
109
+
110
+ ## License
111
+
112
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
113
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dusty_rails_renderer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dusty_rails_renderer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dusty_rails_renderer"
8
+ spec.version = DustyRailsRenderer::VERSION
9
+ spec.authors = "Louis Ellis"
10
+ spec.email = "Louis.f.ellis@gmail.com"
11
+
12
+ spec.summary = "Render Dust server side"
13
+ spec.description = "Render Dust server side"
14
+ spec.homepage = "http://louisellis.com"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ end
@@ -0,0 +1,3 @@
1
+ module DustyRailsRenderer
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,72 @@
1
+ require "singleton"
2
+ require "dusty_rails_renderer/version"
3
+
4
+ module DustyRailsRenderer
5
+ class << self
6
+ include Singleton
7
+ attr_writer :configuration
8
+
9
+ #Initialize, load Dust.js library, and precompile Dust.js templates
10
+ def initialize
11
+ @dust_config = YAML.load_file(self.configuration.dust_config_path)
12
+ dust_library = File.read(self.configuration.dust_js_library_path)
13
+ @precompiled_templates = Hash.new
14
+ @context = V8::Context.new
15
+ @context.eval(dust_library, 'dustjs')
16
+
17
+ read_dust_files
18
+ end
19
+
20
+ #Return precompiled templates in JSON format (Client-side)
21
+ def templates
22
+ @precompiled_templates.to_json
23
+ end
24
+
25
+ #Render template registered Dust.js template
26
+ def render(template_name, json)
27
+ if self.configuration.production
28
+ @context.eval("(function() { var result; dust.render('#{template_name}', #{json}, function(err, out) { result = out; }); return result; })()")
29
+ else
30
+ read_dust_files
31
+ @context.eval("(function() { var result; dust.render('#{template_name}', #{json}, function(err, out) { result = out; }); return result; })()")
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ #Read in and register Dust.js templates
38
+ def read_dust_files
39
+ @dust_config.each do |template, info|
40
+ file_url = self.configuration.dust_template_base_path + info["file_path"]
41
+ template_name = info["name"]
42
+ contents = File.read(file_url).gsub("\n","").gsub("\"","'").gsub("\t","")
43
+ template = @context.eval("dust.compile(\"#{contents}\",'#{template_name}')")
44
+ @context.eval("dust.loadSource(dust.compile(\"#{contents}\",'#{template_name}'))")
45
+ @precompiled_templates[template_name] = template
46
+ end
47
+ end
48
+ end
49
+
50
+ def self.configure
51
+ yield(configuration)
52
+ end
53
+
54
+ def self.configuration
55
+ @configuration ||= Configuration.new
56
+ end
57
+
58
+ class Configuration
59
+ attr_accessor :dust_config_path
60
+ attr_accessor :dust_js_library_path
61
+ attr_accessor :production
62
+ attr_accessor :dust_template_base_path
63
+
64
+ def initialize
65
+ @dust_config_path = "config/dust_initializer.yml"
66
+ @dust_js_library_path = "app/assets/javascripts/libraries/dust/dust-full.js"
67
+ @dust_template_base_path = "app/assets/javascripts/dust/"
68
+ @production = false
69
+ end
70
+ end
71
+
72
+ end
@@ -0,0 +1,15 @@
1
+ module DustyRailsRenderer
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ def copy_initializer
5
+ File.open(File.join('config/initializers', 'dust_initializer.rb'), 'w') do |f|
6
+ f.puts "DustyRailsRenderer.configure do |config|"
7
+ f.puts "\n"
8
+ f.puts "end"
9
+ f.puts "\n"
10
+ f.puts "DustyRailsRenderer.initialize"
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dusty_rails_renderer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Louis Ellis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Render Dust server side
42
+ email: Louis.f.ellis@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".gitignore"
48
+ - ".travis.yml"
49
+ - CODE_OF_CONDUCT.md
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - dusty_rails_renderer.gemspec
57
+ - lib/dusty_rails_renderer.rb
58
+ - lib/dusty_rails_renderer/version.rb
59
+ - lib/generators/dusty_rails_renderer/install_generator.rb
60
+ homepage: http://louisellis.com
61
+ licenses:
62
+ - MIT
63
+ metadata:
64
+ allowed_push_host: https://rubygems.org
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.2.0
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Render Dust server side
85
+ test_files: []