Skip to content

Commit

Permalink
Merge branch 'release/0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
benlangfeld committed Dec 30, 2011
2 parents 09b9bbb + c79097f commit 8a7af1c
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 17 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,12 @@
# develop

# 0.4.0 - 2011-12-30
* Feature: Add the ability to look up child elements by name/attributes easily
* Feature: Allow easy access to a GRXML grammar's root rule element
* Feature: Allow inlining a Grammar's rulerefs
* Bugfix: Ruby 1.8 and JRuby don't do a tree-search for const_defined?
* Bugfix: Don't try to pass a method call up to the DSL block binding if it doesn't respond to the method either

# 0.3.4
* Eager-autoload all elements so that importing will work with elements that havn't been used yet directly
* Allow using the DSL with method calls out of the block
Expand Down
5 changes: 5 additions & 0 deletions Guardfile
@@ -0,0 +1,5 @@
guard 'rspec', :version => 2, :cli => '--format documentation' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec/" }
end
4 changes: 1 addition & 3 deletions README.md
Expand Up @@ -51,9 +51,7 @@ Contruct a GRXML (SRGS) document like this:
```ruby
require 'ruby_speech'

grammy = RubySpeech::GRXML.draw do
self.mode = 'dtmf'
self.root = 'digits'
grammy = RubySpeech::GRXML.draw :mode => 'dtmf', :root => 'digits' do
rule id: 'digits' do
one_of do
0.upto(9) {|d| item { d.to_s } }
Expand Down
36 changes: 24 additions & 12 deletions lib/ruby_speech/generic_element.rb
Expand Up @@ -77,14 +77,28 @@ def eval_dsl_block(&block)
instance_eval &block
end

def children
super.map { |c| self.class.import c }
def children(type = nil, attributes = nil)
if type
expression = type.to_s

expression << '[' << attributes.inject([]) do |h, (key, value)|
h << "@#{key}='#{value}'"
end.join(',') << ']' if attributes

if namespace_href
find "ns:#{expression}", :ns => namespace_href
else
find expression
end
else
super()
end.map { |c| self.class.import c }
end

def embed(other)
case other
when String
self << encode_special_chars(other)
string other
when self.class.root_element
other.children.each do |child|
self << child
Expand All @@ -96,18 +110,16 @@ def embed(other)
end
end

def string(other)
self << encode_special_chars(other)
end

def method_missing(method_name, *args, &block)
const_name = method_name.to_s.sub('ssml', '').titleize.gsub(' ', '')
if self.class.module.const_defined? const_name
if self.class.module.const_defined?(const_name)
const = self.class.module.const_get const_name
if self.class::VALID_CHILD_TYPES.include?(const)
if const == String
self << encode_special_chars(args.first)
else
self << const.new(*args, &block)
end
end
elsif @block_binding# && @block_binding.respond_to?(method_name)
self << const.new(*args, &block)
elsif @block_binding && @block_binding.respond_to?(method_name)
@block_binding.send method_name, *args, &block
else
super
Expand Down
2 changes: 2 additions & 0 deletions lib/ruby_speech/grxml/element.rb
Expand Up @@ -15,6 +15,8 @@ def self.module
GRXML
end

alias_method :nokogiri_children, :children

include GenericElement
end # Element
end # GRXML
Expand Down
16 changes: 16 additions & 0 deletions lib/ruby_speech/grxml/grammar.rb
Expand Up @@ -110,6 +110,22 @@ def tag_format=(s)
write_attr :'tag-format', s
end

def root_rule
children(:rule, :id => root).first
end

def inline
find("//ns:ruleref", :ns => namespace_href).each do |ref|
rule = children(:rule, :id => ref[:uri].sub(/^#/, '')).first
ref.swap rule.nokogiri_children
end

non_root_rules = xpath "./ns:rule[@id!='#{root}']", :ns => namespace_href
non_root_rules.remove

self
end

def +(other)
self.class.new(:base_uri => base_uri).tap do |new_grammar|
(self.children + other.children).each do |child|
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_speech/version.rb
@@ -1,3 +1,3 @@
module RubySpeech
VERSION = "0.3.4"
VERSION = "0.4.0"
end
3 changes: 3 additions & 0 deletions ruby_speech.gemspec
Expand Up @@ -28,4 +28,7 @@ Gem::Specification.new do |s|
s.add_development_dependency %q<rake>, [">= 0"]
s.add_development_dependency %q<mocha>, [">= 0"]
s.add_development_dependency %q<i18n>, [">= 0"]
s.add_development_dependency %q<guard>, [">= 0.9.0"]
s.add_development_dependency %q<guard-rspec>, [">= 0"]
s.add_development_dependency %q<ruby_gntp>, [">= 0"]
end
58 changes: 58 additions & 0 deletions spec/ruby_speech/grxml/grammar_spec.rb
Expand Up @@ -151,6 +151,64 @@ module GRXML
concat.to_s.should_not include('default')
end
end

it "should allow finding its root rule" do
grammar = GRXML::Grammar.new :root => 'foo'
bar = GRXML::Rule.new :id => 'bar'
grammar << bar
foo = GRXML::Rule.new :id => 'foo'
grammar << foo

grammar.root_rule.should == foo
end

it "should allow inlining rule references" do
grammar = GRXML.draw :root => 'pin', :mode => :dtmf do
rule :id => 'digits' do
one_of do
0.upto(9) { |d| item { d.to_s } }
end
end

rule :id => 'pin', :scope => 'public' do
one_of do
item do
item :repeat => '4' do
ruleref :uri => '#digits'
end
"#"
end
item do
"* 9"
end
end
end
end

expected_grammar = GRXML.draw :root => 'pin', :mode => :dtmf do
rule :id => 'pin', :scope => 'public' do
one_of do
item do
item :repeat => '4' do
one_of do
0.upto(9) { |d| item { d.to_s } }
end
end
"#"
end
item do
"* 9"
end
end
end
end

grammar.inline.should == expected_grammar
end

describe "#tokens" do
context "with unquoted tokens"
end
end # Grammar
end # GRXML
end # RubySpeech
14 changes: 14 additions & 0 deletions spec/ruby_speech/grxml_spec.rb
Expand Up @@ -261,6 +261,20 @@ def foo
its(:children) { should == [string,item] }
end
end

it "should allow finding direct children of a particular type, matching certain attributes" do
item = GRXML::Item.new
item1 = GRXML::Item.new :weight => 0.5
item11 = GRXML::Item.new :weight => 0.5
item1 << item11
item << item1
item2 = GRXML::Item.new :weight => 0.7
item << item2
tag = GRXML::Tag.new
item << tag

item.children(:item, :weight => 0.5).should == [item1]
end
end # draw
end # GRXML
end # RubySpeech
9 changes: 8 additions & 1 deletion spec/ruby_speech/ssml_spec.rb
Expand Up @@ -15,13 +15,20 @@ module RubySpeech
end
end

describe "when the return value of the block is a string" do
describe "when the return value of the block is not a string" do
it "should not be inserted into the document" do
expected_doc = SSML::Speak.new
SSML.draw { :foo }.should == expected_doc
end
end

describe "when inserting a string" do
it "should work" do
expected_doc = SSML::Speak.new(:content => "Hi, I'm Fred")
SSML.draw { string "Hi, I'm Fred" }.should == expected_doc
end
end

it "should allow other SSML elements to be inserted in the document" do
doc = SSML.draw { voice :gender => :male, :name => 'fred' }
expected_doc = SSML::Speak.new
Expand Down

0 comments on commit 8a7af1c

Please sign in to comment.