perspectives 0.0.1
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.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +13 -0
- data/lib/generators/perspectives/install.rb +38 -0
- data/lib/generators/perspectives/scaffold/scaffold_generator.rb +54 -0
- data/lib/generators/perspectives/scaffold/templates/edit.mustache +6 -0
- data/lib/generators/perspectives/scaffold/templates/edit.rb +8 -0
- data/lib/generators/perspectives/scaffold/templates/form.mustache +25 -0
- data/lib/generators/perspectives/scaffold/templates/form.rb +27 -0
- data/lib/generators/perspectives/scaffold/templates/index.mustache +22 -0
- data/lib/generators/perspectives/scaffold/templates/index.rb +9 -0
- data/lib/generators/perspectives/scaffold/templates/new.mustache +5 -0
- data/lib/generators/perspectives/scaffold/templates/new.rb +7 -0
- data/lib/generators/perspectives/scaffold/templates/show.mustache +10 -0
- data/lib/generators/perspectives/scaffold/templates/show.rb +8 -0
- data/lib/generators/perspectives/scaffold/templates/tiny.mustache +8 -0
- data/lib/generators/perspectives/scaffold/templates/tiny.rb +8 -0
- data/lib/generators/perspectives/templates/application.js +13 -0
- data/lib/generators/perspectives/templates/rails/scaffold_controller/controller.rb +82 -0
- data/lib/perspectives/active_record.rb +17 -0
- data/lib/perspectives/base.rb +30 -0
- data/lib/perspectives/caching.rb +82 -0
- data/lib/perspectives/collection.rb +21 -0
- data/lib/perspectives/configuration.rb +14 -0
- data/lib/perspectives/context.rb +18 -0
- data/lib/perspectives/controller_additions.rb +142 -0
- data/lib/perspectives/forms/base.rb +5 -0
- data/lib/perspectives/forms/text_field.rb +15 -0
- data/lib/perspectives/forms.rb +7 -0
- data/lib/perspectives/memoization.rb +30 -0
- data/lib/perspectives/mustache_compiler.rb +33 -0
- data/lib/perspectives/params.rb +48 -0
- data/lib/perspectives/properties.rb +130 -0
- data/lib/perspectives/railtie.rb +42 -0
- data/lib/perspectives/rendering.rb +15 -0
- data/lib/perspectives/responder.rb +15 -0
- data/lib/perspectives/templating.rb +39 -0
- data/lib/perspectives/version.rb +3 -0
- data/lib/perspectives.rb +48 -0
- data/lib/rails/projections.json +12 -0
- data/lib/tasks/perspectives_tasks.rake +4 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config/application.rb +23 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +29 -0
- data/spec/dummy/config/environments/production.rb +80 -0
- data/spec/dummy/config/environments/test.rb +36 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +12 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +56 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/log/test.log +0 -0
- data/spec/dummy/public/404.html +58 -0
- data/spec/dummy/public/422.html +58 -0
- data/spec/dummy/public/500.html +57 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/lib/perspectives/properties_spec.rb +31 -0
- data/spec/lib/perspectives/templating_spec.rb +18 -0
- data/spec/lib/perspectives_spec.rb +5 -0
- data/spec/mustaches/users/simple_info.mustache +1 -0
- data/spec/rails_helper.rb +11 -0
- data/spec/spec_helper.rb +16 -0
- data/vendor/assets/javascripts/mustache-0.8.1.js +570 -0
- data/vendor/assets/javascripts/perspectives/forms/text_field.mustache +3 -0
- data/vendor/assets/javascripts/perspectives.js +152 -0
- data/vendor/assets/javascripts/perspectives_views.js +1 -0
- metadata +233 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c92d0eb6138314d35867e5450affaf7ebc7dfb79
|
4
|
+
data.tar.gz: ae22657295f39e2e948cef53acf8182c0ec68250
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0a52edcc6118fd6840d4df8685f735e2bc0553a6aa6eed2b7626219137370ec3c00b7b18a7d90f344461407f07cf7f160279ec7589787b884c7d037778211c8f
|
7
|
+
data.tar.gz: 6755f9c7e108b17ee9c019c5d453c91d8ad195b7dcd947d904e4750d34253cc2b93a93b8f203deecb462ac522be43437ee8172bd59874366c71fd0ac9817a3a9
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
Bundler::GemHelper.install_tasks
|
8
|
+
|
9
|
+
require 'rspec/core/rake_task'
|
10
|
+
|
11
|
+
RSpec::Core::RakeTask.new(:spec)
|
12
|
+
|
13
|
+
task :default => :spec
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module Perspectives
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < ::Rails::Generators::Base
|
6
|
+
source_root File.expand_path("../templates", __FILE__)
|
7
|
+
desc "Installs Perspectives and configures the Asset Pipeline"
|
8
|
+
|
9
|
+
def add_assets
|
10
|
+
js_manifest = 'app/assets/javascripts/application.js'
|
11
|
+
|
12
|
+
if File.exist?(js_manifest)
|
13
|
+
requirements = <<-REQS.strip
|
14
|
+
//= require mustache-0.8.1
|
15
|
+
//= require perspectives
|
16
|
+
//= require perspectives_views
|
17
|
+
//= require_tree ../../mustaches
|
18
|
+
REQS
|
19
|
+
|
20
|
+
gsub_file js_manifest, %r{^//= require turbolinks$}, ''
|
21
|
+
|
22
|
+
insert_into_file js_manifest, "#{requirements}", :after => "jquery_ujs\n"
|
23
|
+
insert_into_file js_manifest, "\n$(function() { $(document).perspectives('a', 'body') })\n", :after => "//= require_tree .\n"
|
24
|
+
else
|
25
|
+
copy_file "application.js", js_manifest
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def configure_directories
|
30
|
+
%w(app/mustaches app/perspectives).each do |dir|
|
31
|
+
empty_directory dir
|
32
|
+
create_file File.join(dir, '.keep')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/resource_helpers'
|
3
|
+
|
4
|
+
module Perspectives
|
5
|
+
module Generators
|
6
|
+
class ScaffoldGenerator < Rails::Generators::NamedBase
|
7
|
+
include Rails::Generators::ResourceHelpers
|
8
|
+
|
9
|
+
argument :attributes, type: :array, default: [], banner: "field:type field:type"
|
10
|
+
|
11
|
+
source_root File.expand_path("../templates", __FILE__)
|
12
|
+
|
13
|
+
def create_root_folders
|
14
|
+
empty_directory mustache_path
|
15
|
+
empty_directory perspectives_path
|
16
|
+
end
|
17
|
+
|
18
|
+
def copy_view_files
|
19
|
+
available_views.each do |view|
|
20
|
+
template "#{view}.mustache", mustache_path("#{view}.mustache")
|
21
|
+
template "#{view}.rb", perspectives_path("#{view}.rb")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# hook_for :form_builder, :as => :scaffold
|
26
|
+
|
27
|
+
def copy_form_file
|
28
|
+
filename = 'form.mustache'
|
29
|
+
template filename, mustache_path(filename)
|
30
|
+
|
31
|
+
filename = 'form.rb'
|
32
|
+
template filename, perspectives_path(filename)
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def available_views
|
38
|
+
%w(index tiny show new edit)
|
39
|
+
end
|
40
|
+
|
41
|
+
def handler
|
42
|
+
:perspectives
|
43
|
+
end
|
44
|
+
|
45
|
+
def mustache_path(filename = nil)
|
46
|
+
File.join(*["app/mustaches", controller_file_path, filename].compact)
|
47
|
+
end
|
48
|
+
|
49
|
+
def perspectives_path(filename = nil)
|
50
|
+
File.join(*["app/perspectives", controller_file_path, filename].compact)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
class <%= controller_class_name %>::Edit < Perspectives::Base
|
2
|
+
param :<%= singular_table_name %>
|
3
|
+
|
4
|
+
property(:show_href) { <%= singular_table_name %>_path(<%= singular_table_name %>) }
|
5
|
+
property(:index_href) { <%= plural_table_name %>_path }
|
6
|
+
|
7
|
+
nested 'form', <%= singular_table_name %>: :<%= singular_table_name %>
|
8
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
<form accept-charset="UTF-8" action="{{submit_to}}" method="post" data-type="json" data-remote="true">
|
2
|
+
{{#errors}}
|
3
|
+
<div id="error_explanation">
|
4
|
+
<h3>{{error_count}} prohibited this {{name}} from being saved:</h3>
|
5
|
+
|
6
|
+
<ul>
|
7
|
+
{{#error_messages}}
|
8
|
+
<li>{{msg}}</li>
|
9
|
+
{{/error_messages}}
|
10
|
+
</ul>
|
11
|
+
</div>
|
12
|
+
{{/errors}}
|
13
|
+
|
14
|
+
{{#submit_method}}
|
15
|
+
<input type="hidden" name="_method" value="{{submit_method}}">
|
16
|
+
{{/submit_method}}
|
17
|
+
|
18
|
+
<% attributes.each do |attribute| -%>
|
19
|
+
{{{<%= attribute.name %>_field}}}
|
20
|
+
<% end -%>
|
21
|
+
|
22
|
+
<div class="actions">
|
23
|
+
<input name="commit" type="submit" value="Submit" data-disable-with="Working...">
|
24
|
+
</div>
|
25
|
+
</form>
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class <%= controller_class_name %>::Form < Perspectives::Base
|
2
|
+
param :<%= singular_table_name %>
|
3
|
+
|
4
|
+
property(:submit_to) do
|
5
|
+
<%= singular_table_name %>.new_record? ? <%= plural_table_name %>_path : <%= singular_table_name %>_path(<%= singular_table_name %>)
|
6
|
+
end
|
7
|
+
|
8
|
+
property(:submit_method) { !<%= singular_table_name %>.new_record? && 'patch' }
|
9
|
+
|
10
|
+
property(:errors) do
|
11
|
+
errors = <%= singular_table_name %>.errors
|
12
|
+
if errors.any?
|
13
|
+
{
|
14
|
+
error_count: errors.count,
|
15
|
+
name: <%= singular_table_name %>.class.name.humanize,
|
16
|
+
error_messages: object.errors.full_messages.map { |msg| {msg: msg } }
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
<% attributes.each do |attribute| -%>
|
22
|
+
nested 'perspectives/forms/text_field',
|
23
|
+
property: :<%= attribute.name %>_field,
|
24
|
+
locals: { object: :<%= singular_table_name %>, field: '<%= attribute.name %>' }
|
25
|
+
|
26
|
+
<% end -%>
|
27
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
<h1>Listing <%= plural_table_name %></h1>
|
2
|
+
|
3
|
+
<table>
|
4
|
+
<thead>
|
5
|
+
<tr>
|
6
|
+
<% attributes.reject(&:password_digest?).each do |attribute| -%>
|
7
|
+
<th><%= attribute.human_name %></th>
|
8
|
+
<% end -%>
|
9
|
+
<th></th>
|
10
|
+
<th></th>
|
11
|
+
<th></th>
|
12
|
+
</tr>
|
13
|
+
</thead>
|
14
|
+
|
15
|
+
<tbody>
|
16
|
+
{{{<%= plural_table_name %>}}}
|
17
|
+
</tbody>
|
18
|
+
</table>
|
19
|
+
|
20
|
+
<br>
|
21
|
+
|
22
|
+
<a href="{{new_href}}">New <%= singular_table_name %></a>
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class <%= controller_class_name %>::Index < Perspectives::Base
|
2
|
+
param :all_<%= plural_table_name %>
|
3
|
+
|
4
|
+
property(:new_href) { new_<%= singular_table_name %>_path }
|
5
|
+
|
6
|
+
nested_collection '<%= plural_table_name %>/tiny',
|
7
|
+
collection: proc { all_<%= plural_table_name %> },
|
8
|
+
property: :<%= plural_table_name %>
|
9
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
class <%= controller_class_name %>::Show < Perspectives::Base
|
2
|
+
param :<%= singular_table_name %>
|
3
|
+
|
4
|
+
delegate_property <%= attributes.map { |a| ":#{a.name}" }.join(', ') %>, to: :<%= singular_table_name %>
|
5
|
+
|
6
|
+
property(:edit_href) { edit_<%= singular_table_name %>_path(<%= singular_table_name %>) }
|
7
|
+
property(:index_href) { <%= plural_table_name %>_path }
|
8
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
<tr>
|
2
|
+
<% attributes.reject(&:password_digest?).each do |attribute| -%>
|
3
|
+
<td>{{<%= attribute.name %>}}</td>
|
4
|
+
<% end %>
|
5
|
+
<td><a href="{{show_href}}">Show</a></td>
|
6
|
+
<td><a href="{{edit_href}}">Edit</a></td>
|
7
|
+
<td><a href="{{show_href}}" data-method="delete" data-confirm="Are you sure?" rel="nofollow">Destroy</a></td>
|
8
|
+
</tr>
|
@@ -0,0 +1,8 @@
|
|
1
|
+
class <%= controller_class_name %>::Tiny < Perspectives::Base
|
2
|
+
param :<%= singular_table_name %>
|
3
|
+
|
4
|
+
delegate_property <%= attributes.map { |a| ":#{a.name}" }.join(', ') %>, to: :<%= singular_table_name %>
|
5
|
+
|
6
|
+
property(:show_href) { <%= singular_table_name %>_path(<%= singular_table_name %>) }
|
7
|
+
property(:edit_href) { edit_<%= singular_table_name %>_path(<%= singular_table_name %>) }
|
8
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into including all the files listed below.
|
2
|
+
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
|
3
|
+
// be included in the compiled file accessible from http://example.com/assets/application.js
|
4
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
5
|
+
// the compiled file.
|
6
|
+
//
|
7
|
+
//= require mustache-0.8.1
|
8
|
+
//= require perspectives
|
9
|
+
//= require_tree ../../mustaches
|
10
|
+
//= require perspectives_views
|
11
|
+
//= require_tree .
|
12
|
+
|
13
|
+
$(function() { $(document).perspectives('a', 'body') })
|
@@ -0,0 +1,82 @@
|
|
1
|
+
<% if namespaced? -%>
|
2
|
+
require_dependency "<%= namespaced_file_path %>/application_controller"
|
3
|
+
|
4
|
+
<% end -%>
|
5
|
+
<% module_namespacing do -%>
|
6
|
+
class <%= controller_class_name %>Controller < ApplicationController
|
7
|
+
before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
|
8
|
+
|
9
|
+
perspectives_actions
|
10
|
+
|
11
|
+
# GET <%= route_url %>
|
12
|
+
def index
|
13
|
+
respond_with(perspective('<%= plural_table_name %>/index', all_<%= plural_table_name %>: <%= orm_class.all(class_name) %>))
|
14
|
+
end
|
15
|
+
|
16
|
+
# GET <%= route_url %>/1
|
17
|
+
def show
|
18
|
+
respond_with(perspective('<%= plural_table_name %>/show', <%= singular_table_name %>: @<%= singular_table_name %>))
|
19
|
+
end
|
20
|
+
|
21
|
+
# GET <%= route_url %>/new
|
22
|
+
def new
|
23
|
+
respond_with(perspective('<%= plural_table_name %>/new', <%= singular_table_name %>: <%= orm_class.build(class_name) %>))
|
24
|
+
end
|
25
|
+
|
26
|
+
# GET <%= route_url %>/1/edit
|
27
|
+
def edit
|
28
|
+
respond_with(perspective('<%= plural_table_name %>/edit', <%= singular_table_name %>: @<%= singular_table_name %>))
|
29
|
+
end
|
30
|
+
|
31
|
+
# POST <%= route_url %>
|
32
|
+
def create
|
33
|
+
<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
|
34
|
+
|
35
|
+
if <%= orm_instance.save %>
|
36
|
+
flash[:notice] = <%= "'#{human_name} was successfully created.'" %>
|
37
|
+
|
38
|
+
respond_to do |format|
|
39
|
+
format.html { redirect_to <%= singular_table_name %> }
|
40
|
+
format.json { render json: perspective('<%= plural_table_name %>/show', <%= singular_table_name %>: <%= singular_table_name %>), status: :created, location: <%= singular_table_name %> }
|
41
|
+
end
|
42
|
+
else
|
43
|
+
respond_with(perspective('<%= plural_table_name %>/new', <%= singular_table_name %>: <%= singular_table_name %>))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# PATCH/PUT <%= route_url %>/1
|
48
|
+
def update
|
49
|
+
if @<%= orm_instance.update("#{singular_table_name}_params") %>
|
50
|
+
flash[:notice] = <%= "'#{human_name} was successfully updated.'" %>
|
51
|
+
|
52
|
+
respond_to do |format|
|
53
|
+
format.html { redirect_to @<%= singular_table_name %> }
|
54
|
+
format.json { render json: perspective('<%= plural_table_name %>/show', <%= singular_table_name %>: @<%= singular_table_name %>) }
|
55
|
+
end
|
56
|
+
else
|
57
|
+
respond_with(perspective('<%= plural_table_name %>/edit', <%= singular_table_name %>: @<%= singular_table_name %>))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# DELETE <%= route_url %>/1
|
62
|
+
def destroy
|
63
|
+
@<%= orm_instance.destroy %>
|
64
|
+
redirect_to <%= index_helper %>_url, notice: <%= "'#{human_name} was successfully destroyed.'" %>
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
# Use callbacks to share common setup or constraints between actions.
|
69
|
+
def set_<%= singular_table_name %>
|
70
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
71
|
+
end
|
72
|
+
|
73
|
+
# Only allow a trusted parameter "white list" through.
|
74
|
+
def <%= "#{singular_table_name}_params" %>
|
75
|
+
<%- if attributes_names.empty? -%>
|
76
|
+
params[<%= ":#{singular_table_name}" %>]
|
77
|
+
<%- else -%>
|
78
|
+
params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
|
79
|
+
<%- end -%>
|
80
|
+
end
|
81
|
+
end
|
82
|
+
<% end -%>
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Perspectives
|
2
|
+
module ActiveRecord
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def id_param
|
9
|
+
:"#{active_record_klass.name.underscore}_id"
|
10
|
+
end
|
11
|
+
|
12
|
+
def active_record_klass
|
13
|
+
name.split('::').first.singularize.constantize
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require 'perspectives/templating'
|
3
|
+
require 'perspectives/properties'
|
4
|
+
require 'perspectives/memoization'
|
5
|
+
require 'perspectives/params'
|
6
|
+
require 'perspectives/context'
|
7
|
+
require 'perspectives/rendering'
|
8
|
+
require 'perspectives/caching'
|
9
|
+
|
10
|
+
module Perspectives
|
11
|
+
class Base
|
12
|
+
include Templating
|
13
|
+
include Properties
|
14
|
+
include Memoization
|
15
|
+
include Params
|
16
|
+
include Context
|
17
|
+
include Rendering
|
18
|
+
include Caching
|
19
|
+
|
20
|
+
class << self
|
21
|
+
def inherited(base)
|
22
|
+
base.__send__(:filename=, caller.first[/^(.*?.rb):\d/, 1])
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
attr_accessor :filename
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Perspectives
|
2
|
+
module Caching
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
|
6
|
+
base.class_eval do
|
7
|
+
class_attribute :_cache_key_additions_block
|
8
|
+
delegate :_class_source_digest, :_mustache_source_digest, to: 'self.class'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def render_html
|
13
|
+
_with_cache('html') { super }
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_json(options = {})
|
17
|
+
_with_cache('json') { super }
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def _cache
|
23
|
+
Perspectives.cache
|
24
|
+
end
|
25
|
+
|
26
|
+
def _expand_cache_key(*args, &block)
|
27
|
+
Perspectives.expand_cache_key(*args, &block)
|
28
|
+
end
|
29
|
+
|
30
|
+
def _with_cache(*key_additions)
|
31
|
+
return yield unless _caching?
|
32
|
+
|
33
|
+
_cache.fetch(_expand_cache_key(_cache_key.concat(key_additions))) { yield }
|
34
|
+
end
|
35
|
+
|
36
|
+
def _cache_key
|
37
|
+
return [] unless _caching?
|
38
|
+
|
39
|
+
[].tap do |key|
|
40
|
+
key << self.class.to_s
|
41
|
+
key << _mustache_source_digest
|
42
|
+
key << _class_source_digest
|
43
|
+
key.concat(Array(instance_eval(&_cache_key_additions_block))) if _cache_key_additions_block
|
44
|
+
key.concat _dependent_cache_keys
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def _caching?
|
49
|
+
Perspectives.caching? && !!_cache_key_additions_block
|
50
|
+
end
|
51
|
+
|
52
|
+
def _dependent_cache_keys
|
53
|
+
_nested_perspectives.each_with_object([]) do |property_name, key|
|
54
|
+
perspectives = __send__(property_name)
|
55
|
+
|
56
|
+
case perspectives
|
57
|
+
when NilClass
|
58
|
+
when Array
|
59
|
+
key.concat(perspectives.map { |p| p.__send__(:_cache_key) }.flatten)
|
60
|
+
else
|
61
|
+
key.concat(perspectives.__send__(:_cache_key))
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
module ClassMethods
|
67
|
+
def cache(&block)
|
68
|
+
raise ArgumentError, "No block given" unless block_given?
|
69
|
+
|
70
|
+
self._cache_key_additions_block = block
|
71
|
+
end
|
72
|
+
|
73
|
+
def _class_source_digest
|
74
|
+
@_class_source_digest ||= Digest::MD5.hexdigest(File.read(filename))
|
75
|
+
end
|
76
|
+
|
77
|
+
def _mustache_source_digest
|
78
|
+
@_mustache_source_digest ||= Digest::MD5.hexdigest(_mustache.template.source)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Perspectives
|
2
|
+
class Collection
|
3
|
+
include ::Enumerable
|
4
|
+
|
5
|
+
def initialize(perspectives)
|
6
|
+
@perspectives = perspectives
|
7
|
+
end
|
8
|
+
|
9
|
+
def each(&block)
|
10
|
+
perspectives.each(&block)
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_html
|
14
|
+
perspectives.map(&:to_html).join
|
15
|
+
end
|
16
|
+
alias_method :to_s, :to_html
|
17
|
+
|
18
|
+
private
|
19
|
+
attr_reader :perspectives
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Perspectives
|
2
|
+
class Configuration
|
3
|
+
delegate :template_path, :template_path=, :raise_on_context_miss?, :raise_on_context_miss, :raise_on_context_miss=, to: 'Mustache'
|
4
|
+
|
5
|
+
CacheNotConfigured = Class.new(StandardError)
|
6
|
+
attr_writer :cache
|
7
|
+
attr_accessor :caching
|
8
|
+
alias_method :caching?, :caching
|
9
|
+
|
10
|
+
def cache
|
11
|
+
@cache || (raise CacheNotConfigured, "You must configure a cache")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Perspectives
|
2
|
+
module Context
|
3
|
+
def respond_to?(method, include_private = false)
|
4
|
+
super || context.key?(method)
|
5
|
+
end
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def method_missing(method, *args, &block)
|
10
|
+
if args.empty? && !block_given? && context.key?(method)
|
11
|
+
self.class.__send__(:define_method, method) { __send__(:context).fetch(method) }
|
12
|
+
__send__(method)
|
13
|
+
else
|
14
|
+
super
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|