rails-multi_block_helpers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZWY1MzgwODQwOWFmNTRjNzNhOGRiNzM1ZjM3MzJkOTBkZGRhMjliMA==
5
+ data.tar.gz: !binary |-
6
+ NjcwMzYxYWEwZDRkNzhlN2QzYTFlNjhjMzdjOTVmMzA1MTFiNTViMg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ M2VjNDdiNDczMTFjNGZlMTlkZDg1ZGE5NGNiNzYxY2Y4MzEwZTVhOGE1ZTQz
10
+ MzE2NjE4ZGU1MDdlYWYwYWQwYzY3M2YzYmEzYTg0MjZhMjZhYjIyY2QxYmFj
11
+ YmZhY2I4ZDk0NzcxZDBlOGQ3OGRkMmQzOGRkNDgwYWIzMDZlMTA=
12
+ data.tar.gz: !binary |-
13
+ ODQ2ODczNWUxYTA2NjAxM2Q0MTRmM2E3YTE0M2U2MWYwNGRlOTE4ZTliODRm
14
+ YTUyNTFiYjgzODg2OTU4OTllOTVhMTA2OWEwZTk4ZGZlMjAxNzdlZmNiMDdh
15
+ NTI2ZDZmODkxMTg4ODVjZjUwODQ5YjY0YWE2MGQwZmQ2ODIxNTA=
@@ -0,0 +1,4 @@
1
+ puts '-------------------------------------'
2
+ require 'rails/multi_block_helpers/switch_block'
3
+ require 'rails/multi_block_helpers/capture_section_helper'
4
+ require 'rails/multi_block_helpers/railtie' if defined?(Rails)
@@ -0,0 +1,19 @@
1
+ module Rails
2
+ module MultiBlockHelpers
3
+ module CaptureSectionHelper
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ include SwitchBlock
8
+ end
9
+
10
+ def capture_section(section, *args)
11
+ value = nil
12
+ buffer = with_output_buffer { value = yield it_to(section) }
13
+ if string = buffer.presence || value and string.is_a?(String)
14
+ ERB::Util.html_escape string
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ module Rails::MultiBlockHelpers
2
+ class Railtie < Rails::Railtie
3
+ initializer 'multi_block_helpers.configure_rails_initialization' do
4
+ ActionView::Base.send :include, Rails::MultiBlockHelpers::CaptureSectionHelper
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,48 @@
1
+ # Courtesy of Roger Braun
2
+ # http://codebrawl.com/contests/methods-taking-multiple-blocks
3
+ # https://gist.github.com/rogerbraun/fae73df4b7063ab43c10
4
+
5
+ require 'blankslate'
6
+
7
+ module SwitchBlock
8
+
9
+ class NothingExecutedError < Exception;
10
+ end
11
+
12
+ class BlockSwitcher < BlankSlate
13
+
14
+ def initialize(x, args)
15
+ @x = x
16
+ @args = args
17
+ @ret = nil
18
+ @called = false
19
+ @callees = []
20
+ end
21
+
22
+ def ensure
23
+ raise NothingExecutedError, "Nothing got executed! Expected one of #{@callees.inspect}, but only got #{@x}" if not @called
24
+ end
25
+
26
+ def method_missing(method, *args, &block)
27
+ if (method.downcase == @x.to_sym.downcase or method == :always) or (@called == false and method == :else) then
28
+ @called = true
29
+ if args.size > 0 then
30
+ @ret = args.first.call(*@args)
31
+ else
32
+ @ret = block.call(*@args)
33
+ end
34
+ end
35
+ # For error reporting
36
+ @callees << method
37
+ @callees.uniq!
38
+ # Always return something meaningful
39
+ @ret
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def it_to(x, *args)
46
+ BlockSwitcher.new(x, args)
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ module Rails
2
+ module MultiBlockHelpers
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-multi_block_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - stevo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Gem allows defining helpers that accept multiple blocks
14
+ email:
15
+ - b.kosmowski@selleo.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/rails/multi_block_helpers.rb
21
+ - lib/rails/multi_block_helpers/capture_section_helper.rb
22
+ - lib/rails/multi_block_helpers/railtie.rb
23
+ - lib/rails/multi_block_helpers/switch_block.rb
24
+ - lib/rails/multi_block_helpers/version.rb
25
+ homepage: https://github.com/Selleo/rails-multi_block_helpers
26
+ licenses: []
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project: ! '[none]'
44
+ rubygems_version: 2.1.10
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Gem allows defining helpers that accept multiple blocks
48
+ test_files: []