erb_sandbox 0.0.1 → 0.0.2
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 +4 -4
- data/LICENSE.txt +25 -0
- data/README.md +49 -0
- data/erb_sandbox.gemspec +4 -4
- data/lib/erb_sandbox/exceptions.rb +6 -0
- data/lib/erb_sandbox.rb +27 -8
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5a17355b42bb940a844b091c3153ab90e978d20
|
4
|
+
data.tar.gz: f1e0b695e0d31d539cdb20665259aa90073697b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2461110f8d61b154b9c51732a80cfd08a88c680d4fd4be5b3b2b34d8e8b548029f4acf3d41a152462ce46999c4b3650a700e17504c7c725c1d95052981cc088e
|
7
|
+
data.tar.gz: 331decddf5e4906777361a187aa22ad4aa6101af6f238c2646399ce50b210c33e167887bb69a4ea3da8a3f78af393fce0a25920434ec74632c44e975b1cdb018
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Copyright (c) 2015 Dmitriy Non
|
2
|
+
|
3
|
+
|
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
|
+
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be included in
|
15
|
+
all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
25
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# ERB Sandbox v0.0.1
|
2
|
+
ERB Sandbox allows you to encapsulate your program from ERB.
|
3
|
+
|
4
|
+
### Problem
|
5
|
+
|
6
|
+
When you use `ERB.new(code).result` "code" can use all parts of your program.
|
7
|
+
For example:
|
8
|
+
```Ruby
|
9
|
+
class ImportantClass
|
10
|
+
def self.nuke_world
|
11
|
+
launch_rockets
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
ERB.new('<% ImportantClass.nuke_world %>').result
|
16
|
+
```
|
17
|
+
|
18
|
+
World is being **bombed**!!! It makes me cry:(
|
19
|
+
|
20
|
+
### Solution
|
21
|
+
|
22
|
+
So, I just wanna render some template with some predefined variables.
|
23
|
+
|
24
|
+
With `erb_sandbox` gem I can do:
|
25
|
+
```Ruby
|
26
|
+
class ImportantClass
|
27
|
+
def self.nuke_world
|
28
|
+
launch_rockets
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
begin
|
33
|
+
ErbSandbox.render('<% ImportantClass.nuke_world %>')
|
34
|
+
laugh # it will never happen
|
35
|
+
rescue
|
36
|
+
# HA-HA-HA ImportantClass is undefined!
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
It's pretty simple:
|
43
|
+
|
44
|
+
```Ruby
|
45
|
+
ErbSandbox.render 'Hello, <%=user_name%>', user_name: 'dude' # => 'Hello, dude'
|
46
|
+
```
|
47
|
+
|
48
|
+
# Copyright
|
49
|
+
Copyright (c) 2015 Dmitriy Non. See [LICENSE.txt](LICENSE.txt)
|
data/erb_sandbox.gemspec
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'erb_sandbox'
|
3
|
-
s.version = '0.0.
|
4
|
-
s.date = '2015-12-
|
3
|
+
s.version = '0.0.2'
|
4
|
+
s.date = '2015-12-04'
|
5
5
|
s.summary = 'ERB Sandbox'
|
6
|
-
s.description = '
|
7
|
-
s.authors = ['Non
|
6
|
+
s.description = 'Gem allows you to render ERB templates in sandbox with predefined vars'
|
7
|
+
s.authors = ['Dmitriy Non']
|
8
8
|
s.email = 'non.dmitriy@gmail.com'
|
9
9
|
s.files = `git ls-files`.split("\n")
|
10
10
|
s.homepage = 'https://github.com/Nondv/erb_sandbox'
|
data/lib/erb_sandbox.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require 'tempfile'
|
2
|
+
require 'open3'
|
3
|
+
require_relative 'erb_sandbox/exceptions'
|
2
4
|
|
3
5
|
#
|
4
6
|
# Module contains methods for rendering ERB templates in "sandbox"
|
@@ -8,21 +10,34 @@ require 'tempfile'
|
|
8
10
|
module ErbSandbox
|
9
11
|
#
|
10
12
|
# uses `erb` (yes-yes, it's slow:( ) to render template with some predefined variables
|
13
|
+
# example:
|
14
|
+
# ErbSandbox.render 'Hello, <%= user %>', user: 'dude' # => "Hello, dude"
|
15
|
+
#
|
11
16
|
# @param template [String]
|
12
17
|
# @param variables [Hash]
|
13
18
|
# @return [String] result of using `erb`
|
19
|
+
# @raise [ErbSandbox::StatusIsNotZero]
|
14
20
|
#
|
15
21
|
def self.render(template, variables = {})
|
16
22
|
file = Tempfile.new 'erb_sandbox'
|
17
23
|
file.write variables_init_code variables
|
18
24
|
file.write template
|
19
25
|
file.close
|
20
|
-
|
26
|
+
|
27
|
+
output, status = Open3.capture2e "erb #{file.path}" # supress stderr
|
21
28
|
file.unlink
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
29
|
+
|
30
|
+
fail(StatusIsNotZero, output) unless status.to_i.zero?
|
31
|
+
output
|
32
|
+
end
|
33
|
+
|
34
|
+
#
|
35
|
+
# wrapping on ErbSandbox.render(File.read(path), variables)
|
36
|
+
# @raise [ErbSandbox::StderrIsNotEmpty] from ErbSandbox.render
|
37
|
+
# @raise [Exceptions] from File.read
|
38
|
+
#
|
39
|
+
def self.render_file(path, variables = {})
|
40
|
+
render File.read(path), variables
|
26
41
|
end
|
27
42
|
|
28
43
|
private
|
@@ -31,10 +46,14 @@ module ErbSandbox
|
|
31
46
|
# generates code, that will define vars with values.
|
32
47
|
# @param variables [Hash] var_name => value
|
33
48
|
# @return [String] erb code (in <% %>)
|
49
|
+
# @raise [TypeError] from Marshal.dump
|
34
50
|
#
|
35
51
|
def self.variables_init_code(variables)
|
36
|
-
|
37
|
-
|
38
|
-
|
52
|
+
lines = variables.map do |key, value|
|
53
|
+
marshal_string = Marshal.dump value
|
54
|
+
"#{key} = Marshal.load(#{marshal_string.inspect})" # inspect - like escape string
|
55
|
+
end
|
56
|
+
|
57
|
+
"<%\n#{lines.join "\n"}\n%>"
|
39
58
|
end
|
40
59
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: erb_sandbox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Non
|
7
|
+
- Dmitriy Non
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0.35'
|
27
|
-
description:
|
27
|
+
description: Gem allows you to render ERB templates in sandbox with predefined vars
|
28
28
|
email: non.dmitriy@gmail.com
|
29
29
|
executables: []
|
30
30
|
extensions: []
|
@@ -32,8 +32,11 @@ extra_rdoc_files: []
|
|
32
32
|
files:
|
33
33
|
- ".gitignore"
|
34
34
|
- ".rubocop.yml"
|
35
|
+
- LICENSE.txt
|
36
|
+
- README.md
|
35
37
|
- erb_sandbox.gemspec
|
36
38
|
- lib/erb_sandbox.rb
|
39
|
+
- lib/erb_sandbox/exceptions.rb
|
37
40
|
homepage: https://github.com/Nondv/erb_sandbox
|
38
41
|
licenses:
|
39
42
|
- MIT
|