js-flasher 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .DS_Store
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (C) 2012 Joseph Smith
2
+
3
+ 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:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ 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.
data/README.md ADDED
@@ -0,0 +1,171 @@
1
+ js-flasher
2
+ ==========
3
+
4
+ A simple gem that exposes unprocessed JS templates.
5
+
6
+ `js-flasher`, as the name loosely suggests, exposes something. At its core, it will take files, and build Javascript objects keyed by filename, where the contents of the file are the values. It can run happily alongside, but is not dependent on, Sprockets.
7
+
8
+ ###### Why would I use this over other solutions like Sprockets JST templates?
9
+
10
+ Since `js-flasher` passes around an object packed with your markup, rather than template functions, you are free to pass many templates, without fear that you generating large objects of precompiled template functions. This approach has bonuses as well as drawbacks.
11
+
12
+ Bonuses
13
+
14
+ - less data is being passed around than say, Sprockets' templates, therefore actual JS payload can be much smaller
15
+ - you are given full flexiblity to handle rendering your templates however you see fit
16
+
17
+ Drawbacks
18
+
19
+ - more client-side processing as you compile/render your templates at runtime or JS app bootstrap.
20
+
21
+ If you have read the above and feel like `js-flasher` is a good fit for your project, continue on.
22
+
23
+
24
+ ## Installation:
25
+
26
+ If you're using Bundler, this will get you up and running
27
+
28
+ gem 'js-flasher', :git => 'git://github.com/technicolorenvy/js-flasher.git'
29
+
30
+
31
+ or, for non-Bundler-users
32
+
33
+ git clone git://github.com/technicolorenvy/js-flasher.git
34
+ cd js-flasher
35
+ gem build js-flasher.gemspec
36
+ gem install js-flasher-VERSION.gem
37
+
38
+ NOTE: The only dependency this gem has is the `json` gem, which is likely already installed for most users
39
+
40
+ ### Default Config
41
+
42
+ `js-flasher` ships with some (hopefully) smart defaults. These are largely geared towards a vanilla Rails 3.1 (or higher) environment, but are easily be overridden.
43
+
44
+ ##### Template Sources
45
+
46
+ Hash of template sources arranged as 'Target Object' => 'Source Directory'
47
+
48
+ :template_sources = {:Templates => 'app/assets/templates'}
49
+
50
+
51
+ ##### Supported Extensions
52
+
53
+ Array of file endings
54
+
55
+ :supported_extensions = ['.tpl.html']
56
+
57
+ ##### Target Directory
58
+
59
+ String representation of your target directory
60
+
61
+ :target_directory = 'app/assets/javascripts/templates'
62
+
63
+ ##### JS Namespace
64
+
65
+ Parent Javascript object generated for namespacing
66
+
67
+ :js_namespace = 'JSF'
68
+
69
+ ##### Extensions in Keys
70
+
71
+ Shoud the resulting JS object include file extensions in it's keys?
72
+
73
+ :extensions_in_keys = false
74
+
75
+ ## Usage - Rails:
76
+
77
+ #### Config
78
+
79
+ The recommended process for modifying `js-flasher` configuration in Rails is
80
+
81
+ - Add an initializer to `conf/initializers/` named `js-flasher.rb`
82
+ - Apply configuration changes via a do block
83
+
84
+ JsFlasher::configure do |c|
85
+ c.template_sources = {:Shared => 'app/assets/templates', :Web => 'app/assets/app/web/'}
86
+ c.supported_extensions = ['.my.extension1', '.extension2', '.etc']
87
+ end
88
+
89
+
90
+ #### Create files
91
+
92
+ You can create files for your JS template objects by running `rake js_flasher:build_files`. This will create a file for each template source that you have passed in.
93
+
94
+ For example, given the above configure block, we would create two files in our `app/assets/javascripts/templates` directory (our default target directory). `Shared.js.erb` and `Web.js.erb`. Each file will have only its set of templates.
95
+
96
+ The contents of the generated `Shared.js.erb` would look something like the following.
97
+
98
+ var JSF = JSF || {}; JSF.Shared = <%= JsFlasher.get_templates([:Shared]) %>;
99
+
100
+ In this case our namespace `JSF` is also generated from our default config and may be overridden
101
+
102
+
103
+ #### Access your templates
104
+
105
+ Now in your Javascript files you are free to use whatever templating system you want. You may access your template like so.
106
+
107
+ JSF.Shared['path/to/my/template']
108
+
109
+ Paths are relative to your template set. In other words, in the above example your template would actually live in `app/assets/templates/path/to/my/template`.
110
+
111
+ ## Usage - Non Rails
112
+ (or for those who prefer greater flexibility):
113
+
114
+ `js-flasher` comes with an instantiatable class called `JsFlasherInitable`.
115
+
116
+ #### Create + Config
117
+
118
+ - Instantiate your object, passing in your config
119
+
120
+ js_flasher = JsFlasherInitable.new({
121
+ :template_sources => {
122
+ :Shared => 'app/assets/templates',
123
+ :Web => 'app/assets/app/web/'
124
+ }
125
+ })
126
+
127
+
128
+ #### Expose templates
129
+
130
+ It's recommended that you do this through a `.js.erb` file. This way, in production, you can take advantage of cacheing, assuming you are precompiling your assets. Something like `templates.js.erb` seems nice and self-documenting.
131
+
132
+ Assuming you want All of your templates, you could create a file whose contents look like.
133
+
134
+ # contents of templates.js.erb
135
+ <% js_flasher = JsFlasherInitable.new({
136
+ :template_sources => {
137
+ :Shared => 'app/assets/templates',
138
+ :Web => 'app/assets/app/web/'
139
+ }
140
+ })
141
+ %>
142
+ window.Shared = <%= js_flasher.get_templates %>;
143
+
144
+ If you have multiple template sets and you prefer breaking the generated template strings into multiple files you could split them like so
145
+
146
+ shared.js.erb
147
+
148
+ # contents of shared.js.erb
149
+ <% shared_emplates = JsFlasherInitable.new({
150
+ :template_sources => {
151
+ :Shared => 'app/assets/templates',
152
+ }
153
+ })
154
+ %>
155
+ window.Shared = <%= shared_templates.get_templates %>;
156
+
157
+ web.js.erb
158
+
159
+ # contents of web.js.erb
160
+ <% web_templates = JsFlasherInitable.new({
161
+ :template_sources => {
162
+ :Web => 'app/assets/app/web/'
163
+ }
164
+ })
165
+ %>
166
+ window.webTemplates = <%= web_templates.get_templates %>;
167
+
168
+ Using the above described methods, you can skip using `js-flasher`s `:target_directory` and `:js_namespace` options as they are mostly used for file generation in the included Rails specific Rake task.
169
+
170
+
171
+
data/Rakefile ADDED
File without changes
@@ -0,0 +1,18 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "js_flasher/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "js-flasher"
6
+ s.version = JsFlasher::VERSION
7
+ s.authors = ["Jos Smith"]
8
+ s.email = ["technicolorenvy@gmail.com"]
9
+ s.homepage = "https://github.com/technicolorenvy/js-flasher"
10
+ s.summary = "A simple gem that exposes unprocessed JS templates"
11
+ s.description = "A simple gem that exposes unprocessed JS templates"
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.require_paths = ["lib"]
15
+
16
+ s.add_dependency 'json'
17
+
18
+ end
data/lib/js-flasher.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'js_flasher/version'
2
+ require 'js_flasher/configuration'
3
+ require 'js_flasher/get_templates'
4
+
5
+ module JsFlasher
6
+ extend Configuration
7
+ extend GetTemplates
8
+ require 'js_flasher/railtie' if defined?(Rails)
9
+ end
10
+
11
+ class JsFlasherInitable
12
+ # include JsFlasher
13
+ include JsFlasher::Configuration
14
+ include JsFlasher::GetTemplates
15
+
16
+ def initialize(config = {})
17
+ reset()
18
+
19
+ config.each do |opt, val|
20
+ self.send("#{opt}=",val)
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,46 @@
1
+ module JsFlasher
2
+
3
+ module Configuration
4
+
5
+ VALID_OPTIONS = [
6
+ :template_sources,
7
+ :supported_extensions,
8
+ :target_directory,
9
+ :js_namespace,
10
+ :extensions_in_keys
11
+ ]
12
+
13
+ VALID_OPTIONS.each { |opt| attr_accessor opt }
14
+
15
+ DEFAULT_TEMPLATE_SOURCES = {:Templates => 'app/assets/templates'}
16
+
17
+ DEFAULT_SUPPORTED_EXENSIONS = ['.tpl.html']
18
+
19
+ DEFAULT_TARGET_DIRECTORY = 'app/assets/javascripts/templates'
20
+
21
+ DEFAULT_JS_NAMESPACE = 'JSF'
22
+
23
+ DEFAULT_EXTENSIONS_IN_KEYS = false
24
+
25
+ # When this module is extended, set all configuration options to their default values
26
+ def self.extended(base)
27
+ base.reset
28
+ end
29
+
30
+ # Convenience method to allow configuration options to be set in a block
31
+ def configure
32
+ yield self
33
+ end
34
+
35
+ # assign our defaults
36
+ def reset
37
+ self.template_sources = DEFAULT_TEMPLATE_SOURCES
38
+ self.supported_extensions = DEFAULT_SUPPORTED_EXENSIONS
39
+ self.target_directory = DEFAULT_TARGET_DIRECTORY
40
+ self.js_namespace = DEFAULT_JS_NAMESPACE
41
+ self.extensions_in_keys = DEFAULT_EXTENSIONS_IN_KEYS
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,47 @@
1
+ require 'json'
2
+
3
+ module JsFlasher
4
+ module GetTemplates
5
+
6
+ def get_templates_ob(subset = nil)
7
+
8
+ subset ||= self.template_sources.keys
9
+ templates_ob = {}
10
+
11
+ self.template_sources.each do |tpl_name, tpl_dir|
12
+
13
+ if subset && subset.include?(tpl_name)
14
+ templates_ob[tpl_name] = {}
15
+
16
+ Dir.chdir(tpl_dir) do
17
+ outs = Dir.glob("**/*")
18
+
19
+ outs.each do |filename|
20
+ out_arr = filename.split('/').last.split('.')
21
+ out_arr.shift
22
+ extension = ".#{out_arr.join('.')}"
23
+ filename_key = (self.extensions_in_keys) ? filename : filename.gsub(extension, '')
24
+
25
+ if self.supported_extensions.include? extension
26
+ filecontents = File.open(filename, "rb") { |f| f.read }
27
+ templates_ob[tpl_name][filename_key] = filecontents.gsub(/([\n\t\r])/, '')
28
+ end
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+ if subset.length === 1
36
+ templates_ob = templates_ob[subset[0].to_sym]
37
+ end
38
+
39
+ return templates_ob
40
+ end
41
+
42
+ def get_templates(subset = nil)
43
+ templates = get_templates_ob(subset).to_json
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,8 @@
1
+ require 'rails'
2
+ module JsFlasher
3
+ class Railtie < Rails::Railtie
4
+ rake_tasks do
5
+ load 'tasks/js-flasher.rake'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ module JsFlasher
2
+ # shit's in beta, son
3
+ VERSION = "0.2.2"
4
+ end
@@ -0,0 +1,21 @@
1
+ namespace :js_flasher do
2
+ desc ""
3
+ task :build_files => :environment do
4
+ templates_object = JsFlasher::get_templates_ob
5
+ target_directory = JsFlasher::target_directory
6
+ js_namespace = JsFlasher::js_namespace
7
+
8
+ templates_object.each do |ob_name, templates|
9
+ out = "var #{js_namespace} = #{js_namespace} || {}; "
10
+
11
+ out << "#{js_namespace}.#{ob_name} = "
12
+ out << "<%= JsFlasher.get_templates([:#{ob_name}]) %>;"
13
+
14
+ FileUtils.mkdir_p(target_directory)
15
+
16
+ File.open("#{target_directory}/#{ob_name}.js.erb", 'w') {|f| f.write(out) }
17
+ end
18
+
19
+ end
20
+
21
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: js-flasher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jos Smith
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: A simple gem that exposes unprocessed JS templates
31
+ email:
32
+ - technicolorenvy@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - LICENSE
39
+ - README.md
40
+ - Rakefile
41
+ - js-flasher.gemspec
42
+ - lib/js-flasher.rb
43
+ - lib/js_flasher/configuration.rb
44
+ - lib/js_flasher/get_templates.rb
45
+ - lib/js_flasher/railtie.rb
46
+ - lib/js_flasher/version.rb
47
+ - lib/tasks/js-flasher.rake
48
+ homepage: https://github.com/technicolorenvy/js-flasher
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.24
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: A simple gem that exposes unprocessed JS templates
72
+ test_files: []