piper 0.0.2 → 3.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -2
- data/README.md +54 -0
- data/app/controllers/piper/partials_controller.rb +11 -0
- data/app/views/_wrapper.html.erb +6 -0
- data/lib/assets/javascripts/piper.js +15 -0
- data/lib/piper.rb +8 -116
- data/lib/piper/encoding.rb +16 -0
- data/lib/piper/engine.rb +7 -0
- data/lib/piper/helper.rb +15 -0
- data/lib/piper/railtie.rb +10 -0
- data/lib/piper/version.rb +2 -3
- data/piper.gemspec +15 -9
- data/piper_demo.gif +0 -0
- data/render.png +0 -0
- metadata +68 -59
- data/.rspec +0 -2
- data/Gemfile +0 -9
- data/Rakefile +0 -2
- data/spec/lib/piper_spec.rb +0 -32
- data/spec/spec_helper.rb +0 -5
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cc3741d101dc8595741550217d9f75380cabe893
|
4
|
+
data.tar.gz: 461c22041159c6ab6931ad0e2b46b5fcee44e031
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0a37af6c807058ffff32d00e48104aab49916b45e13139d85768c3d7990c5b0faf5310aae0d3477c4527c75c8417c3a86d1542d7b74b0f6c46e692af7c64df36
|
7
|
+
data.tar.gz: ed2f9067d521b85e5f9903febd3731dfb55a74a6fbaa874a87dc82a4d2f43219f41926c3aa8fc94ae60ccedfb69d38e83517d09f0eb7fc7f141ca38fceeb9266
|
data/.gitignore
CHANGED
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
[![License](https://img.shields.io/badge/license-MIT-green.svg)](http://opensource.org/licenses/MIT)
|
2
|
+
|
3
|
+
Are you web pages composed of many rails partials? Wouldn't it be cool if the partials can be told to refresh itself?
|
4
|
+
|
5
|
+
This gem introduces a jquery method `.reload()`, when executed, pulls a fresh copy of the partial and renders it inside the div.
|
6
|
+
|
7
|
+
![demo](https://raw.githubusercontent.com/oystersauce8/piper/master/piper_demo.gif)
|
8
|
+
|
9
|
+
This, combined with [Server Generated Javacript Responses](https://signalvnoise.com/posts/3697-server-generated-javascript-responses) gives your app SPA (Single Page App) like benefits, without the added complexity of a client-side JavaScript framework
|
10
|
+
|
11
|
+
To install, add this to your Gemfile
|
12
|
+
|
13
|
+
gem "piper"
|
14
|
+
|
15
|
+
After that, specify the use of the jquery and best in place javascripts in your application.js
|
16
|
+
|
17
|
+
//= require piper
|
18
|
+
|
19
|
+
|
20
|
+
To make use of this gem, use `<%= render_cell %>` instead of `<%= render partial:` This wraps the rendered partial in a div, and that div gets locals metadata embeded in it as a `data-piper` attribute.
|
21
|
+
|
22
|
+
![render](https://raw.githubusercontent.com/oystersauce8/piper/master/render.png)
|
23
|
+
|
24
|
+
`render_cell` also expects an addditional argument, called container, which serves as the jquery selector, so if you pass *container: 'foo'* your resulting javascript command becomes `$('.foo').reload()`
|
25
|
+
|
26
|
+
Also remember to pass locals literally like this
|
27
|
+
|
28
|
+
locals: '{ var1: "abc", var2: Blog.first }' # single quotes!
|
29
|
+
|
30
|
+
This means we're "passing by reference".
|
31
|
+
|
32
|
+
You will also need to add this route
|
33
|
+
|
34
|
+
get '/partial' => 'piper/partials#show'
|
35
|
+
|
36
|
+
|
37
|
+
### Prereqs
|
38
|
+
|
39
|
+
Assumes jquery-ui has been installed in the rails app.
|
40
|
+
|
41
|
+
|
42
|
+
### Authors, License and Stuff
|
43
|
+
|
44
|
+
MIT license.
|
45
|
+
Many thanks to best_in_place project on github. I borrowed quite a few ideas from it.
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
*
|
3
|
+
* @requires jQuery
|
4
|
+
*
|
5
|
+
*/
|
6
|
+
|
7
|
+
jQuery.fn.reload = function () {
|
8
|
+
var that = this;
|
9
|
+
$.getJSON( '/partial?'+
|
10
|
+
'data=' + $(this).data('piper')
|
11
|
+
).complete( function(data) {
|
12
|
+
$(that).html(data.responseText);
|
13
|
+
$(that).toggle( "clip" ).toggle( "clip" );
|
14
|
+
});
|
15
|
+
};
|
data/lib/piper.rb
CHANGED
@@ -1,119 +1,11 @@
|
|
1
|
+
require 'rails/railtie'
|
2
|
+
require 'action_view/railtie'
|
3
|
+
require 'action_controller/railtie'
|
1
4
|
|
2
|
-
|
3
|
-
DestinyNotYetChosenException = Class.new(Exception)
|
4
|
-
CannotWriteToReaderException = Class.new(Exception)
|
5
|
-
CannotReadFromWriterException = Class.new(Exception)
|
6
|
-
|
7
|
-
def initialize
|
8
|
-
@readpipe, @writepipe = IO.pipe
|
9
|
-
end
|
10
|
-
|
11
|
-
def destiny_chosen?
|
12
|
-
!(@writer.nil? && @reader.nil?)
|
13
|
-
end
|
14
|
-
|
15
|
-
def writer!
|
16
|
-
if !destiny_chosen?
|
17
|
-
@writer = true
|
18
|
-
@readpipe.close
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def reader!
|
23
|
-
if !destiny_chosen?
|
24
|
-
@reader = true
|
25
|
-
@writepipe.close
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def writer?
|
30
|
-
!!@writer
|
31
|
-
end
|
32
|
-
|
33
|
-
def reader?
|
34
|
-
!!@reader
|
35
|
-
end
|
36
|
-
|
37
|
-
def <<(obj)
|
38
|
-
raise CannotWriteToReader if reader?
|
39
|
-
|
40
|
-
# Force writer
|
41
|
-
writer!
|
42
|
-
|
43
|
-
dump wrap(obj)
|
44
|
-
end
|
45
|
-
|
46
|
-
def close
|
47
|
-
dump :close => true if writer?
|
48
|
-
end
|
49
|
-
|
50
|
-
def load
|
51
|
-
raise CannotReadFromWriterException if writer?
|
52
|
-
|
53
|
-
# Force reader
|
54
|
-
reader!
|
55
|
-
|
56
|
-
Marshal.load(@readpipe)
|
57
|
-
end
|
58
|
-
|
59
|
-
def raw
|
60
|
-
raise DestinyNotYetChosenException if !destiny_chosen?
|
61
|
-
|
62
|
-
reader? ? @readpipe : @writepipe
|
63
|
-
end
|
64
|
-
|
65
|
-
protected
|
66
|
-
|
67
|
-
def dump(obj)
|
68
|
-
Marshal.dump(obj, @writepipe)
|
69
|
-
end
|
70
|
-
|
71
|
-
def wrap(obj)
|
72
|
-
{:object => obj}
|
73
|
-
end
|
74
|
-
|
75
|
-
class << self
|
76
|
-
def receive(*pipes)
|
77
|
-
open_pipes = [*pipes]
|
78
|
-
|
79
|
-
# Force all pipes to be readers
|
80
|
-
open_pipes.each {|p| p.reader! }
|
81
|
-
|
82
|
-
pipes_by_raw_read_pipe = {}
|
83
|
-
open_pipes.each do |p|
|
84
|
-
pipes_by_raw_read_pipe[p.raw] = p
|
85
|
-
end
|
86
|
-
|
87
|
-
loop do
|
88
|
-
ready = IO.select(open_pipes.map {|p| p.raw }, nil, nil, nil)
|
89
|
-
|
90
|
-
# Should always be ready, but if not, somehow it
|
91
|
-
# timed out. Throw error
|
92
|
-
if !ready
|
93
|
-
raise Exception
|
94
|
-
else
|
95
|
-
ready_pipes = ready[0].select {|p|
|
96
|
-
!p.eof?
|
97
|
-
}.map {|raw_pipe|
|
98
|
-
pipes_by_raw_read_pipe[raw_pipe]
|
99
|
-
}
|
100
|
-
|
101
|
-
ready_pipes.each do |pipe|
|
102
|
-
wrapper = pipe.load
|
103
|
-
|
104
|
-
if wrapper[:close]
|
105
|
-
pipes_by_raw_read_pipe.delete(pipe.raw)
|
106
|
-
|
107
|
-
open_pipes.delete(pipe)
|
108
|
-
return if open_pipes.empty?
|
109
|
-
else
|
110
|
-
object = wrapper.delete :object
|
111
|
-
yield(object)
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
5
|
+
module Piper
|
118
6
|
end
|
119
7
|
|
8
|
+
require 'piper/engine'
|
9
|
+
require 'piper/helper'
|
10
|
+
require 'piper/railtie'
|
11
|
+
require 'piper/encoding'
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Piper
|
2
|
+
module Encoding
|
3
|
+
|
4
|
+
def self.encode(name,locals)
|
5
|
+
verifier = ActiveSupport::MessageVerifier.new( Rails.application.secrets[:secret_key_base] )
|
6
|
+
verifier.generate("#{name};#{locals}")
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.decode(arg)
|
10
|
+
verifier = ActiveSupport::MessageVerifier.new( Rails.application.secrets[:secret_key_base] )
|
11
|
+
verifier.verify(arg).split(';')
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
data/lib/piper/engine.rb
ADDED
data/lib/piper/helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module Piper
|
2
|
+
module Helper
|
3
|
+
def render_cell(name,locals:,container:)
|
4
|
+
render partial: name,
|
5
|
+
locals: eval(locals).merge({
|
6
|
+
metadata: {
|
7
|
+
:container => container,
|
8
|
+
:name => name,
|
9
|
+
:locals => locals
|
10
|
+
}}),
|
11
|
+
layout: "/wrapper"
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
data/lib/piper/version.rb
CHANGED
data/piper.gemspec
CHANGED
@@ -1,21 +1,27 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
$:.push File.expand_path("../lib", __FILE__)
|
3
|
-
require
|
3
|
+
require 'piper/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "piper"
|
7
7
|
s.version = Piper::VERSION
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
|
-
s.authors = ["
|
10
|
-
s.email = ["
|
11
|
-
s.homepage = "http://github.com/
|
12
|
-
s.summary =
|
13
|
-
|
14
|
-
|
15
|
-
s.
|
9
|
+
s.authors = ["Sam Weerasinghr"]
|
10
|
+
s.email = ["oystersauce8@gmail.com"]
|
11
|
+
s.homepage = "http://www.github.com/oystersauce8/piper"
|
12
|
+
s.summary = <<SUM
|
13
|
+
Spice up your vanilla rails app with some SPA like benefits
|
14
|
+
SUM
|
15
|
+
s.description = <<DESC
|
16
|
+
Spice up your vanilla rails app with some SPA like benefits without the added complexity of a client-side JavaScript framework. From within javascript, there is no straightforward way (at least I'm aware of) to invalidate/reload a partial. Articles describing techniques such as this one have been around for some time now. This gem builds on techniques such as that one, and allows you to reload partials by following a few simple conventions.
|
17
|
+
DESC
|
16
18
|
|
17
19
|
s.files = `git ls-files`.split("\n")
|
18
|
-
s.test_files = `git ls-files -- {
|
20
|
+
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
19
21
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
22
|
s.require_paths = ["lib"]
|
23
|
+
|
24
|
+
s.add_runtime_dependency 'actionpack', '>= 3.2'
|
25
|
+
s.add_runtime_dependency 'railties', '>= 3.2'
|
26
|
+
|
21
27
|
end
|
data/piper_demo.gif
ADDED
Binary file
|
data/render.png
ADDED
Binary file
|
metadata
CHANGED
@@ -1,77 +1,86 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: piper
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 2
|
10
|
-
version: 0.0.2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 3.1.1
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
13
|
-
-
|
6
|
+
authors:
|
7
|
+
- Sam Weerasinghr
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
-
|
11
|
+
date: 2017-10-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionpack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: railties
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.2'
|
41
|
+
description: |2
|
42
|
+
Spice up your vanilla rails app with some SPA like benefits without the added complexity of a client-side JavaScript framework. From within javascript, there is no straightforward way (at least I'm aware of) to invalidate/reload a partial. Articles describing techniques such as this one have been around for some time now. This gem builds on techniques such as that one, and allows you to reload partials by following a few simple conventions.
|
43
|
+
email:
|
44
|
+
- oystersauce8@gmail.com
|
25
45
|
executables: []
|
26
|
-
|
27
46
|
extensions: []
|
28
|
-
|
29
47
|
extra_rdoc_files: []
|
30
|
-
|
31
|
-
files:
|
48
|
+
files:
|
32
49
|
- .gitignore
|
33
|
-
- .
|
34
|
-
-
|
35
|
-
-
|
50
|
+
- README.md
|
51
|
+
- app/controllers/piper/partials_controller.rb
|
52
|
+
- app/views/_wrapper.html.erb
|
53
|
+
- lib/assets/javascripts/piper.js
|
36
54
|
- lib/piper.rb
|
55
|
+
- lib/piper/encoding.rb
|
56
|
+
- lib/piper/engine.rb
|
57
|
+
- lib/piper/helper.rb
|
58
|
+
- lib/piper/railtie.rb
|
37
59
|
- lib/piper/version.rb
|
38
60
|
- piper.gemspec
|
39
|
-
-
|
40
|
-
-
|
41
|
-
|
42
|
-
homepage: http://github.com/iamjwc/piper
|
61
|
+
- piper_demo.gif
|
62
|
+
- render.png
|
63
|
+
homepage: http://www.github.com/oystersauce8/piper
|
43
64
|
licenses: []
|
44
|
-
|
65
|
+
metadata: {}
|
45
66
|
post_install_message:
|
46
67
|
rdoc_options: []
|
47
|
-
|
48
|
-
require_paths:
|
68
|
+
require_paths:
|
49
69
|
- lib
|
50
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
none: false
|
61
|
-
requirements:
|
62
|
-
- - ">="
|
63
|
-
- !ruby/object:Gem::Version
|
64
|
-
hash: 3
|
65
|
-
segments:
|
66
|
-
- 0
|
67
|
-
version: "0"
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
68
80
|
requirements: []
|
69
|
-
|
70
|
-
|
71
|
-
rubygems_version: 1.4.2
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.0.14.1
|
72
83
|
signing_key:
|
73
|
-
specification_version:
|
74
|
-
summary:
|
75
|
-
test_files:
|
76
|
-
- spec/lib/piper_spec.rb
|
77
|
-
- spec/spec_helper.rb
|
84
|
+
specification_version: 4
|
85
|
+
summary: Spice up your vanilla rails app with some SPA like benefits
|
86
|
+
test_files: []
|
data/.rspec
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
data/spec/lib/piper_spec.rb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
require File.expand_path("../../spec_helper", __FILE__)
|
2
|
-
|
3
|
-
describe Piper do
|
4
|
-
it "should be able to send a message between two processes" do
|
5
|
-
send_to_child("hi").should == "hi"
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should be able to send all different types of objects" do
|
9
|
-
send_to_child(:hey).should == :hey
|
10
|
-
send_to_child(1).should == 1
|
11
|
-
send_to_child(1.3).should == 1.3
|
12
|
-
send_to_child([1,2,3]).should == [1,2,3]
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should use a special message when sending" do
|
16
|
-
send_to_child(:close).should == :close
|
17
|
-
end
|
18
|
-
|
19
|
-
def send_to_child(message)
|
20
|
-
p = Piper.new
|
21
|
-
|
22
|
-
if fork
|
23
|
-
m = nil
|
24
|
-
Piper.receive(p) {|message| m = message }
|
25
|
-
m
|
26
|
-
else
|
27
|
-
p << message
|
28
|
-
p.close
|
29
|
-
exit!
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|