sinatra-ember 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README.md +62 -4
  2. data/lib/sinatra/ember.rb +51 -14
  3. metadata +10 -5
data/README.md CHANGED
@@ -2,12 +2,14 @@
2
2
 
3
3
  Helpers for serving an [Ember.js][ember] app from [Sinatra][sinatra].
4
4
 
5
- - Pre-compile handlebars templates from separate view files rather than littering them throughout your html.
5
+ - Pre-compile handlebars templates from separate view files rather than
6
+ littering them throughout your html.
6
7
 
7
8
 
8
9
  ## Installation
9
10
 
10
- You can install Sinatra Ember as a Ruby gem. You can install it via `gem install`.
11
+ You can install Sinatra Ember as a Ruby gem. You can install it via `gem
12
+ install`.
11
13
 
12
14
  ``` console
13
15
  $ gem install sinatra-ember
@@ -31,13 +33,69 @@ require 'sinatra/ember'
31
33
 
32
34
  class App < Sinatra::Base
33
35
  register Sinatra::Ember
34
- serve_templates '/js/templates.js'
36
+ ember {
37
+ templates '/js/templates.js', ['app/templates/**/*.hbs'], :relative_to => 'app/templates'
38
+
39
+ # optional; defaults to :path
40
+ template_name_style :path
41
+ }
35
42
  end
36
43
  ```
37
44
 
45
+
46
+ ## API reference
47
+
48
+ ### ember.templates
49
+
50
+ Combines templates in the given paths into a single asset of those templates in
51
+ javascript form.
52
+
53
+ ```ruby
54
+ # Usage:
55
+ ember {
56
+ templates 'URI', [PATH, PATH, ...], OPTIONS_HASH
57
+ }
58
+ ```
59
+
60
+ `URI`
61
+ > a string defining where the templates will be served.
62
+
63
+ `PATH`
64
+ > a string or glob pattern describing a file or files that should be included
65
+
66
+ #### Options
67
+
68
+ `:relative_to`
69
+ > a string describing a path relative to the Sinatra root from which the
70
+ > template names should be made relative to when using the `:path` template
71
+ > name style. For example, if `:relative_to` is `app/templates` and the `PATH`
72
+ > `app/templates/views/post.hbs` will be named `views/post`.
73
+
74
+
75
+ ### ember.template\_name\_style
76
+
77
+ Defines the style by which template names will be derived from the paths.
78
+
79
+ ```ruby
80
+ # Usage:
81
+ ember {
82
+ template_name_style :basename
83
+ }
84
+ ```
85
+
86
+ Valid options include:
87
+
88
+ `:path`
89
+ > the template will be named as a relative path to the template file
90
+
91
+ `:basename`
92
+ > the template will be named as the basename of the template file
93
+
94
+
38
95
  ## Sinatra AssetPack
39
96
 
40
- If you're using the [sinatra-assetpack][assetpack] gem, add your served templates to a package.
97
+ If you're using the [sinatra-assetpack][assetpack] gem, add your served
98
+ templates to a package.
41
99
 
42
100
 
43
101
  [assetpack]: https://github.com/rstacruz/sinatra-assetpack
@@ -1,7 +1,7 @@
1
1
  module Sinatra
2
2
  module Ember
3
3
  def self.version
4
- "0.0.1"
4
+ "0.0.2"
5
5
  end
6
6
 
7
7
  def self.registered(app)
@@ -17,26 +17,39 @@ module Sinatra
17
17
  end
18
18
 
19
19
  def ember_init!
20
- ember.template_packages.each do |route, globs|
20
+ ember.template_packages.each do |route, spec|
21
21
  get route do
22
22
  mtime, output = @template_cache.fetch(route) do
23
23
  # find all the template files
24
- paths = globs.map do |glob|
24
+ paths = spec.globs.map do |glob|
25
25
  glob = File.expand_path(File.join(settings.root, glob))
26
26
  Dir[glob].map { |x| x.squeeze('/') }
27
27
  end
28
- paths = paths.flatten.uniq
29
28
 
30
- # build up template hash
31
- template_paths = {}
32
- paths.each do |path|
33
- template_paths[File.basename(path, '.hbs')] = path
29
+ # define the portion of the path to remove
30
+ remove = settings.root
31
+ if spec.opts[:relative_to].empty?
32
+ remove = settings.root
33
+ else
34
+ remove = File.join(remove, spec.opts[:relative_to])
34
35
  end
36
+ remove = File.expand_path(remove)
35
37
 
36
38
  # build up the javascript
37
- templates = template_paths.map do |(name, path)|
38
- content = File.read(path)
39
- "Ember.TEMPLATES[#{name.inspect}] = Ember.Handlebars.compile(#{content.inspect});"
39
+ templates = paths.map do |files|
40
+ files.map do |file|
41
+ # derive the template name
42
+ tmpl_name = file.sub(/\.(handlebars|hbs|hjs)$/, '')
43
+ if settings.ember.template_name_style == :path
44
+ tmpl_name.sub!(/^#{Regexp.quote(remove)}\//, '')
45
+ else
46
+ tmpl_name = File.basename(tmpl_name)
47
+ end
48
+
49
+ # add the template content
50
+ content = File.read(file)
51
+ "Ember.TEMPLATES[#{tmpl_name.inspect}] = Ember.Handlebars.compile(#{content.inspect});"
52
+ end
40
53
  end
41
54
 
42
55
  # wrap it up in a closure
@@ -48,7 +61,7 @@ module Sinatra
48
61
  output = output.strip.gsub(/^ {16}/, '')
49
62
 
50
63
  # compute the maximum mtime for all paths
51
- mtime = paths.map do |path|
64
+ mtime = paths.flatten.map do |path|
52
65
  if File.file?(path)
53
66
  File.mtime(path).to_i
54
67
  end
@@ -70,13 +83,37 @@ module Sinatra
70
83
 
71
84
  def initialize(app, &block)
72
85
  @app = app
86
+ @template_name_style = :path
73
87
  @template_packages = {}
74
88
 
75
89
  instance_eval(&block)
76
90
  end
77
91
 
78
- def templates(route, files=[])
79
- @template_packages[route] = files
92
+ def templates(route, files=[], opts={})
93
+ opts_defaults = {
94
+ :relative_to => '',
95
+ }
96
+ opts = opts_defaults.merge(opts)
97
+ @template_packages[route] = TemplateSpec.new(route, files, opts)
98
+ end
99
+
100
+ def template_name_style(*args)
101
+ val = args.first
102
+ if not val.nil?
103
+ @template_name_style = val
104
+ end
105
+ return @template_name_style
106
+ end
107
+ alias :template_name_style= :template_name_style
108
+ end
109
+
110
+ class TemplateSpec
111
+ attr_accessor :route, :globs, :opts
112
+
113
+ def initialize(route, globs, opts)
114
+ @route = route
115
+ @globs = globs
116
+ @opts = opts
80
117
  end
81
118
  end
82
119
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-ember
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-07 00:00:00.000000000 Z
12
+ date: 2012-11-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
16
- requirement: &70219783856880 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,12 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70219783856880
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  description: Serves Ember Handlebars pages.
26
31
  email:
27
32
  - gamepoet@gmail.com
@@ -53,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
58
  version: '0'
54
59
  requirements: []
55
60
  rubyforge_project:
56
- rubygems_version: 1.8.17
61
+ rubygems_version: 1.8.24
57
62
  signing_key:
58
63
  specification_version: 3
59
64
  summary: Helpers for serving an Ember.js app from Sinatra.