Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored.

  • Loading branch information...
commit 19c58629e43c784443a484126d83754aaca72bb5 1 parent df2aeb8
@cohitre authored
View
2  Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- hanlon (0.0.1)
+ hanlon (0.0.2)
haml
GEM
View
20 lib/hanlon.rb
@@ -1,8 +1,22 @@
require 'haml'
require 'hanlon/version'
-require 'hanlon/engine'
-require 'hanlon/loader'
-require 'hanlon/file'
+require 'hanlon/methods_finder'
+require 'hanlon/template_loader'
module Hanlon
+ def self.included base
+ base.extend TemplateLoader
+ end
+
+ def execute_hanlon engine, name, *args
+ @method_being_searched_for = name
+ @arguments_for_method = args
+ engine.render self
+ end
+
+ def hanlon name, &block
+ (name.to_s == @method_being_searched_for) ?
+ block.call(*@arguments_for_method) :
+ ''
+ end
end
View
24 lib/hanlon/engine.rb
@@ -1,24 +0,0 @@
-module Hanlon
- class Engine
- def initialize hanlon_text, options={}
- @haml_engine = Haml::Engine.new hanlon_text, options
- end
-
- def module
- @module ||= process
- end
-
- def process
- loader = Hanlon::Loader.new
- @haml_engine.render loader
- @module = loader.build
- end
-
- def evaluate haml
- klass = Class.new
- klass.send :include, self.module
- engine = Haml::Engine.new haml
- engine.render klass.new
- end
- end
-end
View
17 lib/hanlon/file.rb
@@ -1,17 +0,0 @@
-module Hanlon
- class File
- def self.load file
- f = self.new "#{file}.hanlon"
- f.module
- end
-
- def initialize filename
- @template = ::File.read(filename)
- @engine = Hanlon::Engine.new @template
- end
-
- def module
- @module ||= @engine.module
- end
- end
-end
View
30 lib/hanlon/loader.rb
@@ -1,30 +0,0 @@
-module Hanlon
- class Loader
- def initialize
- @loaded_methods = {}
- end
-
- def add_method name, &block
- @loaded_methods[name] = block
- end
-
- def hanlon name, &block
- add_method name do |*helper_args|
- capture_haml *helper_args, &block
- end
- end
-
- def [] name
- @loaded_methods[name]
- end
-
- def build
- loaded_methods = @loaded_methods
- Module.new do
- loaded_methods.each_pair do |key, value|
- define_method key, value
- end
- end
- end
- end
-end
View
21 lib/hanlon/methods_finder.rb
@@ -0,0 +1,21 @@
+module Hanlon
+ class MethodsFinder
+ def self.find engine
+ Hanlon::MethodsFinder.new(engine).methods
+ end
+
+ def initialize engine
+ @methods = []
+ @engine = engine
+ end
+
+ def hanlon name, &block
+ @methods.push name.to_s
+ end
+
+ def methods
+ @engine.render(self)
+ @methods
+ end
+ end
+end
View
12 lib/hanlon/template_loader.rb
@@ -0,0 +1,12 @@
+module Hanlon
+ module TemplateLoader
+ def load_template template
+ engine = Haml::Engine.new template
+ MethodsFinder.find(engine).each do |name|
+ define_method name do |*args|
+ execute_hanlon engine, name, *args
+ end
+ end
+ end
+ end
+end
View
2  lib/hanlon/version.rb
@@ -1,3 +1,3 @@
module Hanlon
- VERSION = "0.0.1"
+ VERSION = "0.0.2"
end
View
4 spec/spec_helper.rb
@@ -13,3 +13,7 @@
end
require 'hanlon'
+
+def template temp
+ File.read("#{File.dirname __FILE__}/templates/#{temp}")
+end
View
11 spec/templates/math.hanlon
@@ -0,0 +1,11 @@
+- hanlon :operation do |op, num_1, num_2, result|
+ %p
+ %span.operations> #{num_1} #{op} #{num_2} =
+ %span.result>= result
+
+- hanlon :add do |num_1, num_2|
+ = operation '+', num_1, num_2, num_1 + num_2
+
+- hanlon :substract do |num_1, num_2|
+ = operation '-', num_1, num_2, num_1 - num_2
+
View
17 spec/unit/engine_spec.rb
@@ -1,17 +0,0 @@
-require 'spec_helper'
-
-describe Hanlon::Engine do
- describe '#process' do
- it 'should return a module with the defined methods' do
- engine = Hanlon::Engine.new %q<
-- hanlon :greeting do |name|
- %p== hello #{name}.
-- hanlon :headline do
- %h1 Headline>
-
- engine.module.should be_a(Module)
- engine.evaluate("= greeting 'Carlos'").should == "<p>hello Carlos.</p>\n"
- engine.evaluate("= headline").should == "<h1>Headline</h1>\n"
- end
- end
-end
View
10 spec/unit/file_spec.rb
@@ -1,10 +0,0 @@
-require 'spec_helper'
-
-describe Hanlon::File do
- describe '#load' do
- it 'should load a file and return a module' do
- mod = Hanlon::File.load("#{File.dirname(__FILE__)}/../templates/greetings")
- mod.should be_a Module
- end
- end
-end
View
21 spec/unit/hanlon_spec.rb
@@ -1,4 +1,25 @@
require 'spec_helper'
describe Hanlon do
+ before :all do
+ class RobHanlon
+ include Hanlon
+ load_template template('math.hanlon')
+ end
+ end
+ it 'should add some methods when included in a class' do
+ RobHanlon.should respond_to :load_template
+ RobHanlon.new.should respond_to :execute_hanlon
+ RobHanlon.new.should respond_to :hanlon
+ end
+
+ it 'should load hanlon methods' do
+ RobHanlon.new.should respond_to :substract
+ RobHanlon.new.should respond_to :add
+ RobHanlon.new.should respond_to :operation
+ end
+
+ it 'should execute the hanlon methods without error' do
+ RobHanlon.new.add(1, 4).should == "<p><span class='operations'>1 + 4 =</span><span class='result'>5</span></p>\n"
+ end
end
View
33 spec/unit/loader_spec.rb
@@ -1,33 +0,0 @@
-require 'spec_helper'
-
-describe Hanlon::Loader do
- describe '#add_method' do
- it 'should add a method to the methods hash' do
- loader = Hanlon::Loader.new
- loader.add_method :headline do
- 'headline'
- end
- loader[:headline].should be_a Proc
- end
- end
-
- describe '#build' do
- it 'should return a module with the defined methods' do
- loader = Hanlon::Loader.new
- loader.add_method :headline do
- 'headline'
- end
- loader.add_method :paragraph do
- 'bogus'
- end
- mod = loader.build
- mod.should be_a Module
- ModuleHelper = mod
- class ModuleTester
- include ModuleHelper
- end
- ModuleTester.new.paragraph.should == 'bogus'
- ModuleTester.new.headline.should == 'headline'
- end
- end
-end
View
10 spec/unit/methods_finder_spec.rb
@@ -0,0 +1,10 @@
+require 'spec_helper'
+
+describe Hanlon::MethodsFinder do
+ describe "#find" do
+ it 'should return a list of methods in a template' do
+ engine = Haml::Engine.new template("math.hanlon")
+ Hanlon::MethodsFinder.find(engine).should =~ ['add', 'substract', 'operation']
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.