Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add boolean helpers #21

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/ruby-handlebars/helpers/default_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,20 @@ def self.register(hbs)
# # Do things and stuffa, but with 'as |param| notation'
# end
end

class BooleanHelper < DefaultHelper
# Helper that simply return "true" or ""

def self.apply(context, item1, item2, block = nil, else_block = nil)
self.cmp(item1, item2) ? 'true' : ''
rescue Exception => err
''
end

# To be implemented by sub-classes
# def self.cmp(item1, item2)
# do a boolean comparison on item1 and item2
# end
end
end
end
15 changes: 15 additions & 0 deletions lib/ruby-handlebars/helpers/eq_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require_relative 'default_helper'

module Handlebars
module Helpers
class EqHelper < BooleanHelper
def self.registry_name
'eq'
end

def self.cmp(item1, item2)
item1 == item2
end
end
end
end
15 changes: 15 additions & 0 deletions lib/ruby-handlebars/helpers/gt_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require_relative 'default_helper'

module Handlebars
module Helpers
class GtHelper < BooleanHelper
def self.registry_name
'gt'
end

def self.cmp(item1, item2)
item1 > item2
end
end
end
end
15 changes: 15 additions & 0 deletions lib/ruby-handlebars/helpers/gte_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require_relative 'default_helper'

module Handlebars
module Helpers
class GteHelper < BooleanHelper
def self.registry_name
'gte'
end

def self.cmp(item1, item2)
item1 >= item2
end
end
end
end
6 changes: 6 additions & 0 deletions lib/ruby-handlebars/helpers/register_default_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require_relative 'each_helper'
require_relative 'eq_helper'
require_relative 'gt_helper'
require_relative 'gte_helper'
require_relative 'helper_missing_helper'
require_relative 'if_helper'
require_relative 'unless_helper'
Expand All @@ -7,6 +10,9 @@ module Handlebars
module Helpers
def self.register_default_helpers(hbs)
EachHelper.register(hbs)
EqHelper.register(hbs)
GtHelper.register(hbs)
GteHelper.register(hbs)
HelperMissingHelper.register(hbs)
IfHelper.register(hbs)
UnlessHelper.register(hbs)
Expand Down
38 changes: 38 additions & 0 deletions spec/ruby-handlebars/helpers/default_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require_relative '../../spec_helper'
require_relative './shared'

require_relative '../../../lib/ruby-handlebars'
require_relative '../../../lib/ruby-handlebars/helpers/default_helper'

describe Handlebars::Helpers::BooleanHelper do
subject { Handlebars::Helpers::BooleanHelper }
let(:hbs) {Handlebars::Handlebars.new}

context '.apply' do
it 'simply forward values to .cmp' do
allow(subject).to receive(:cmp)
subject.apply(hbs, 1, 2)

expect(subject).to have_received(:cmp).once.with(1, 2)
end

it "returns 'true' (as a string) when cmp returns true" do
allow(subject).to receive(:cmp).and_return(true)

expect(subject.apply(hbs, 1, 2)).to eq('true')
end

it "returns an empty string when cmp returns false" do
allow(subject).to receive(:cmp).and_return(false)

expect(subject.apply(hbs, 1, 2)).to eq('')
end

it 'returns an empty string when cmp raises an exception' do
allow(subject).to receive(:cmp).and_raise("Whatever error")

expect(subject.apply(hbs, 1, 2)).to eq('')

end
end
end
47 changes: 47 additions & 0 deletions spec/ruby-handlebars/helpers/eq_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require_relative '../../spec_helper'
require_relative './shared'

require_relative '../../../lib/ruby-handlebars/helpers/eq_helper'


describe Handlebars::Helpers::EqHelper do
let(:subject) { Handlebars::Helpers::EqHelper }
let(:hbs) {Handlebars::Handlebars.new}

it_behaves_like "a registerable helper", "eq"

context '.apply' do
include_context "shared apply helper"

