easyrb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/easyrb.rb +4 -0
- data/lib/easyrb/context.rb +64 -0
- data/lib/easyrb/local.rb +66 -0
- data/lib/easyrb/monkey.rb +19 -0
- data/lib/easyrb/runner.rb +60 -0
- metadata +50 -0
data/lib/easyrb.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
module Easyrb
|
2
|
+
|
3
|
+
class Context
|
4
|
+
|
5
|
+
################
|
6
|
+
# #
|
7
|
+
# Declarations #
|
8
|
+
# #
|
9
|
+
################
|
10
|
+
|
11
|
+
attr_reader :helpers
|
12
|
+
|
13
|
+
###############
|
14
|
+
# #
|
15
|
+
# Constructor #
|
16
|
+
# #
|
17
|
+
###############
|
18
|
+
|
19
|
+
def initialize(context, helpers)
|
20
|
+
@context = context
|
21
|
+
@helpers = Array(helpers)
|
22
|
+
end
|
23
|
+
|
24
|
+
#################
|
25
|
+
# #
|
26
|
+
# Class Methods #
|
27
|
+
# #
|
28
|
+
#################
|
29
|
+
|
30
|
+
def self.[](context, helpers)
|
31
|
+
new(context, helpers).generate_context
|
32
|
+
end
|
33
|
+
|
34
|
+
####################
|
35
|
+
# #
|
36
|
+
# Instance Methods #
|
37
|
+
# #
|
38
|
+
####################
|
39
|
+
|
40
|
+
def generate_context
|
41
|
+
context.tap do |obj|
|
42
|
+
helpers.each do |mod|
|
43
|
+
obj.extend(mod)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def bare_context
|
51
|
+
@context
|
52
|
+
end
|
53
|
+
|
54
|
+
def context
|
55
|
+
delegated_context || bare_context || Object.new
|
56
|
+
end
|
57
|
+
|
58
|
+
def delegated_context
|
59
|
+
SimpleDelegator.new(bare_context) if bare_context && helpers.any?
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
data/lib/easyrb/local.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
module Easyrb
|
2
|
+
|
3
|
+
class Local
|
4
|
+
|
5
|
+
################
|
6
|
+
# #
|
7
|
+
# Declarations #
|
8
|
+
# #
|
9
|
+
################
|
10
|
+
|
11
|
+
attr_reader :context, :hash
|
12
|
+
|
13
|
+
###############
|
14
|
+
# #
|
15
|
+
# Constructor #
|
16
|
+
# #
|
17
|
+
###############
|
18
|
+
|
19
|
+
def initialize(context, hash)
|
20
|
+
@context = context
|
21
|
+
@hash = hash || Hash.new
|
22
|
+
end
|
23
|
+
|
24
|
+
#################
|
25
|
+
# #
|
26
|
+
# Class Methods #
|
27
|
+
# #
|
28
|
+
#################
|
29
|
+
|
30
|
+
def self.[](context, hash)
|
31
|
+
new(context, hash).generate_binding
|
32
|
+
end
|
33
|
+
|
34
|
+
####################
|
35
|
+
# #
|
36
|
+
# Instance Methods #
|
37
|
+
# #
|
38
|
+
####################
|
39
|
+
|
40
|
+
def generate_binding
|
41
|
+
locals_function.(*values)
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def function_string
|
47
|
+
locals_string = "#{ keys.join(', ') }, = args;" if keys.any?
|
48
|
+
|
49
|
+
"->(*args) { #{ locals_string } ->(){}.binding }"
|
50
|
+
end
|
51
|
+
|
52
|
+
def keys
|
53
|
+
hash.keys
|
54
|
+
end
|
55
|
+
|
56
|
+
def locals_function
|
57
|
+
context.instance_eval(function_string)
|
58
|
+
end
|
59
|
+
|
60
|
+
def values
|
61
|
+
hash.values
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class String
|
2
|
+
|
3
|
+
def erb(options = {})
|
4
|
+
Easyrb::Runner.new(self).run(options)
|
5
|
+
end
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
class File
|
10
|
+
|
11
|
+
def self.erb(filename, options = {})
|
12
|
+
File.read(filename).erb(options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def erb(options = {})
|
16
|
+
self.read.erb(options)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'delegate'
|
3
|
+
|
4
|
+
module Easyrb
|
5
|
+
|
6
|
+
class Runner
|
7
|
+
|
8
|
+
################
|
9
|
+
# #
|
10
|
+
# Declarations #
|
11
|
+
# #
|
12
|
+
################
|
13
|
+
|
14
|
+
attr_reader :erb_template
|
15
|
+
|
16
|
+
VALID_OPTIONS = [:context, :helpers, :locals]
|
17
|
+
|
18
|
+
###############
|
19
|
+
# #
|
20
|
+
# Constructor #
|
21
|
+
# #
|
22
|
+
###############
|
23
|
+
|
24
|
+
def initialize(erb_template)
|
25
|
+
@erb_template = erb_template
|
26
|
+
end
|
27
|
+
|
28
|
+
####################
|
29
|
+
# #
|
30
|
+
# Instance Methods #
|
31
|
+
# #
|
32
|
+
####################
|
33
|
+
|
34
|
+
def run(options = {})
|
35
|
+
validate_options!(options)
|
36
|
+
|
37
|
+
object = Easyrb::Context[options[:context], options[:helpers]]
|
38
|
+
binding = Easyrb::Local[object, options[:locals]]
|
39
|
+
|
40
|
+
erb.result(binding)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def erb
|
46
|
+
ERB.new(erb_template)
|
47
|
+
end
|
48
|
+
|
49
|
+
def validate_options!(options)
|
50
|
+
bad_keys = options.keys - VALID_OPTIONS
|
51
|
+
|
52
|
+
if bad_keys.any?
|
53
|
+
bad_key_string = bad_keys.map { |k| k.inspect }.join(', ')
|
54
|
+
raise ArgumentError, "#erb invalid keys: #{ bad_key_string }"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easyrb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mark Josef
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-09-21 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Making it easy to use ERB in a Rails-y fashion
|
15
|
+
email: mcphage@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/easyrb.rb
|
21
|
+
- lib/easyrb/context.rb
|
22
|
+
- lib/easyrb/local.rb
|
23
|
+
- lib/easyrb/monkey.rb
|
24
|
+
- lib/easyrb/runner.rb
|
25
|
+
homepage: http://github.com/mark/easyrb
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 1.8.10
|
47
|
+
signing_key:
|
48
|
+
specification_version: 3
|
49
|
+
summary: Making it easy to use ERB in a Rails-y fashion
|
50
|
+
test_files: []
|