Skip to content

Commit

Permalink
[FEATURE] Add the ability to look up child elements by name/attribute…
Browse files Browse the repository at this point in the history
…s easily
  • Loading branch information
benlangfeld committed Dec 20, 2011
1 parent afebbc0 commit 8c7d515
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/ruby_speech/generic_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,22 @@ 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)
Expand Down
14 changes: 14 additions & 0 deletions spec/ruby_speech/grxml_spec.rb
Original file line number Diff line number Diff line change
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

0 comments on commit 8c7d515

Please sign in to comment.