erb_sandbox 0.0.1
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 +1 -0
- data/.rubocop.yml +23 -0
- data/erb_sandbox.gemspec +18 -0
- data/lib/erb_sandbox.rb +40 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 46d74516bb4e1f4a7bc8f2efffca222574b08c49
|
4
|
+
data.tar.gz: 2214f9c1c879a8674cd004972ab487e725cf96e4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0a899b05b68bf4ec9c6a15082c157ff7bbb2e7a3234af8382c3485ff678bcf2bb969a70cbfc7562215ba78423a398086d9420cebe082fa1a071c43405155c97c
|
7
|
+
data.tar.gz: ea9f09eea725328c770fc35449153fd4353cbecbc34d6a64fcb64718ce3f896d3c2e16bcbb818fa3a668f1efaef711809ad0ddcc131912247d452915b6dc341a
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Metrics/LineLength:
|
2
|
+
Max: 120
|
3
|
+
|
4
|
+
Style/FileName:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
Metrics/CyclomaticComplexity:
|
8
|
+
Enabled: false
|
9
|
+
|
10
|
+
Metrics/MethodLength:
|
11
|
+
Max: 50
|
12
|
+
|
13
|
+
Metrics/PerceivedComplexity:
|
14
|
+
Enabled: false
|
15
|
+
|
16
|
+
Metrics/ClassLength:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Metrics/AbcSize: # Assignment Branch Condition size
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Metrics/ModuleLength:
|
23
|
+
Enabled: false
|
data/erb_sandbox.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'erb_sandbox'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2015-12-03'
|
5
|
+
s.summary = 'ERB Sandbox'
|
6
|
+
s.description = 'Allows you to render ERB templates in sandbox with predefined vars'
|
7
|
+
s.authors = ['Non Dmitriy']
|
8
|
+
s.email = 'non.dmitriy@gmail.com'
|
9
|
+
s.files = `git ls-files`.split("\n")
|
10
|
+
s.homepage = 'https://github.com/Nondv/erb_sandbox'
|
11
|
+
s.license = 'MIT'
|
12
|
+
# s.test_files = `git ls-files -- spec/*`.split("\n")
|
13
|
+
# s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
14
|
+
s.require_paths = ['lib']
|
15
|
+
|
16
|
+
s.required_ruby_version = '>= 2.0.0'
|
17
|
+
s.add_development_dependency 'rubocop', '~> 0.35'
|
18
|
+
end
|
data/lib/erb_sandbox.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
|
3
|
+
#
|
4
|
+
# Module contains methods for rendering ERB templates in "sandbox"
|
5
|
+
# so code in template will not be able to use your environment (methods, classes, ...)
|
6
|
+
# It's useful when you want to encapsulate your program from ERB (e.g. encapsulate models)
|
7
|
+
#
|
8
|
+
module ErbSandbox
|
9
|
+
#
|
10
|
+
# uses `erb` (yes-yes, it's slow:( ) to render template with some predefined variables
|
11
|
+
# @param template [String]
|
12
|
+
# @param variables [Hash]
|
13
|
+
# @return [String] result of using `erb`
|
14
|
+
#
|
15
|
+
def self.render(template, variables = {})
|
16
|
+
file = Tempfile.new 'erb_sandbox'
|
17
|
+
file.write variables_init_code variables
|
18
|
+
file.write template
|
19
|
+
file.close
|
20
|
+
result = `erb #{file.path}`
|
21
|
+
file.unlink
|
22
|
+
result
|
23
|
+
rescue
|
24
|
+
# TODO: do something useful
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
#
|
31
|
+
# generates code, that will define vars with values.
|
32
|
+
# @param variables [Hash] var_name => value
|
33
|
+
# @return [String] erb code (in <% %>)
|
34
|
+
#
|
35
|
+
def self.variables_init_code(variables)
|
36
|
+
# TODO: validate values
|
37
|
+
variables = variables.map { |key, value| "#{key} = #{value.inspect}" }
|
38
|
+
"<%\n#{variables.join "\n"}\n%>"
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: erb_sandbox
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Non Dmitriy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rubocop
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.35'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.35'
|
27
|
+
description: Allows you to render ERB templates in sandbox with predefined vars
|
28
|
+
email: non.dmitriy@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- ".gitignore"
|
34
|
+
- ".rubocop.yml"
|
35
|
+
- erb_sandbox.gemspec
|
36
|
+
- lib/erb_sandbox.rb
|
37
|
+
homepage: https://github.com/Nondv/erb_sandbox
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 2.0.0
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 2.4.6
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: ERB Sandbox
|
61
|
+
test_files: []
|
62
|
+
has_rdoc:
|