it 'returns "true" when first value equals second one' do
expect(subject.apply(hbs, 1, 1)).to eq('true')
end

it 'returns an empty string when first value equals second one' do
expect(subject.apply(hbs, 1, 2)).to eq('')
end

it 'returns an empty string when values can not be compared' do
expect(subject.apply(hbs, 123, "456")).to eq('')
end

it 'not not takes the blocks into account' do
subject.apply(hbs, 123, 123, block, else_block)
subject.apply(hbs, 123, 456, block, else_block)

expect(block).not_to have_received(:fn)
expect(else_block).not_to have_received(:fn)
end
end

context 'integration' do
include_context "shared helpers integration tests"

it 'can be used as a condition for an #if helper' do
template = "{{#if (eq a b)}}Ok{{else}}Not ok ...{{/if}}"

expect(evaluate(template, {a: 1, b: 1})).to eq("Ok")
expect(evaluate(template, {a: 1, b: "1"})).to eq("Not ok ...")
end
end
end
47 changes: 47 additions & 0 deletions spec/ruby-handlebars/helpers/gt_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require_relative '../../spec_helper'
require_relative './shared'

require_relative '../../../lib/ruby-handlebars/helpers/gt_helper'


describe Handlebars::Helpers::GtHelper do
let(:subject) { Handlebars::Helpers::GtHelper }
let(:hbs) {Handlebars::Handlebars.new}

it_behaves_like "a registerable helper", "gt"

context '.apply' do
include_context "shared apply helper"

it 'returns "true" when first value is greater than second one' do
expect(subject.apply(hbs, 2, 1)).to eq('true')
end

it 'returns an empty string when first value is greater than second one' do
expect(subject.apply(hbs, 1, 2)).to eq('')
end

it 'returns an empty string when values can not be compared' do
expect(subject.apply(hbs, 123, "456")).to eq('')
end

it 'not not takes the blocks into account' do
subject.apply(hbs, 123, 123, block, else_block)
subject.apply(hbs, 123, 456, block, else_block)

expect(block).not_to have_received(:fn)
expect(else_block).not_to have_received(:fn)
end
end

context 'integration' do
include_context "shared helpers integration tests"

it 'can be used as a condition for an #if helper' do
template = "{{#if (gt a b)}}Ok{{else}}Not ok ...{{/if}}"

expect(evaluate(template, {a: 2, b: 1})).to eq("Ok")
expect(evaluate(template, {a: 1, b: 1})).to eq("Not ok ...")
end
end
end
47 changes: 47 additions & 0 deletions spec/ruby-handlebars/helpers/gte_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require_relative '../../spec_helper'
require_relative './shared'

require_relative '../../../lib/ruby-handlebars/helpers/gte_helper'


describe Handlebars::Helpers::GteHelper do
let(:subject) { Handlebars::Helpers::GteHelper }
let(:hbs) {Handlebars::Handlebars.new}

it_behaves_like "a registerable helper", "gte"

context '.apply' do
include_context "shared apply helper"

it 'returns "true" when first value is greater or equal than second one' do
expect(subject.apply(hbs, 1, 1)).to eq('true')
end

it 'returns an empty string when first value is greater or equal than second one' do
expect(subject.apply(hbs, 1, 2)).to eq('')
end

it 'returns an empty string when values can not be compared' do
expect(subject.apply(hbs, 123, "456")).to eq('')
end

it 'not not takes the blocks into account' do
subject.apply(hbs, 123, 123, block, else_block)
subject.apply(hbs, 123, 456, block, else_block)

expect(block).not_to have_received(:fn)
expect(else_block).not_to have_received(:fn)
end
end

context 'integration' do
include_context "shared helpers integration tests"

it 'can be used as a condition for an #if helper' do
template = "{{#if (gt a b)}}Ok{{else}}Not ok ...{{/if}}"

expect(evaluate(template, {a: 2, b: 1})).to eq("Ok")
expect(evaluate(template, {a: 0, b: 1})).to eq("Not ok ...")
end
end
end