Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'release/0.4.0'

  • Loading branch information...
commit 8a7af1c39b31ac99faa405fae6cb42d33d9ac884 2 parents 09b9bbb + c79097f
@benlangfeld authored
View
9 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
View
5 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
View
4 README.md
@@ -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 } }
View
36 lib/ruby_speech/generic_element.rb
@@ -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
@@ -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
View
2  lib/ruby_speech/grxml/element.rb
@@ -15,6 +15,8 @@ def self.module
GRXML
end
+ alias_method :nokogiri_children, :children
+
include GenericElement
end # Element
end # GRXML
View
16 lib/ruby_speech/grxml/grammar.rb
@@ -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|
View
2  lib/ruby_speech/version.rb
@@ -1,3 +1,3 @@
module RubySpeech
- VERSION = "0.3.4"
+ VERSION = "0.4.0"
end
View
3  ruby_speech.gemspec
@@ -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
View
58 spec/ruby_speech/grxml/grammar_spec.rb
@@ -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
View
14 spec/ruby_speech/grxml_spec.rb
@@ -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
View
9 spec/ruby_speech/ssml_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.