erbb 0.1.0
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/.gitignore +11 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +14 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +71 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/erbb.gemspec +38 -0
- data/lib/erbb.rb +13 -0
- data/lib/erbb/VERSION +1 -0
- data/lib/erbb/parser.rb +46 -0
- data/lib/erbb/receiver.rb +44 -0
- data/lib/erbb/result.rb +18 -0
- data/lib/erbb/version.rb +3 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 50c49f3ca3586eeee28bf7a2b58371d1fbb8b82996dc41fdd8902c45e25a8233
|
4
|
+
data.tar.gz: 83d9b2ec7976df8a40e27114076a843b28b9fff1e0cfaf9a319d677c000f11d7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ad9a81aacdbf35111ef533bd91d69f5990bb38f5ba42d3ae0449f708e7ff0da5b0f72d52addb4d13faafd0082022c5d848e45f22098a3ba9a140c9da3c6709d3
|
7
|
+
data.tar.gz: f1e5a732eef491b53870f9de8dfa457b36e4a9c04ac2ec818d06858b886e9055e933637628af7a662eae2edd2b2f80054fdaf4a462bc48ee05749bf05f067768
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.5.1
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
erbb (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.8.0)
|
12
|
+
rspec-core (~> 3.8.0)
|
13
|
+
rspec-expectations (~> 3.8.0)
|
14
|
+
rspec-mocks (~> 3.8.0)
|
15
|
+
rspec-core (3.8.0)
|
16
|
+
rspec-support (~> 3.8.0)
|
17
|
+
rspec-expectations (3.8.1)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.8.0)
|
20
|
+
rspec-mocks (3.8.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.8.0)
|
23
|
+
rspec-support (3.8.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.16)
|
30
|
+
erbb!
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.16.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Josh Lauer
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
[](https://travis-ci.com/josh-lauer/erbb)
|
2
|
+
|
3
|
+
# ERBB
|
4
|
+
|
5
|
+
`ERBB` is `ERB`, with the additional ability to name blocks and save the output of rendering those blocks separately. It works exactly like `ERB`, with some additional methods.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'erbb'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install erbb
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
`ERBB` is built on top of `ERB`. It implements a `#named_block` method in templates which tells the parser to save the rendered contents of the block. In every other way, `ERBB` is used the same way as `ERB`.
|
26
|
+
|
27
|
+
First, set up your erb template and instantiate.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
template = <<~TEMPLATE
|
31
|
+
some text
|
32
|
+
<% named_block :foo do %>
|
33
|
+
some text in the named block
|
34
|
+
<% end %>
|
35
|
+
some more text
|
36
|
+
TEMPLATE
|
37
|
+
|
38
|
+
erbb = ERBB.new(template)
|
39
|
+
```
|
40
|
+
Then call `#result` to get the rendered output
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
result = erbb.result(binding)
|
44
|
+
=> "some text\nsome text in the named block\nsome more text\n"
|
45
|
+
```
|
46
|
+
The result is a string-like object that has a `#named_blocks` method that returns the rendered contents of all the named blocks in the template.
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
result.named_blocks
|
50
|
+
=> {:foo=>"some text in the named block\n"}
|
51
|
+
```
|
52
|
+
|
53
|
+
Also, for convenience, the result delegates method calls to `#named_blocks` if they don't collide with String methods
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
result.foo
|
57
|
+
=> "some text in the named block\n"
|
58
|
+
```
|
59
|
+
That's pretty much it. Use it like `ERB`.
|
60
|
+
|
61
|
+
## Development
|
62
|
+
|
63
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/josh-lauer/erbb.
|
68
|
+
|
69
|
+
## License
|
70
|
+
|
71
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "erbb"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/erbb.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "erbb/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "erbb"
|
8
|
+
spec.version = ERBB::VERSION
|
9
|
+
spec.authors = ["Josh Lauer", "Jeremiah Hemphill", "Seth Brogan"]
|
10
|
+
spec.email = ["josh.lauer75@gmail.com", "jeremiah.hemphill@gmail.com", "brogan.seth@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{ERB with Blocks}
|
13
|
+
spec.description = %q{ERB with Blocks}
|
14
|
+
spec.homepage = "https://github.com/josh-lauer/erbb"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# # to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
# "public gem pushes."
|
24
|
+
# end
|
25
|
+
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
28
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
29
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
30
|
+
end
|
31
|
+
spec.bindir = "exe"
|
32
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
36
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
37
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
38
|
+
end
|
data/lib/erbb.rb
ADDED
data/lib/erbb/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/erbb/parser.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module ERBB
|
2
|
+
# Parses ERBB files (erb files with #named_block calls in them). Use like you
|
3
|
+
# would the ERB class, except instead of passing a binding when getting the
|
4
|
+
# result, you pass in an object which will be the implicit receiver for all
|
5
|
+
# ruby calls made in the erb template. This object will be decorated with the
|
6
|
+
# instance methods in ERBB::Receiver when rendering.
|
7
|
+
class Parser < ERB
|
8
|
+
# @see https://ruby-doc.org/stdlib-2.5.1/libdoc/erb/rdoc/ERB.html#method-c-new
|
9
|
+
# @param str [String] the template to render.
|
10
|
+
# @param safe_level [Nil,Integer] the safe level.
|
11
|
+
# @param trim_mode [Nil,String] how to handle newlines.
|
12
|
+
# @param eoutvar [Nil] if the user passes a 4th arg, error
|
13
|
+
def initialize(str, safe_level=nil, trim_mode='>', eoutvar=nil)
|
14
|
+
# The fourth arg is the name of the variable defined on the receiver
|
15
|
+
# which is used to store the rendered output when parsing a template.
|
16
|
+
# ERBB works by exploiting that, so it has to control it.
|
17
|
+
raise ArgumentError, "In ERBB, the template ivar can’t be set" if eoutvar
|
18
|
+
super(str, safe_level, trim_mode, Receiver::RENDERED_TEMPLATE)
|
19
|
+
end
|
20
|
+
|
21
|
+
# @see https://ruby-doc.org/stdlib-2.5.1/libdoc/erb/rdoc/ERB.html#method-i-result
|
22
|
+
# @param b [Binding] the binding to use for rendering.
|
23
|
+
# @return [ERBB::Result] a string decorated with named blocks
|
24
|
+
def result(b=new_toplevel)
|
25
|
+
receiver = extract_receiver_from_binding(b)
|
26
|
+
|
27
|
+
# Render the template using the binding and return the result along with
|
28
|
+
# the output of any named blocks.
|
29
|
+
ERBB::Result.new(
|
30
|
+
super(b),
|
31
|
+
receiver.named_blocks
|
32
|
+
)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
# @param b [Binding] the binding to use to find its implicit receiver.
|
38
|
+
def extract_receiver_from_binding(b)
|
39
|
+
# yoink 'self' out of the binding context.
|
40
|
+
b.send(:eval, "self").tap do |receiver|
|
41
|
+
# decorate it before returning to so erbb methods work in templates.
|
42
|
+
receiver.instance_exec { extend ERBB::Receiver }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module ERBB
|
2
|
+
# Used to decorate the binding receiver with methods we want available in the
|
3
|
+
# template that are not defined on the binding receiver.
|
4
|
+
module Receiver
|
5
|
+
# The name of the ivar to use for storing the rendered template output
|
6
|
+
RENDERED_TEMPLATE = '@_erbb_out'.freeze
|
7
|
+
|
8
|
+
# When called within a template, defines a named_block. Repeated calls to
|
9
|
+
# a block with the same name within a template will be concatenated in the
|
10
|
+
# order in which they appear in the template.
|
11
|
+
def named_block(block_name, *args, &block)
|
12
|
+
# dup the output so far and save it
|
13
|
+
original_output = __erbb_get_rendered_template.dup
|
14
|
+
|
15
|
+
# wipe the template output ivar
|
16
|
+
__erbb_set_rendered_template("")
|
17
|
+
|
18
|
+
# render the block, populating the ivar with the result
|
19
|
+
yield(*args)
|
20
|
+
|
21
|
+
# concatenate the rendered output from the named block into the hash
|
22
|
+
named_blocks[block_name.to_sym] ||= ""
|
23
|
+
named_blocks[block_name.to_sym] << __erbb_get_rendered_template
|
24
|
+
|
25
|
+
# restore the template output ivar to its original value plus this block
|
26
|
+
__erbb_set_rendered_template(original_output + __erbb_get_rendered_template)
|
27
|
+
end
|
28
|
+
|
29
|
+
# @return [Hash] The blocks
|
30
|
+
def named_blocks
|
31
|
+
@_erbb_named_blocks ||= {}
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def __erbb_get_rendered_template
|
37
|
+
instance_variable_get(RENDERED_TEMPLATE)
|
38
|
+
end
|
39
|
+
|
40
|
+
def __erbb_set_rendered_template(str)
|
41
|
+
instance_variable_set(RENDERED_TEMPLATE, str)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/erbb/result.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module ERBB
|
2
|
+
class Result < String
|
3
|
+
attr_reader :named_blocks
|
4
|
+
|
5
|
+
def method_missing(method_name)
|
6
|
+
if named_blocks.key?(method_name)
|
7
|
+
named_blocks[method_name]
|
8
|
+
else
|
9
|
+
super
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(str, named_blocks, opts = {})
|
14
|
+
super(str, *opts)
|
15
|
+
@named_blocks = Hash[named_blocks.map { |k,v| [k.to_sym, v] }]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/erbb/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: erbb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Josh Lauer
|
8
|
+
- Jeremiah Hemphill
|
9
|
+
- Seth Brogan
|
10
|
+
autorequire:
|
11
|
+
bindir: exe
|
12
|
+
cert_chain: []
|
13
|
+
date: 2018-10-12 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bundler
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - "~>"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.16'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - "~>"
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '1.16'
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: rake
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - "~>"
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '10.0'
|
36
|
+
type: :development
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '10.0'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: rspec
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '3.0'
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - "~>"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '3.0'
|
57
|
+
description: ERB with Blocks
|
58
|
+
email:
|
59
|
+
- josh.lauer75@gmail.com
|
60
|
+
- jeremiah.hemphill@gmail.com
|
61
|
+
- brogan.seth@gmail.com
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- ".gitignore"
|
67
|
+
- ".rspec"
|
68
|
+
- ".ruby-version"
|
69
|
+
- ".travis.yml"
|
70
|
+
- Gemfile
|
71
|
+
- Gemfile.lock
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- bin/console
|
76
|
+
- bin/setup
|
77
|
+
- erbb.gemspec
|
78
|
+
- lib/erbb.rb
|
79
|
+
- lib/erbb/VERSION
|
80
|
+
- lib/erbb/parser.rb
|
81
|
+
- lib/erbb/receiver.rb
|
82
|
+
- lib/erbb/result.rb
|
83
|
+
- lib/erbb/version.rb
|
84
|
+
homepage: https://github.com/josh-lauer/erbb
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata: {}
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.7.7
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: ERB with Blocks
|
108
|
+
test_files: []
|