Skip to content

Commit

Permalink
[FEATURE] Allow parameterisation of the boolean grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
benlangfeld committed Sep 29, 2013
1 parent 3a9bb29 commit a6e7af5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/ruby_speech/grxml/builtins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@ module RubySpeech::GRXML::Builtins
#
# Create a grammar for interpreting a boolean response, where 1 is yes and two is no.
#
# @param [Hash] options Options to parameterize the grammar
# @option options [#to_s] :y The positive/truthy/affirmative digit
# @option options [#to_s] :n The negative/falsy digit
#
# @return [RubySpeech::GRXML::Grammar] a grammar for interpreting a boolean response.
#
def self.boolean
# @raise [ArgumentError] if the :y and :n options are the same
#
def self.boolean(options = {})
truthy_digit = (options[:y] || '1').to_s
falsy_digit = (options[:n] || '2').to_s

raise ArgumentError, "Yes and no values cannot be the same" if truthy_digit == falsy_digit

RubySpeech::GRXML.draw mode: :dtmf, root: 'boolean' do
rule id: 'boolean', scope: 'public' do
one_of do
item do
tag { 'true' }
'1'
truthy_digit
end
item do
tag { 'false' }
'2'
falsy_digit
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions spec/ruby_speech/grxml/builtins_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@
it { should not_match('0') }
it { should not_match('3') }
it { should not_match('10') }

context "with the true/false digits parameterized" do
subject { described_class.boolean y: 3, n: 7 }

it { should max_match('3').and_interpret_as('true') }
it { should max_match('7').and_interpret_as('false') }

it { should not_match('1') }
it { should not_match('2') }
it { should not_match('4') }

context "both the same" do
it "should raise ArgumentError" do
expect { described_class.boolean y: '1', n: 1 }.to raise_error(ArgumentError, /same/)
end
end
end
end

describe "currency" do
Expand Down

0 comments on commit a6e7af5

Please sign in to comment.