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 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
@@ -1,4 +1,14 @@
1
- *.gem
1
+ .DS_Store
2
2
  .bundle
3
- Gemfile.lock
3
+ .idea
4
4
  pkg/*
5
+
6
+ *.lock
7
+ spec/internal/.bundle
8
+ spec/internal/db/*.sqlite3
9
+ spec/internal/log/*.log
10
+ spec/internal/tmp/**/*
11
+
12
+ .rvmrc
13
+ coverage
14
+ tmp
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,11 @@
1
+ module Piper
2
+ class PartialsController < ApplicationController
3
+ def show
4
+ name, locals = Piper::Encoding::decode(params[:data])
5
+ render partial: name, locals: eval(locals)
6
+ end
7
+
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,6 @@
1
+ <div
2
+ class="<%=metadata[:container]%>"
3
+ data-piper="<%= Piper::Encoding::encode(metadata[:name],metadata[:locals]) %>"
4
+ >
5
+ <%= yield %>
6
+ </div>
@@ -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
- class Piper
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
@@ -0,0 +1,7 @@
1
+ module Piper
2
+ class Engine < Rails::Engine
3
+ initializer 'piper' do
4
+ ActionView::Base.send(:include, Piper::Helper)
5
+ end
6
+ end
7
+ end
@@ -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
@@ -0,0 +1,10 @@
1
+ require 'rails/railtie'
2
+ require 'action_view/base'
3
+
4
+ module Piper
5
+ class Railtie < ::Rails::Railtie #:nodoc:
6
+ config.after_initialize do
7
+ Piper::ViewHelpers = ActionView::Base.new
8
+ end
9
+ end
10
+ end
data/lib/piper/version.rb CHANGED
@@ -1,4 +1,3 @@
1
- class Piper
2
- VERSION = "0.0.2"
1
+ module Piper
2
+ VERSION = '3.1.1'
3
3
  end
4
-
data/piper.gemspec CHANGED
@@ -1,21 +1,27 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require "piper/version"
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 = ["Justin Camerer"]
10
- s.email = ["iamjwc@gmail.com"]
11
- s.homepage = "http://github.com/iamjwc/piper"
12
- s.summary = %q{Wrapper around IO.pipe to allow easier communication between multiple processes.}
13
- s.description = %q{Check the summary.}
14
-
15
- s.rubyforge_project = "piper"
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 -- {test,spec,features}/*`.split("\n")
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
- hash: 27
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
- - Justin Camerer
6
+ authors:
7
+ - Sam Weerasinghr
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2011-05-12 00:00:00 -04:00
19
- default_executable:
20
- dependencies: []
21
-
22
- description: Check the summary.
23
- email:
24
- - iamjwc@gmail.com
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
- - .rspec
34
- - Gemfile
35
- - Rakefile
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
- - spec/lib/piper_spec.rb
40
- - spec/spec_helper.rb
41
- has_rdoc: true
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
- none: false
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- hash: 3
56
- segments:
57
- - 0
58
- version: "0"
59
- required_rubygems_version: !ruby/object:Gem::Requirement
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
- rubyforge_project: piper
71
- rubygems_version: 1.4.2
81
+ rubyforge_project:
82
+ rubygems_version: 2.0.14.1
72
83
  signing_key:
73
- specification_version: 3
74
- summary: Wrapper around IO.pipe to allow easier communication between multiple processes.
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
@@ -1,2 +0,0 @@
1
- --colour
2
-
data/Gemfile DELETED
@@ -1,9 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- # Specify your gem's dependencies in piper.gemspec
4
- gemspec
5
-
6
- group :test do
7
- gem "rspec"
8
- end
9
-
data/Rakefile DELETED
@@ -1,2 +0,0 @@
1
- require 'bundler'
2
- Bundler::GemHelper.install_tasks
@@ -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
data/spec/spec_helper.rb DELETED
@@ -1,5 +0,0 @@
1
- Bundler.setup(:test)
2
- require 'rspec'
3
-
4
- require File.expand_path(File.dirname(__FILE__)+'/../lib/piper')
5
-