hanlon 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/hanlon.rb +17 -2
- data/lib/hanlon/methods_finder.rb +21 -0
- data/lib/hanlon/template_loader.rb +12 -0
- data/lib/hanlon/version.rb +1 -1
- data/readme.md +23 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/templates/greetings.hanlon +5 -0
- data/spec/templates/math.hanlon +11 -0
- data/spec/unit/hanlon_spec.rb +21 -0
- data/spec/unit/methods_finder_spec.rb +10 -0
- metadata +12 -9
- data/lib/hanlon/engine.rb +0 -24
- data/lib/hanlon/loader.rb +0 -30
- data/spec/unit/engine_spec.rb +0 -17
- data/spec/unit/loader_spec.rb +0 -33
data/Gemfile.lock
CHANGED
data/lib/hanlon.rb
CHANGED
@@ -1,7 +1,22 @@
|
|
1
1
|
require 'haml'
|
2
2
|
require 'hanlon/version'
|
3
|
-
require 'hanlon/
|
4
|
-
require 'hanlon/
|
3
|
+
require 'hanlon/methods_finder'
|
4
|
+
require 'hanlon/template_loader'
|
5
5
|
|
6
6
|
module Hanlon
|
7
|
+
def self.included base
|
8
|
+
base.extend TemplateLoader
|
9
|
+
end
|
10
|
+
|
11
|
+
def execute_hanlon engine, name, *args
|
12
|
+
@method_being_searched_for = name
|
13
|
+
@arguments_for_method = args
|
14
|
+
engine.render self
|
15
|
+
end
|
16
|
+
|
17
|
+
def hanlon name, &block
|
18
|
+
(name.to_s == @method_being_searched_for) ?
|
19
|
+
block.call(*@arguments_for_method) :
|
20
|
+
''
|
21
|
+
end
|
7
22
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Hanlon
|
2
|
+
class MethodsFinder
|
3
|
+
def self.find engine
|
4
|
+
Hanlon::MethodsFinder.new(engine).methods
|
5
|
+
end
|
6
|
+
|
7
|
+
def initialize engine
|
8
|
+
@methods = []
|
9
|
+
@engine = engine
|
10
|
+
end
|
11
|
+
|
12
|
+
def hanlon name, &block
|
13
|
+
@methods.push name.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
def methods
|
17
|
+
@engine.render(self)
|
18
|
+
@methods
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/hanlon/version.rb
CHANGED
data/readme.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# hanlon: Rails helpers defined in haml
|
2
|
+
|
3
|
+
## Example
|
4
|
+
|
5
|
+
class RobGreeter
|
6
|
+
include Hanlon
|
7
|
+
load_template %q<
|
8
|
+
|
9
|
+
- hanlon :yell do |message|
|
10
|
+
%p= message.upcase
|
11
|
+
|
12
|
+
- hanlon :message do
|
13
|
+
%h1= yell 'Hey there, Rob'
|
14
|
+
%p= 'I wanted to let you know that I just adore your flaming, red mane. You remind me of a confident jungle cat, constantly on the prowl. Hit me up soon, tiger.'
|
15
|
+
>
|
16
|
+
end
|
17
|
+
|
18
|
+
puts RobGreeter.new.message
|
19
|
+
|
20
|
+
## Result
|
21
|
+
|
22
|
+
<h1><p>HEY THERE, ROB</p></h1>
|
23
|
+
<p>I wanted to let you know that I just adore your flaming, red mane. You remind me of a confident jungle cat, constantly on the prowl. Hit me up soon, tiger.</p>
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,11 @@
|
|
1
|
+
- hanlon :operation do |op, num_1, num_2, result|
|
2
|
+
%p
|
3
|
+
%span.operations> #{num_1} #{op} #{num_2} =
|
4
|
+
%span.result>= result
|
5
|
+
|
6
|
+
- hanlon :add do |num_1, num_2|
|
7
|
+
= operation '+', num_1, num_2, num_1 + num_2
|
8
|
+
|
9
|
+
- hanlon :substract do |num_1, num_2|
|
10
|
+
= operation '-', num_1, num_2, num_1 - num_2
|
11
|
+
|
data/spec/unit/hanlon_spec.rb
CHANGED
@@ -1,4 +1,25 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Hanlon do
|
4
|
+
before :all do
|
5
|
+
class RobHanlon
|
6
|
+
include Hanlon
|
7
|
+
load_template template('math.hanlon')
|
8
|
+
end
|
9
|
+
end
|
10
|
+
it 'should add some methods when included in a class' do
|
11
|
+
RobHanlon.should respond_to :load_template
|
12
|
+
RobHanlon.new.should respond_to :execute_hanlon
|
13
|
+
RobHanlon.new.should respond_to :hanlon
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should load hanlon methods' do
|
17
|
+
RobHanlon.new.should respond_to :substract
|
18
|
+
RobHanlon.new.should respond_to :add
|
19
|
+
RobHanlon.new.should respond_to :operation
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should execute the hanlon methods without error' do
|
23
|
+
RobHanlon.new.add(1, 4).should == "<p><span class='operations'>1 + 4 =</span><span class='result'>5</span></p>\n"
|
24
|
+
end
|
4
25
|
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hanlon::MethodsFinder do
|
4
|
+
describe "#find" do
|
5
|
+
it 'should return a list of methods in a template' do
|
6
|
+
engine = Haml::Engine.new template("math.hanlon")
|
7
|
+
Hanlon::MethodsFinder.find(engine).should =~ ['add', 'substract', 'operation']
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- cohitre
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-08-
|
17
|
+
date: 2011-08-08 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -59,13 +59,15 @@ files:
|
|
59
59
|
- Rakefile
|
60
60
|
- hanlon.gemspec
|
61
61
|
- lib/hanlon.rb
|
62
|
-
- lib/hanlon/
|
63
|
-
- lib/hanlon/
|
62
|
+
- lib/hanlon/methods_finder.rb
|
63
|
+
- lib/hanlon/template_loader.rb
|
64
64
|
- lib/hanlon/version.rb
|
65
|
+
- readme.md
|
65
66
|
- spec/spec_helper.rb
|
66
|
-
- spec/
|
67
|
+
- spec/templates/greetings.hanlon
|
68
|
+
- spec/templates/math.hanlon
|
67
69
|
- spec/unit/hanlon_spec.rb
|
68
|
-
- spec/unit/
|
70
|
+
- spec/unit/methods_finder_spec.rb
|
69
71
|
has_rdoc: true
|
70
72
|
homepage: ""
|
71
73
|
licenses: []
|
@@ -100,6 +102,7 @@ specification_version: 3
|
|
100
102
|
summary: Define rails helpers with Haml
|
101
103
|
test_files:
|
102
104
|
- spec/spec_helper.rb
|
103
|
-
- spec/
|
105
|
+
- spec/templates/greetings.hanlon
|
106
|
+
- spec/templates/math.hanlon
|
104
107
|
- spec/unit/hanlon_spec.rb
|
105
|
-
- spec/unit/
|
108
|
+
- spec/unit/methods_finder_spec.rb
|
data/lib/hanlon/engine.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
module Hanlon
|
2
|
-
class Engine
|
3
|
-
def initialize hanlon_text, options={}
|
4
|
-
@haml_engine = Haml::Engine.new hanlon_text, options
|
5
|
-
end
|
6
|
-
|
7
|
-
def module
|
8
|
-
@module ||= process
|
9
|
-
end
|
10
|
-
|
11
|
-
def process
|
12
|
-
loader = Hanlon::Loader.new
|
13
|
-
@haml_engine.render loader
|
14
|
-
@module = loader.build
|
15
|
-
end
|
16
|
-
|
17
|
-
def evaluate haml
|
18
|
-
klass = Class.new
|
19
|
-
klass.send :include, self.module
|
20
|
-
engine = Haml::Engine.new haml
|
21
|
-
engine.render klass.new
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
data/lib/hanlon/loader.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
module Hanlon
|
2
|
-
class Loader
|
3
|
-
def initialize
|
4
|
-
@loaded_methods = {}
|
5
|
-
end
|
6
|
-
|
7
|
-
def add_method name, &block
|
8
|
-
@loaded_methods[name] = block
|
9
|
-
end
|
10
|
-
|
11
|
-
def hanlon name, &block
|
12
|
-
add_method name do |*helper_args|
|
13
|
-
capture_haml *helper_args, &block
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def [] name
|
18
|
-
@loaded_methods[name]
|
19
|
-
end
|
20
|
-
|
21
|
-
def build
|
22
|
-
loaded_methods = @loaded_methods
|
23
|
-
Module.new do
|
24
|
-
loaded_methods.each_pair do |key, value|
|
25
|
-
define_method key, value
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
data/spec/unit/engine_spec.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Hanlon::Engine do
|
4
|
-
describe '#process' do
|
5
|
-
it 'should return a module with the defined methods' do
|
6
|
-
engine = Hanlon::Engine.new %q<
|
7
|
-
- hanlon :greeting do |name|
|
8
|
-
%p== hello #{name}.
|
9
|
-
- hanlon :headline do
|
10
|
-
%h1 Headline>
|
11
|
-
|
12
|
-
engine.module.should be_a(Module)
|
13
|
-
engine.evaluate("= greeting 'Carlos'").should == "<p>hello Carlos.</p>\n"
|
14
|
-
engine.evaluate("= headline").should == "<h1>Headline</h1>\n"
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
data/spec/unit/loader_spec.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Hanlon::Loader do
|
4
|
-
describe '#add_method' do
|
5
|
-
it 'should add a method to the methods hash' do
|
6
|
-
loader = Hanlon::Loader.new
|
7
|
-
loader.add_method :headline do
|
8
|
-
'headline'
|
9
|
-
end
|
10
|
-
loader[:headline].should be_a Proc
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
describe '#build' do
|
15
|
-
it 'should return a module with the defined methods' do
|
16
|
-
loader = Hanlon::Loader.new
|
17
|
-
loader.add_method :headline do
|
18
|
-
'headline'
|
19
|
-
end
|
20
|
-
loader.add_method :paragraph do
|
21
|
-
'bogus'
|
22
|
-
end
|
23
|
-
mod = loader.build
|
24
|
-
mod.should be_a Module
|
25
|
-
ModuleHelper = mod
|
26
|
-
class ModuleTester
|
27
|
-
include ModuleHelper
|
28
|
-
end
|
29
|
-
ModuleTester.new.paragraph.should == 'bogus'
|
30
|
-
ModuleTester.new.headline.should == 'headline'
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|